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

import com.prolog.eis.constants.base.OrderStatusConstants;
import com.prolog.eis.dao.out.BizOutboundTaskDetailMapper;
import com.prolog.eis.dto.outbound.SupervisionCodeDto;
import com.prolog.eis.dto.page.PackOrderDetailRespDto;
import com.prolog.eis.dto.strategy.OrderDetailDto;
import com.prolog.eis.model.outbound.BizOutboundTaskDetail;
import com.prolog.eis.service.out.BizOutboundTaskDetailService;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.utils.MapUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @author zmr
 * @description: 出库任务详情表-服务实现类
 * @date 2023-08-30 12:24
 */
@Service
public class BizOutboundTaskDetailServiceImpl implements BizOutboundTaskDetailService {

    @Autowired
    private BizOutboundTaskDetailMapper bizOutboundTaskDetailMapper;

    /**
     * 新增数据
     *
     * @param bizOutboundTaskDetail 出库任务详情表
     */
    @Override
    public long save(BizOutboundTaskDetail bizOutboundTaskDetail) {
        return bizOutboundTaskDetailMapper.save(bizOutboundTaskDetail);
    }

    /**
     * 新增批量数据
     *
     * @param bizOutboundTaskDetailList 出库任务详情表
     */
    @Override
    public long saveBatch(List<BizOutboundTaskDetail> bizOutboundTaskDetailList) {
        return bizOutboundTaskDetailMapper.saveBatch(bizOutboundTaskDetailList);
    }

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

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

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

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

    /**
     * 修改数据
     *
     * @param bizOutboundTaskDetail 实例对象
     */
    @Override
    public long update(BizOutboundTaskDetail bizOutboundTaskDetail) {
        return bizOutboundTaskDetailMapper.update(bizOutboundTaskDetail);
    }

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

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

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

    @Override
    public List<OrderDetailDto> getByOrderIds(List<Integer> orderIds) {
        return bizOutboundTaskDetailMapper.getByOrderIds(orderIds);
    }

    @Override
    public List<OrderDetailDto> findOutDetailList(String billType, String warehouseArea,String isBindGrid) {
        return bizOutboundTaskDetailMapper.findOutDetailList(billType,warehouseArea,isBindGrid);
    }

    @Override
    public void batchUpdate(List<BizOutboundTaskDetail> updateBizOutboundTaskDetailList) {
        for (BizOutboundTaskDetail bizOutboundTaskDetail : updateBizOutboundTaskDetailList) {
            bizOutboundTaskDetailMapper.update(bizOutboundTaskDetail);
        }
    }

    @Override
    public List<PackOrderDetailRespDto> findPackOrderDetail(Integer id,String gridNo) {
        return bizOutboundTaskDetailMapper.findPackOrderDetail(id,gridNo);
    }

    @Override
    public List<PackOrderDetailRespDto> findPackOrderDetailLast(Integer id) {
        return bizOutboundTaskDetailMapper.findPackOrderDetailLast(id);
    }

    public void updateMapByCriteria(Map<String, Object> map, Criteria criteria) {
        bizOutboundTaskDetailMapper.updateMapByCriteria(map, criteria);
    }

    @Override
    public List<OrderDetailDto> findAddOutDetailList(String billType, String warehouseArea, String assemblyOrderSwitch) {
        return bizOutboundTaskDetailMapper.findAddOutDetailList(billType,warehouseArea,assemblyOrderSwitch);
    }

    @Override
    public List<OrderDetailDto> findDetailList(String warehouseArea) {
        return bizOutboundTaskDetailMapper.findDetailList(warehouseArea);
    }

    @Override
    public long findCountByCriteria(Criteria criteria) {
        return bizOutboundTaskDetailMapper.findCountByCriteria(criteria);
    }

    @Override
    public List<SupervisionCodeDto> findSupervisionCode(List<String> drugSupervisionCode) {
        return bizOutboundTaskDetailMapper.findSupervisionCode(drugSupervisionCode);
    }

    @Override
    public void batchUpdateStatus(List<Integer> detailIds, Integer status) {
        if(CollectionUtils.isEmpty(detailIds)){
            return;
        }
        Criteria criteria = Criteria.forClass(BizOutboundTaskDetail.class);
        criteria.restrictionBuilder().in(BizOutboundTaskDetail :: getId,detailIds.toArray()).build();
        bizOutboundTaskDetailMapper.updateMapByCriteria(MapUtils
                .put("status", OrderStatusConstants.START_STATUS)
                .put("updateTime",LocalDateTime.now()).getMap(),criteria);
    }

    @Override
    public List<OrderDetailDto> findAddOutDetailLsList(String billType, String warehouseArea, String assemblyOrderSwitch) {
        return bizOutboundTaskDetailMapper.findAddOutDetailLSList(billType,warehouseArea,assemblyOrderSwitch);
    }
}
