package com.hzs.sale.wares.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hzs.common.domain.sale.ext.BdWaresDetailExt;
import com.hzs.common.domain.sale.wares.BdWaresDetail;
import com.hzs.sale.order.param.OrderItemsParam;
import com.hzs.sale.order.param.WaresItemsParam;
import com.hzs.sale.wares.mapper.BdWaresDetailMapper;
import com.hzs.sale.wares.param.CuProductParams;
import com.hzs.sale.wares.service.IBdWaresDetailService;
import com.hzs.sale.wares.vo.WaresProductDetailVo;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 商品发布明细表 服务实现类
 */
@Service
public class BdWaresDetailServiceImpl extends ServiceImpl<BdWaresDetailMapper, BdWaresDetail> implements IBdWaresDetailService {

    @Override
    public List<BdWaresDetailExt> queryWaresDetailByCondition(Integer specialArea, Integer transType, List<OrderItemsParam> orderItemsParams) {
        List<BdWaresDetailExt> bdWaresDetailExtList = new ArrayList<>();
        for (OrderItemsParam orderItemsParam : orderItemsParams) {
            for (WaresItemsParam waresItemsParam : orderItemsParam.getWaresItemsParamList()) {
                BdWaresDetailExt bdWaresDetailExt = BdWaresDetailExt.builder()
                        .waresCode(orderItemsParam.getWaresCode())
                        .pkWaresSpecsSku(waresItemsParam.getPkWaresSpecsSku())
                        .waresQuantity(orderItemsParam.getQuantity())
                        .price(BigDecimal.ZERO)
                        .achieve(BigDecimal.ZERO)
                        .build();
                bdWaresDetailExt.setQuantity(waresItemsParam.getQuantity());
                bdWaresDetailExtList.add(bdWaresDetailExt);
            }
        }
        List<BdWaresDetailExt> waresDetailExtList = baseMapper.queryWaresDetailByCondition(specialArea, transType, orderItemsParams);
        if (CollectionUtil.isNotEmpty(waresDetailExtList)) {
            Map<String, BdWaresDetailExt> bdWaresDetailExtMap = new HashMap<>();
            waresDetailExtList.forEach(waresDetailExt -> {
                bdWaresDetailExtMap.put(waresDetailExt.getWaresCode() + waresDetailExt.getPkWaresSpecsSku(), waresDetailExt);
            });
            bdWaresDetailExtList.forEach(waresDetailExt -> {
                String key = waresDetailExt.getWaresCode() + waresDetailExt.getPkWaresSpecsSku();
                if (bdWaresDetailExtMap.containsKey(key)) {
                    BeanUtil.copyProperties(bdWaresDetailExtMap.get(key), waresDetailExt, "waresQuantity", "quantity", "pkMakerSpace");
                }
            });
        } else {
            bdWaresDetailExtList.clear();
        }
        return bdWaresDetailExtList;
    }

    @Override
    public List<CuProductParams> selectByWaresCode(String waresCode, Integer specialArea, Integer isGift) {
        return baseMapper.selectByWaresCode(waresCode, specialArea, isGift);
    }

    @Override
    public List<BdWaresDetailExt> selectByPkWares(Integer pkWares) {
        return baseMapper.selectByPkWares(pkWares);
    }

    @Override
    public List<WaresProductDetailVo> selectByList(Integer pkWares) {
        return baseMapper.selectByList(pkWares);
    }


}
