package com.prolog.eis.service.out.impl;

import cn.hutool.core.util.StrUtil;
import com.google.common.collect.Lists;
import com.prolog.eis.dto.item.ItemSyncVO;
import com.prolog.eis.dao.out.GoodsInfoMapper;
import com.prolog.eis.dao.shelves.sys.SysParamMapper;
import com.prolog.eis.model.container.GoodsInfo;
import com.prolog.eis.service.out.GoodsInfoService;
import com.prolog.eis.service.shelves.InventoryService;
import com.prolog.eis.service.shelves.ItemService;
import com.prolog.eis.vo.GoodsInfoVO;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.upcloud.base.inventory.model.BatchNumberSync;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author zmr
 * @description: 商品信息表-服务实现类
 * @date 2023-08-30 12:24
 */
@Service
public class GoodsInfoServiceImpl implements GoodsInfoService {

    @Resource
    private GoodsInfoMapper goodsInfoMapper;

    @Resource
    private InventoryService inventoryService;


    @Resource
    SysParamMapper sysParamMapper;

    @Resource
    private ItemService itemService;

    /**
     * 新增数据
     *
     * @param goodsInfo 商品信息表
     */
    @Override
    public long save(GoodsInfo goodsInfo) {
        return goodsInfoMapper.save(goodsInfo);
    }

    /**
     * 新增批量数据
     *
     * @param goodsInfoList 商品信息表
     */
    @Override
    public long saveBatch(List<GoodsInfo> goodsInfoList) {
        return goodsInfoMapper.saveBatch(goodsInfoList);
    }

    /**
     * 查询多条数据
     *
     * @param paramMap 查询起始位置
     * @return 对象列表
     */
    @Override
    public List<GoodsInfo> findByMap(Map<String, Object> paramMap) {
        return goodsInfoMapper.findByMap(paramMap, GoodsInfo.class);
    }

    /**
     * 查询单条数据
     *
     * @param paramMap 查询起始位置
     * @return 对象
     */
    @Override
    public GoodsInfo findOneByMap(Map<String, Object> paramMap) {
        List<GoodsInfo> dataList = findByMap(paramMap);
        if(dataList.isEmpty()) {
            return null;
        }
        return dataList.get(0);
    }

    /**
     * 查询多条数据
     *
     * @param criteria 查询起始位置
     * @return 对象列表
     */
    @Override
    public List<GoodsInfo> findByCriteria(Criteria criteria) {
        return goodsInfoMapper.findByCriteria(criteria);
    }

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public GoodsInfo findById(int id) {
        return goodsInfoMapper.findById(id, GoodsInfo.class);
    }

    /**
     * 修改数据
     *
     * @param goodsInfo 实例对象
     */
    @Override
    public long update(GoodsInfo goodsInfo) {
        return goodsInfoMapper.update(goodsInfo);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     */
    @Override
    public long deleteById(int id) {
        return goodsInfoMapper.deleteById(id, GoodsInfo.class);
    }

    /**
     * 通过参数删除数据
     *
     * @param paramMap 参数
     */
    @Override
    public long deleteByMap(Map<String, Object> paramMap) {
        return goodsInfoMapper.deleteByMap(paramMap, GoodsInfo.class);
    }

    /**
     * 通过参数查询总条数
     *
     * @param paramMap 参数
     */
    @Override
    public long findCountByMap(Map<String, Object> paramMap) {
        return goodsInfoMapper.findCountByMap(paramMap, GoodsInfo.class);
    }

    @Override
    public List<GoodsInfoVO> getGoodsInfo(List<GoodsInfoVO> result) throws Exception {
        List<String> lotIds = result.stream().filter(x -> !StringUtils.isEmpty(x.getLotId())&& !StrUtil.equals(x.getLotId(),"-1")).map(GoodsInfoVO::getLotId)
                .distinct().collect(Collectors.toList());
        List<String> goodsIds = result.stream().filter(x -> !StringUtils.isEmpty(x.getGoodsId())&& !StrUtil.equals(x.getGoodsId(),"-1"))
                .map(GoodsInfoVO::getGoodsId).distinct().collect(Collectors.toList());
        List<BatchNumberSync> batchNumberSyncList = null;
        List<ItemSyncVO> itemSyncVOS = null;
        if (!lotIds.isEmpty()) {
            batchNumberSyncList = inventoryService.getBatchNumberSync(lotIds);

        }
        if (!goodsIds.isEmpty()) {
            itemSyncVOS = itemService.queryItemMst(goodsIds);
        }
        List<GoodsInfoVO> list = Lists.newArrayList();
        for (GoodsInfoVO goodsInfoVO : result) {
            if (null != itemSyncVOS) {
                ItemSyncVO itemSyncVO = itemSyncVOS.stream()
                        .filter(x -> !Objects.isNull(x.getItemId()) && x.getItemId().equals(goodsInfoVO.getGoodsId()))
                        .findFirst().orElse(null);
                if (!Objects.isNull(itemSyncVO)) {
                    goodsInfoVO.setItemCode(itemSyncVO.getItemCode());
                    goodsInfoVO.setPieceBarcode(itemSyncVO.getPieceBarcode());
                    goodsInfoVO.setItemName(itemSyncVO.getItemName());
//                    final List<SysParam> sysParams = sysParamMapper.findByMap(MapUtils.put("paramNo", WmsConstants.DEFAULT_ABC_TYPE).getMap(), SysParam.class);
//                    Assert.notEmpty(sysParams,"商品默认ABC属性未配置！");
                    goodsInfoVO.setAbcType(itemSyncVO.getAbcType());
                    goodsInfoVO.setManufacturerId(itemSyncVO.getManufacturerId());
                    goodsInfoVO.setItemHealthApprovalNbr(itemSyncVO.getItemHealthApprovalNbr());
                    goodsInfoVO.setCargoOwnerName(itemSyncVO.getCargoOwnerName());
                    goodsInfoVO.setPieceConversionRate(itemSyncVO.getPieceConversionRate());
                    goodsInfoVO.setItemHealthCategory(itemSyncVO.getItemHealthCategory());
                    goodsInfoVO.setSpecification(itemSyncVO.getSpecification());
                    goodsInfoVO.setItemHealthCategoryName(itemSyncVO.getItemHealthCategoryName());
                    goodsInfoVO.setSingleVolume(
                            null == itemSyncVO.getUomList() || itemSyncVO.getUomList().isEmpty() ? BigDecimal.ZERO
                                    : itemSyncVO.getUomList().get(0).getVolume().divide(itemSyncVO.getUomList().get(0).getConversionRate(),2,BigDecimal.ROUND_DOWN));
                }
            }
            if (null != batchNumberSyncList) {
                BatchNumberSync batchNumberSync = batchNumberSyncList.stream()
                        .filter(v -> !Objects.isNull(v.getBatchId()) && v.getBatchId().equals(goodsInfoVO.getLotId()))
                        .findFirst().orElse(null);
                if (!Objects.isNull(batchNumberSync)) {
                    goodsInfoVO.setManufacturerLotNum(batchNumberSync.getManufacturerLotNum());
                    goodsInfoVO.setBatchExpiredDate(batchNumberSync.getBatchExpiredDate());
                    goodsInfoVO.setBatchProductionDate(batchNumberSync.getBatchProductionDate());
                    goodsInfoVO.setBatchExpiredDateShow(batchNumberSync.getBatchExpiredDateShow());
                    goodsInfoVO.setBatchProductionDateShow(batchNumberSync.getBatchProductionDateShow());
                }
            }
            list.add(goodsInfoVO);
        }
        return list;
    }

    @Override
    public List<GoodsInfoVO> getGoodsInfoNew(List<GoodsInfoVO> result) throws Exception {
        List<String> lotIds = result.stream().filter(x -> !StringUtils.isEmpty(x.getLotId())).map(GoodsInfoVO::getLotId)
                .distinct().collect(Collectors.toList());
        List<String> goodsIds = result.stream().filter(x -> !StringUtils.isEmpty(x.getGoodsId()))
                .map(GoodsInfoVO::getGoodsId).distinct().collect(Collectors.toList());
        List<BatchNumberSync> batchNumberSyncList = null;
        List<ItemSyncVO> itemSyncVOS = null;
        if (!lotIds.isEmpty()) {
            batchNumberSyncList = inventoryService.getBatchNumberSync(lotIds);

        }
        if (!goodsIds.isEmpty()) {
            itemSyncVOS = itemService.queryItemMst(goodsIds);
        }
        List<GoodsInfoVO> list = Lists.newArrayList();
        for (GoodsInfoVO goodsInfoVO : result) {
            if (null != itemSyncVOS) {
                ItemSyncVO itemSyncVO = itemSyncVOS.stream()
                        .filter(x -> !Objects.isNull(x.getItemId()) && x.getItemId().equals(goodsInfoVO.getGoodsId()))
                        .findFirst().orElse(null);
                if (!Objects.isNull(itemSyncVO)) {
                    goodsInfoVO.setItemCode(itemSyncVO.getItemCode());
                    goodsInfoVO.setPieceBarcode(itemSyncVO.getPieceBarcode());
                    goodsInfoVO.setItemName(itemSyncVO.getItemName());
//                    final List<SysParam> sysParams = sysParamMapper.findByMap(MapUtils.put("paramNo", WmsConstants.DEFAULT_ABC_TYPE).getMap(), SysParam.class);
//                    Assert.notEmpty(sysParams,"商品默认ABC属性未配置！");
                    goodsInfoVO.setAbcType(itemSyncVO.getAbcType());
                    goodsInfoVO.setManufacturerId(itemSyncVO.getManufacturerId());
                    goodsInfoVO.setItemHealthApprovalNbr(itemSyncVO.getItemHealthApprovalNbr());
                    goodsInfoVO.setCargoOwnerName(itemSyncVO.getCargoOwnerName());
                    goodsInfoVO.setPieceConversionRate(itemSyncVO.getPieceConversionRate());
                    goodsInfoVO.setItemHealthCategory(itemSyncVO.getItemHealthCategory());
                    goodsInfoVO.setSpecification(itemSyncVO.getSpecification());
                    goodsInfoVO.setItemHealthCategoryName(itemSyncVO.getItemHealthCategoryName());
                    goodsInfoVO.setSingleVolume(
                            null == itemSyncVO.getUomList() || itemSyncVO.getUomList().isEmpty() ? BigDecimal.ZERO
                                    : itemSyncVO.getUomList().get(0).getVolume().divide(itemSyncVO.getUomList().get(0).getConversionRate(),2,BigDecimal.ROUND_DOWN));
                }
            }
            if (null != batchNumberSyncList) {
                BatchNumberSync batchNumberSync = batchNumberSyncList.stream()
                        .filter(v -> !Objects.isNull(v.getBatchId()) && v.getBatchId().equals(goodsInfoVO.getLotId()))
                        .findFirst().orElse(null);
                if (!Objects.isNull(batchNumberSync)) {
                    goodsInfoVO.setManufacturerLotNum(batchNumberSync.getManufacturerLotNum());
                    goodsInfoVO.setBatchExpiredDate(batchNumberSync.getBatchExpiredDate());
                    goodsInfoVO.setBatchProductionDate(batchNumberSync.getBatchProductionDate());
                    goodsInfoVO.setBatchExpiredDateShow(batchNumberSync.getBatchExpiredDateShow());
                    goodsInfoVO.setBatchProductionDateShow(batchNumberSync.getBatchProductionDateShow());
                }
            }
            list.add(goodsInfoVO);
        }
        return list;
    }
}
