package com.sckw.goods.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.goods.entity.KwBusinessInventory;
import com.sckw.goods.entity.req.InventoryInsertParam;
import com.sckw.goods.entity.req.InventoryPageListParam;
import com.sckw.goods.entity.req.InventoryUpdateParam;
import com.sckw.goods.entity.res.InventoryFindListRes;
import com.sckw.goods.entity.res.InventoryPageListRes;
import com.sckw.goods.mapper.InventoryMapper;
import com.sckw.core.model.constant.Global;
import com.sckw.core.utils.*;
import com.sckw.core.model.page.PageRes;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @desc:
 * @author: Lt
 * @date: 2024-04-26
 */
@Service
public class InventoryService {

    @Autowired
    InventoryMapper inventoryMapper;

    /**
     * @return
     * @Description: 分类清单添加
     * @Author: Lt
     * @Date: 2024/4/26 17:12
     */
    public int insert(InventoryInsertParam inventoryParam)
    {
        LambdaQueryWrapper<KwBusinessInventory> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(KwBusinessInventory::getName, inventoryParam.getName())
                .eq(KwBusinessInventory::getDelFlag, Global.NUMERICAL_ZERO);
        KwBusinessInventory inventory = inventoryMapper.selectOne(wrapper);
        if (ObjectUtils.isNotNull(inventory)) {
            throw new RuntimeException("分类清单已存在");
        }
        KwBusinessInventory kwBusinessInventory = BeanUtils.copyProperties(inventoryParam, KwBusinessInventory.class);
        return inventoryMapper.insert(kwBusinessInventory);
    }

    /**
    * @Description: 清单编辑
    * @Author: Lt
    * @Date: 2024/4/26 0026 17:20  
    */
    public int update(InventoryUpdateParam inventoryUpdateParam)
    {
        LambdaQueryWrapper<KwBusinessInventory> wrapper = new LambdaQueryWrapper<>();
        wrapper.ne(KwBusinessInventory::getId, inventoryUpdateParam.getId())
                .eq(KwBusinessInventory::getName, inventoryUpdateParam.getName())
                .eq(KwBusinessInventory::getDelFlag, Global.NUMERICAL_ZERO);
        Long l = inventoryMapper.selectCount(wrapper);

        if (l > 0){
            throw new RuntimeException("清单名称已存在");
        }
        KwBusinessInventory updateParam = BeanUtils.copyProperties(inventoryUpdateParam, KwBusinessInventory.class);
        return inventoryMapper.updateById(updateParam);
    }
    public List<InventoryFindListRes> findList(String name) {
        return inventoryMapper.findList(name);
    }

    public PageRes<InventoryPageListRes> findPage(InventoryPageListParam params) {
        PageHelper.startPage(params.getPage(), params.getPageSize());
        List<InventoryPageListRes> kwBusinessGoods = inventoryMapper.findPage(params);
        return new PageRes<>(new PageInfo<>(kwBusinessGoods));
    }

    /**
     * @Description: 清单删除
     * @Author: Lt
     * @Date: 2024/4/26 16:59
     */
    public int delete(List<Long> ids)
    {
        LambdaUpdateWrapper<KwBusinessInventory> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(KwBusinessInventory::getDelFlag, Global.YES).in(KwBusinessInventory::getId, ids);
        return inventoryMapper.update(null, updateWrapper);
    }

    /**
    * @Description: 根据清单ids查询所有清单
    * @Author: Lt
    * @Date: 2024/4/27 14:01
    */
    public List<InventoryPageListRes> inventoryAllByInventoryIds(List<Long> ids)
    {
        InventoryPageListParam inventoryPageListParam = new InventoryPageListParam();
        inventoryPageListParam.setIds(ids);
        List<InventoryPageListRes> inventoryListRes = inventoryMapper.findPage(inventoryPageListParam);
        return inventoryListRes;
    }




}
