package com.sckw.goods.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sckw.goods.common.ProductCodeGenerator;
import com.sckw.goods.entity.KwBusinessGoods;
import com.sckw.goods.entity.KwBusinessGoodsMore;
import com.sckw.goods.entity.KwBusinessGoodsReplenish;
import com.sckw.goods.entity.KwBusinessInventoryDetail;
import com.sckw.goods.entity.dto.InventoryDetailDto;
import com.sckw.goods.entity.dto.ReplenishDto;
import com.sckw.goods.entity.req.*;
import com.sckw.goods.entity.res.GoodsDetailByIdRes;
import com.sckw.goods.entity.res.GoodsFindListRes;
import com.sckw.goods.entity.res.GoodsFindPageRes;
import com.sckw.goods.enums.GoodsStatusEnums;
import com.sckw.goods.mapper.GoodsMapper;
import com.sckw.goods.mapper.InventoryDetailMapper;
import com.sckw.goods.mapper.KwBusinessGoodsMoreMapper;
import com.sckw.goods.mapper.KwBusinessGoodsReplenishMapper;
import com.sckw.core.model.constant.Global;
import com.sckw.core.utils.*;
import com.sckw.core.model.enums.*;
import com.sckw.core.model.page.PageRes;
import com.sckw.core.web.response.HttpResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

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


    @Autowired
    private GoodsMapper kwBusinessGoodsMapper;

    @Autowired
    InventoryDetailMapper inventoryDetailMapper;

    @Autowired
    KwBusinessGoodsMoreMapper kwBusinessGoodsMoreMapper;

    @Autowired
    KwBusinessGoodsReplenishMapper kwBusinessGoodsReplenishMapper;

    public PageRes<GoodsFindPageRes> findPage(GoodsPageParam params) {
        PageHelper.startPage(params.getPage(), params.getPageSize());
        List<GoodsFindPageRes> kwBusinessGoods = kwBusinessGoodsMapper.findPage(params);
        kwBusinessGoods.forEach(e -> {
            if(e.getStatus() != null){
                e.setStatusStr(GoodsStatusEnums.getNameByCode(e.getStatus()));
            }
        });
        return new PageRes<>(new PageInfo<>(kwBusinessGoods));
    }

    public PageRes<GoodsFindListRes> findList(GoodsFindListParam kwBusinessGoods) {
        List<GoodsFindListRes> resultList;

        // 判断是否需要分页
        if (kwBusinessGoods.getPage() != null && kwBusinessGoods.getPageSize() != null) {
            // 分页查询
            PageHelper.startPage(kwBusinessGoods.getPage(), kwBusinessGoods.getPageSize());
            resultList = kwBusinessGoodsMapper.findList(kwBusinessGoods);
            return new PageRes<>(new PageInfo<>(resultList)); // 注意这里应该返回resultList而不是kwBusinessGoods
        } else {
            // 不分页查询
            resultList = kwBusinessGoodsMapper.findList(kwBusinessGoods);
            // 直接返回结果列表，不包装PageInfo，除非你有其他非分页的PageRes构造方式
            return new PageRes<>(resultList); // 假设PageRes有合适的构造方法处理非分页情况
        }
    }

    public GoodsDetailByIdRes findById(Long id) {
        return kwBusinessGoodsMapper.findById(id);
    }

    /**
    * @Description: 商品新增
    * @Author: Lt
    * @Date: 2024/5/7 0007 16:39
    */
    public int insert(GoodsInsertParam goodsInsertParam) {
        GoodsFindListParam goodsFindListParam = new GoodsFindListParam();
        goodsFindListParam.setName(goodsInsertParam.getName());
        List<GoodsFindListRes> list = kwBusinessGoodsMapper.findList(goodsFindListParam);
        if (CollectionUtils.isNotEmpty(list)){
            throw new RuntimeException("当前商品已存在,请重新输入");
        }
        KwBusinessGoods kwBusinessGoods = BeanUtils.copyProperties(goodsInsertParam, KwBusinessGoods.class);
        kwBusinessGoods.setGoodsNum(ProductCodeGenerator.generateCode());
        Long goodsId = new IdWorker(Global.NUMERICAL_ONE).nextId();
        kwBusinessGoods.setId(goodsId);
        int insert = kwBusinessGoodsMapper.insert(kwBusinessGoods);
        if (insert > 0 ) {
            // 新增清单数据
            saveGoodsInventoryDetail(goodsId, goodsInsertParam.getInventoryList());
            // 新增更多数据
            saveGoodsMore(goodsId, goodsInsertParam.getMore());
            // 新增补充数据
            saveReplenish(goodsId, goodsInsertParam.getReplenishList());
            // 履约信息保存
            //savePerformance(goodsId, goodsInsertParam.getPerformanceList());

        }
        return 1;
    }

    /**
    * @Description: 保存补充信息
    * @Author: Lt
    * @Date: 2024/5/7 0007 16:35
    */
    public void saveReplenish(Long goodsId, List<ReplenishDto> replenishList)
    {
        List<KwBusinessGoodsReplenish> resList = new ArrayList<>();
        replenishList.forEach(e -> {
            KwBusinessGoodsReplenish kwBusinessGoodsReplenish = new KwBusinessGoodsReplenish();
            kwBusinessGoodsReplenish.setId(new IdWorker(Global.NUMERICAL_ONE).nextId());
            kwBusinessGoodsReplenish.setGoodsId(goodsId);
            kwBusinessGoodsReplenish.setName(e.getName());
            kwBusinessGoodsReplenish.setInfo(e.getInfo());
            resList.add(kwBusinessGoodsReplenish);
        });
        kwBusinessGoodsReplenishMapper.insertBatch(resList);
    }

    /**
    * @Description: 存储更多信息
    * @Author: Lt
    * @Date: 2024/5/7 16:34
    */
    public void saveGoodsMore(Long goodsId, GoodsMoreParam more)
    {
        KwBusinessGoodsMore kwBusinessGoodsMore = new KwBusinessGoodsMore();
        kwBusinessGoodsMore.setId(new IdWorker(Global.NUMERICAL_ONE).nextId());
        kwBusinessGoodsMore.setGoodsId(goodsId);
        kwBusinessGoodsMore.setAddressId(more.getAddressId());
        kwBusinessGoodsMore.setAddressName(more.getAddressName());
        kwBusinessGoodsMore.setOtherUnitType(more.getOtherUnitType());
        kwBusinessGoodsMore.setOtherId(more.getOtherId());
        kwBusinessGoodsMoreMapper.insert(kwBusinessGoodsMore);
    }

    /**
    * @Description: 存储清单信息
    * @Author: Lt
    * @Date: 2024/5/7 16:34
    */
    public int saveGoodsInventoryDetail(Long goodsId, List<InventoryDetailDto> inventoryList)
    {
        List<KwBusinessInventoryDetail> resList = new ArrayList<>();
        inventoryList.forEach(e -> {
            KwBusinessInventoryDetail kwBusinessInventoryDetail = new KwBusinessInventoryDetail();
            kwBusinessInventoryDetail.setId(new IdWorker(Global.NUMERICAL_ONE).nextId());
            kwBusinessInventoryDetail.setGoodsId(goodsId);
            kwBusinessInventoryDetail.setGoodsNum(e.getInventoryNum());
            kwBusinessInventoryDetail.setColor(e.getColor());
            kwBusinessInventoryDetail.setSize(e.getSize());
            resList.add(kwBusinessInventoryDetail);
        });
        return inventoryDetailMapper.insertBatch(resList);
    }

    /**
    * @Description: 编辑商品
    * @Author: Lt
    * @Date: 2024/5/7 16:34
    */
    public int update(GoodsUpdateParam goodsUpdateParam) {
        LambdaQueryWrapper<KwBusinessGoods> QueryWrapper = new LambdaQueryWrapper<>();
        QueryWrapper.eq(KwBusinessGoods::getName, goodsUpdateParam.getName())
                .ne(KwBusinessGoods::getId, goodsUpdateParam.getId())
                .eq(KwBusinessGoods::getDelFlag, Global.NUMERICAL_ZERO);
        KwBusinessGoods kwBusinessGoods = kwBusinessGoodsMapper.selectOne(QueryWrapper);
        if (kwBusinessGoods != null) {
            throw new RuntimeException("当前商品已存在,请重新输入");
        }
        KwBusinessGoods updateParam = BeanUtils.copyProperties(goodsUpdateParam, KwBusinessGoods.class);
        return kwBusinessGoodsMapper.updateById(updateParam);
    }

    public int delete(List<Long> ids)
    {
        LambdaUpdateWrapper<KwBusinessGoods> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(KwBusinessGoods::getDelFlag, Global.YES).in(KwBusinessGoods::getId, ids);
        return kwBusinessGoodsMapper.update(null, updateWrapper);
    }

}
