package com.yiqi.core.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.yiqi.common.exception.ServiceException;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yiqi.common.enums.SourceType;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.SecurityUtils;
import com.yiqi.core.domain.GoodsInfo;
import com.yiqi.core.domain.GoodsStore;
import com.yiqi.core.domain.dto.GoodsUpdateSalesDTO;
import com.yiqi.core.domain.dto.weapp.WeAppGoodsInfoQueryDTO;
import com.yiqi.core.domain.vo.GoodsInfoQueryVO;
import com.yiqi.core.domain.vo.weapp.WeAppGoodsInfoQueryVO;
import com.yiqi.core.mapper.GoodsInfoMapper;
import com.yiqi.core.mapper.GoodsSkuMapper;
import com.yiqi.core.service.IGoodsInfoService;
import com.yiqi.core.service.IGoodsStoreService;
import com.yiqi.order.domain.dto.RetailQueryGoodsDTO;
import com.yiqi.order.domain.vo.OrderGoodsSkuInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 商品管理Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-21
 */
@Service
public class GoodsInfoServiceImpl extends ServiceImpl<GoodsInfoMapper, GoodsInfo> implements IGoodsInfoService {

    @Autowired
    IGoodsStoreService goodsStoreService;
    @Autowired
    GoodsSkuMapper goodsSkuMapper;

    @Override
    public List<WeAppGoodsInfoQueryVO> findPage(WeAppGoodsInfoQueryDTO weAppGoodsInfoQueryDTO) {
        return baseMapper.findPage(weAppGoodsInfoQueryDTO);
    }

    /**
     * 查询商品管理
     *
     * @param id 商品管理主键
     * @return 商品管理
     */
    @Override
    public GoodsInfo selectGoodsById(Long id) {
        return baseMapper.selectGoodsById(id);
    }

    /**
     * 查询商品管理列表
     *
     * @param goodsInfoQueryVO 商品管理
     * @return 商品管理
     */
    @Override
    public List<GoodsInfoQueryVO> selectGoodsList(GoodsInfoQueryVO goodsInfoQueryVO) {
        List<GoodsInfoQueryVO> goodsInfoQueryVOList = baseMapper.selectGoodsList(goodsInfoQueryVO);
        for(GoodsInfoQueryVO goodsInfoQuery : goodsInfoQueryVOList){
            goodsInfoQuery.setGoodsItemNum(goodsSkuMapper.getGoodsSkuNumsByGoodsId(goodsInfoQuery.getId()));
        }
        return goodsInfoQueryVOList;
    }

    /**
     * 新增商品管理
     *
     * @param goods 商品管理
     * @return 结果
     */
    @Override
    public int insertGoods(GoodsInfo goods) {
        goods.buildCreateData();
        goods.setSellNum(0);
        goods.setGoodComment(BigDecimal.ZERO);
        int i = baseMapper.insertGoods(goods);

        // 新增商品总部关系
        GoodsStore goodsStore = new GoodsStore();
        goodsStore.setSourceType(SourceType.MANAGER.getCode());
        goodsStore.setGoodsId(goods.getId());
        goodsStore.setStatus("0");
        goodsStore.buildCreateData();
        goodsStoreService.insertGoodsStore(goodsStore);

        // 新增商品工厂关系
        goodsStoreService.insertGoodsToAllFactory(goodsStore);
        // 新增商品门店关系
        goodsStoreService.insertGoodsToAllStore(goodsStore);

        return i;
    }

    /**
     * 修改商品管理
     *
     * @param goods 商品管理
     * @return 结果
     */
    @Override
    public int updateGoods(GoodsInfo goods) {
        goods.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateGoods(goods);
    }

    /**
     * 批量删除商品管理
     *
     * @param ids 需要删除的商品管理主键
     * @return 结果
     */
    @Override
    public int deleteGoodsByIds(Long[] ids) {
        return baseMapper.deleteGoodsByIds(ids);
    }

    /**
     * 删除商品管理信息
     *
     * @param id 商品管理主键
     * @return 结果
     */
    @Override
    public int deleteGoodsById(Long id) {
        return baseMapper.deleteGoodsById(id);
    }

    /**
     * 修改商品管理状态
     *
     * @param goods 商品管理
     * @return 结果
     */
    @Override
    public int updateGoodsStatus(GoodsInfo goods) {
        goods.setUpdateBy(SecurityUtils.getUsername());
        goods.setUpdateById(SecurityUtils.getUserId());
        goods.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateGoods(goods);
    }

    /**
     * 查询商品管理
     *
     * @param ids 商品管理主键
     * @return 商品管理
     */
    @Override
    public int selectGoodsByBrandIds(Long[] ids) {
        return baseMapper.selectGoodsByBrandIds(ids);
    }

    /**
     * 查询商品管理
     *
     * @param ids 商品管理主键
     * @return 商品管理
     */
    @Override
    public int selectGoodsCategoryIds(Long[] ids) {
        return baseMapper.selectGoodsCategoryIds(ids);
    }

    @Override
    public List<GoodsInfo> selectGoodsByCategoryId(RetailQueryGoodsDTO retailQueryGoodsDTO) {
        retailQueryGoodsDTO.setSourceType(SecurityUtils.getLoginUser().getSourceType());
        retailQueryGoodsDTO.setOrgId(SecurityUtils.getLoginUser().getOrgId());
        return baseMapper.selectGoodsByCategoryId(retailQueryGoodsDTO);
    }

    /**
     * 处理商品销量
     * @param goodsUpdateSales
     * @param isAdd  是否增加销售
     * @return
     */
    @Override
    public int handleGoodsSellNum(List<GoodsUpdateSalesDTO> goodsUpdateSales, boolean isAdd) {
        if (isAdd){
            return baseMapper.addGoodsSaleNumber(goodsUpdateSales);
        }else {
            return baseMapper.minusGoodsSaleNumber(goodsUpdateSales);
        }
    }

    /**
     * 直接更新虚拟销售的值
     * @param goodsId
     * @param virtualSellNum
     * @return
     */
    @Override
    public int updateGoodsVirtualSellNum(Long goodsId, Integer virtualSellNum) {
         this.update(new UpdateWrapper<GoodsInfo>().lambda()
                .set(GoodsInfo::getVirtualSellNum, virtualSellNum)
                .eq(GoodsInfo::getId, goodsId));
        return 1;
    }

    /**
     * 获取商品物料信息，通过物料id
     * @param skuIdList
     * @param sourceType
     * @return
     */
    @Override
    public List<OrderGoodsSkuInfoVO> getSkuInfoList(List<Long> skuIdList, String sourceType) {
        if (SourceType.MANAGER.getCode().equals(sourceType)){
            return baseMapper.getSkuInfoListByApp(skuIdList);
        }
        if (SourceType.STORE.getCode().equals(sourceType)){
            return baseMapper.getSkuInfoListByStore(skuIdList, SecurityUtils.getLoginUser().getSourceType(), SecurityUtils.getLoginUser().getOrgId());
        }
        return null;
    }

}
