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

import cn.hutool.core.collection.CollectionUtil;
import com.prolog.eis.constants.base.ContainerTaskStatusConstants;
import com.prolog.eis.dao.out.ContainerInfoMapper;
import com.prolog.eis.dao.out.ContainerTaskMapper;
import com.prolog.eis.dto.strategy.ContainerInfoDto;
import com.prolog.eis.enums.wms.WmsCarryTaskInCode;
import com.prolog.eis.model.container.ContainerInfo;
import com.prolog.eis.model.container.ContainerTask;
import com.prolog.eis.service.out.ContainerInfoService;
import com.prolog.framework.core.restriction.Criteria;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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 ContainerInfoServiceImpl implements ContainerInfoService {

    @Autowired
    private ContainerInfoMapper containerInfoMapper;
    @Autowired
    private ContainerTaskMapper containerTaskMapper;

    /**
     * 新增数据
     *
     * @param containerInfo 容器信息表
     */
    @Override
    public long save(ContainerInfo containerInfo) {
        return containerInfoMapper.save(containerInfo);
    }

    /**
     * 新增批量数据
     *
     * @param containerInfoList 容器信息表
     */
    @Override
    public long saveBatch(List<ContainerInfo> containerInfoList) {
        return containerInfoMapper.saveBatch(containerInfoList);
    }

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

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

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

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

    /**
     * 修改数据
     *
     * @param containerInfo 实例对象
     */
    @Override
    public long update(ContainerInfo containerInfo) {
        return containerInfoMapper.update(containerInfo);
    }

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

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

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

    @Override
    public List<ContainerInfoDto> getContainerInfoDto(String warehouseArea) {
        return containerInfoMapper.getContainerInfoDto(warehouseArea);
    }

    @Override
    public void updateByMap(Map<String, Object> paramMap, Criteria criteria) {
        containerInfoMapper.updateMapByCriteria(paramMap,criteria);
    }

    @Override
    public List<ContainerInfoDto> getContainerInfo() {
        return containerInfoMapper.getContainerInfo();
    }

    /**
     * 获取拆零库区所有剩余库存
     * @return
     */
    @Override
    public List<ContainerInfoDto> findAGVLContainerInfo() {
        //获取拆零库存编码
        final String agvlCode = WmsCarryTaskInCode.AGVL.getCode();
        //获取拆零库所有库存
        List<ContainerInfoDto> containerInfoDtoList = containerInfoMapper.findAGVLContainerInfo(agvlCode);
        if (CollectionUtil.isEmpty(containerInfoDtoList)) {
            return containerInfoDtoList;
        }
        //查询拆零库存已经被定位的库存
        Criteria criteria = new Criteria(ContainerTask.class);
        criteria.restrictionBuilder().eq(ContainerTask::getWmsLocationCode, agvlCode).lt(ContainerTask::getStatus,ContainerTaskStatusConstants.OFFER_FINISH_STATUS).build();
        List<ContainerTask> containerTaskList = containerTaskMapper.findByCriteria(criteria);
        //获取整托托盘并过滤库存
        List<String> full = containerTaskList.stream().filter(s -> s.getContainerType() == ContainerTaskStatusConstants.ONE_FULL_TRAY_TYPE || s.getContainerType() == ContainerTaskStatusConstants.MORE_FULL_TRAY_TYPE).map(ContainerTask::getContainerNo).distinct().collect(Collectors.toList());
        containerInfoDtoList = containerInfoDtoList.stream().filter(containerInfoDto -> !full.contains(containerInfoDto.getContainerNo())).collect(Collectors.toList());
        Map<String, BigDecimal> collect = containerTaskList.stream().collect(Collectors.toMap(ContainerTask::getContainerSubNo, ContainerTask::getQty, BigDecimal::add));
        for (ContainerInfoDto containerInfoDto : containerInfoDtoList) {
            BigDecimal qty = collect.get(containerInfoDto.getContainerSubNo());
            if (Objects.nonNull(qty)) {
                containerInfoDto.setQty(containerInfoDto.getQty().subtract(qty));
            }
        }
        return containerInfoDtoList;
    }
}
