package com.sckw.product.service;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sckw.core.common.enums.enums.DictTypeEnum;
import com.sckw.core.model.constant.Global;
import com.sckw.core.utils.BeanUtils;
import com.sckw.core.utils.CollectionUtils;
import com.sckw.product.dao.KwpGoodsUnitMapper;
import com.sckw.product.model.KwpGoodsUnit;
import com.sckw.product.model.vo.res.GoodsUnitDetail;
import com.sckw.system.api.RemoteSystemService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @desc 商品-单位换算
 * @author zk
 * @date 2023/12/4 0004
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class KwpGoodsUnitService {

    @DubboReference(version = "1.0.0", group = "design", check = false)
    private RemoteSystemService remoteSystemService;

    private final KwpGoodsUnitMapper goodsUnitMapper;

    /**
     * @param units 单位换算信息
     * @desc 批量新增
     * @author zk
     * @date 2023/12/4
     **/
    public int insertBatch(List<KwpGoodsUnit> units) {
        AtomicInteger count = new AtomicInteger();
        if (CollectionUtils.isNotEmpty(units)) {
            units.forEach(e -> {
                count.addAndGet(goodsUnitMapper.insert(e));
            });
        }

        return count.get();
    }

    /**
     * @param goodsIds 商品信息ID
     * @desc 批量更新
     * @author zk
     * @date 2023/12/4
     **/
    public void delByGoodsIds(List<Long> goodsIds) {
        LambdaUpdateWrapper<KwpGoodsUnit> wrapper = new LambdaUpdateWrapper<>();
        wrapper.in(KwpGoodsUnit::getGoodsId, goodsIds).eq(KwpGoodsUnit::getDelFlag, Global.NO)
                .set(KwpGoodsUnit::getDelFlag, Global.YES);
        goodsUnitMapper.update(null, wrapper);
    }

    /**
     * @param goodsId 商品主键ID
     * @desc 查询商品单位换算信息
     * @author zk
     * @date 2023/12/4
     **/
    public List<GoodsUnitDetail> findGoodsUnit(Long goodsId) {
        List<GoodsUnitDetail> units = new ArrayList<>();
        List<KwpGoodsUnit> assistUnit = goodsUnitMapper.findGoodsUnit(new HashMap<>(Global.NUMERICAL_SIXTEEN){{put("goodsId", goodsId);}});
        if (CollectionUtils.isNotEmpty(assistUnit)) {
            //数据组装
            assistUnit.forEach(e -> {
                GoodsUnitDetail unit = BeanUtils.copyProperties(e, GoodsUnitDetail.class);
                units.add(unit);
            });
        }
        return units;
    }

    /**
     * @param goodsId 商品主键ID
     * @desc 查询商品单位换算信息
     * @author zk
     * @date 2023/12/4
     **/
    public List<GoodsUnitDetail> findGoodsUnitDetail(Long goodsId) {
        List<GoodsUnitDetail> units = new ArrayList<>();
        List<KwpGoodsUnit> assistUnit = goodsUnitMapper.findGoodsUnit(new HashMap<>(Global.NUMERICAL_SIXTEEN){{put("goodsId", goodsId);}});
        if (CollectionUtils.isNotEmpty(assistUnit)) {
            //单位
            Map<String, Map<String, String>> dict = remoteSystemService.queryDictByType(List.of(DictTypeEnum.UNIT_TYPE.getType()));
            Map<String, String> unitMap = new HashMap<>(Global.NUMERICAL_SIXTEEN);
            if (CollectionUtils.isNotEmpty(dict)) {
                unitMap = dict.get(DictTypeEnum.UNIT_TYPE.getType());
            }

            //数据组装
            Map<String, String> finalUnitMap = unitMap;
            assistUnit.forEach(e -> {
                GoodsUnitDetail unit = BeanUtils.copyProperties(e, GoodsUnitDetail.class);
                unit.setFromUnitName(finalUnitMap != null ? finalUnitMap.get(unit.getFromUnit()) : null);
                unit.setToUnitName(finalUnitMap != null ? finalUnitMap.get(unit.getToUnit()) : null);
                units.add(unit);
            });
        }
        return units;
    }
}
