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

import com.prolog.eis.constants.sys.SysParamConstants;
import com.prolog.eis.dao.out.BizOutboundTaskMapper;
import com.prolog.eis.dao.out.shelves.sys.SysParamMapper;
import com.prolog.eis.dto.buildwave.PObOrderDto;
import com.prolog.eis.dto.buildwave.PObResultDto;
import com.prolog.eis.model.outbound.BizOutboundTask;
import com.prolog.eis.model.outbound.BizOutboundTaskDetail;
import com.prolog.eis.model.outbound.OrderLog;
import com.prolog.eis.model.sys.SysParam;
import com.prolog.eis.service.buildwave.BuildWaveSaveResultService;
import com.prolog.eis.service.out.BizOutboundTaskDetailService;
import com.prolog.framework.core.exception.BizException;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.core.restriction.Restrictions;
import com.prolog.framework.utils.MapUtils;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Version: 1.0
 * @Author: wuhan
 * @Created: 2024/7/30 15:10
 */
@Service
@RequiredArgsConstructor
public class BuildWaveSaveResultServiceImpl implements BuildWaveSaveResultService {

    private final BizOutboundTaskMapper bizOutboundTaskMapper;

    private final BizOutboundTaskDetailService bizOutboundTaskDetailService;

    private final SysParamMapper sysParamMapper;

    @Override
    public void saveResult(PObResultDto result) {
        // 获取波次号
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        String formattedTime = now.format(formatter);
        String waveNo = String.format("W%s",formattedTime);
        Criteria c1 = Criteria.forClass(BizOutboundTask.class);
        List<Integer> ids = result.getOrderList().stream().map(PObOrderDto::getOrderId).collect(Collectors.toList());
        if(CollectionUtils.isEmpty(ids)){
            throw new BizException("订单id不能为空");
        }
        // 生成波次号、更新库存命中标识
        Date nowDate = new Date();
        c1.restrictionBuilder().in(BizOutboundTask :: getId,ids.toArray()).build();
        bizOutboundTaskMapper.updateMapByCriteria(MapUtils.put("eisWaveNo",waveNo)
                .put("eisWaveTime",nowDate)
                .put("checkStore",1)
                .put("updateTime",nowDate)
                .getMap(),c1);

        Criteria c2 = Criteria.forClass(BizOutboundTaskDetail.class);
        c2.restrictionBuilder().in(BizOutboundTaskDetail :: getOutboundId, ids.toArray())
                .eq(BizOutboundTaskDetail :: getCheckStore,0).build();
        bizOutboundTaskDetailService.updateMapByCriteria(MapUtils.put("checkStore", 1).getMap(), c2);

        // 将尾波处理更改为0
        List<SysParam> tailWaveSwitchParams = sysParamMapper.findByMap(MapUtils.put("paramNo", SysParamConstants.TAIL_WAVE_SWITCH).getMap(), SysParam.class);
        if (CollectionUtils.isNotEmpty(tailWaveSwitchParams)) {
            SysParam tailWaveSwitchParam = tailWaveSwitchParams.get(0);
            int tailWaveSwitch = Integer.parseInt(tailWaveSwitchParam.getParamValue());
            if(tailWaveSwitch == 1){
                sysParamMapper.updateMapById(tailWaveSwitchParam.getId(),MapUtils.put("paramValue","0").getMap(),SysParam.class);
            }
        }

    }
}
