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

import com.prolog.eis.constants.redisKey.RedisKey;
import com.prolog.eis.constants.sys.SysParamConstants;
import com.prolog.eis.constants.wms.WmsConstants;
import com.prolog.eis.dao.out.BizOutboundTaskMapper;
import com.prolog.eis.dao.out.OrderLogMapper;
import com.prolog.eis.dao.out.shelves.GridInfoMapper;
import com.prolog.eis.dto.bindgrid.BGDGridDto;
import com.prolog.eis.dto.shelves.OutSaleDetailDto;
import com.prolog.eis.model.outbound.BizOutboundTask;
import com.prolog.eis.model.outbound.OrderLog;
import com.prolog.eis.model.outbound.PrintTask;
import com.prolog.eis.model.shelves.GridInfo;
import com.prolog.eis.service.bindgrid.BindGridSaveResultService;
import com.prolog.eis.service.out.BizOutboundTaskService;
import com.prolog.eis.service.out.PrintTaskService;
import com.prolog.eis.service.shelves.GridInfoService;
import com.prolog.eis.service.sys.SysParamService;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.core.restriction.Restrictions;
import com.prolog.framework.utils.AssertUtils;
import com.prolog.framework.utils.MapUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Version: 1.0
 * @Author: wuhan
 * @Created: 2024/8/1 15:37
 */
@Service
public class BindGridSaveResultServiceImpl implements BindGridSaveResultService {
    @Resource
    private GridInfoMapper gridInfoMapper;

    @Resource
    private BizOutboundTaskMapper bizOutboundTaskMapper;

    @Resource
    private SysParamService sysParamService;

    @Resource
    private GridInfoService gridInfoService;

    @Resource
    private BizOutboundTaskService bizOutboundTaskService;

    @Resource
    private OrderLogMapper orderLogMapper;

    @Resource
    private PrintTaskService printTaskService;

    @Override
    @Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRES_NEW)
    public void saveResult(List<BGDGridDto> gridList) {
        for (BGDGridDto grid : gridList) {
//            this.updateGridOrderId(grid);
//            this.updateOrderIsBindGrid(grid);
            binding(grid);
        }
    }

    private void updateGridOrderId(BGDGridDto grid) {
        Criteria criteria = new Criteria(GridInfo.class);
        criteria.setRestriction(Restrictions.eq(GridInfo::getGridNo, grid.getGridNo()));
        gridInfoMapper.updateMapByCriteria(MapUtils.put("orderId", grid.getBindOrderId()).getMap(), criteria);
    }

    private void updateOrderIsBindGrid(BGDGridDto grid) {
        Criteria criteria = new Criteria(BizOutboundTask.class);
        criteria.setRestriction(Restrictions.eq(BizOutboundTask::getId, grid.getBindOrderId()));
        bizOutboundTaskMapper.updateMapByCriteria(MapUtils.put("isBindGrid", 1).put("bindTime",new Date()).getMap(), criteria);
    }

    private void binding(BGDGridDto bestGridDto) {
        Criteria criteria = new Criteria(GridInfo.class);
        criteria.setRestriction(Restrictions.eq("gridNo", bestGridDto.getGridNo()));
        gridInfoService.updateMapByCriteria(MapUtils.put("orderId", bestGridDto.getBindOrderId()).put("billFlag", 1).put("lastFlag", 1).getMap(), criteria);
        criteria = new Criteria(BizOutboundTask.class);
        criteria.setRestriction(Restrictions.eq("id", bestGridDto.getBindOrderId()));
        bizOutboundTaskService.updateMapByCriteria(MapUtils.put("isBindGrid", WmsConstants.bind).put("bindTime", new Date()).put("extend2", 2).getMap(), criteria);
        BizOutboundTask outboundTask = bizOutboundTaskService.findById(bestGridDto.getBindOrderId());
        orderLogMapper.save(OrderLog.builder()
                .orderId(bestGridDto.getBindOrderId().toString())
                .orderNo(outboundTask.getBillNo())
                .type("格口绑定")
                .remark("绑定格口号：" + bestGridDto.getGridNo())
                .createTime(new Date())
                .build());
        //修改绑定时间
//        new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(new Date());
        sysParamService.updateSysParam(SysParamConstants.GIRL_BIND_TIME, String.valueOf(System.currentTimeMillis()));
        //回告wms绑定信息
        callWmsBind(bestGridDto);
    }

    private void callWmsBind(BGDGridDto bestGridDto) {
        PrintTask printTask = new PrintTask();
        printTask.setPrintCount(0);
        printTask.setGridNo(bestGridDto.getGridNo());
        printTask.setCreateTime(new Date());
        printTask.setStatus(0);
        printTask.setWmsOutTaskId(bestGridDto.getBindOrderId());
        printTask.setType(2);
        printTaskService.save(printTask);
    }

}
