package com.shkj.wcs.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shkj.common.core.domain.Result;
import com.shkj.common.utils.DateUtils;
import com.shkj.common.utils.SecurityUtils;
import com.shkj.system.service.ISysParameterService;
import com.shkj.wcs.domain.BasePoint;
import com.shkj.wcs.service.IBasePointService;
import com.shkj.wcs.service.IBusinContainerUpkeepService;
import com.shkj.wcs.service.IBusinEnptyContainerService;
import com.shkj.wms.bo.BasePointDeviceQueryBo;
import com.shkj.wms.bo.BusinContainerUpkeepAddBo;
import com.shkj.wms.bo.BusinContainerUpkeepQueryBo;
import com.shkj.wms.bo.BusinTaskQueryBo;
import com.shkj.wms.constants.ParameterConstants;
import com.shkj.wms.domain.BaseContainer;
import com.shkj.wms.domain.BusinContainerUpkeep;
import com.shkj.wms.mapper.BaseContainerMapper;
import com.shkj.wms.mapper.BasePointDeviceMapper;
import com.shkj.wms.mapper.BusinContainerUpkeepMapper;
import com.shkj.wms.service.IBusinDataTaskService;
import com.shkj.wms.vo.BasePointDeviceVo;
import com.shkj.wms.vo.BusinContainerUpkeepVo;
import com.shkj.wms.vo.BusinDataTaskVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author zjx
 */
@Slf4j
@Service
public class IBusinEnptyContainerServiceImpl extends ServiceImpl<BaseContainerMapper, BaseContainer> implements IBusinEnptyContainerService {

    @Autowired
    private ISysParameterService sysParameterService;

    @Autowired
    IBasePointService iBasePointService;

    @Autowired
    IBusinDataTaskService iBusinDataTaskService;

    @Autowired
    BasePointDeviceMapper basePointDeviceMapper;

    @Override
    public Boolean emptyContainerFlowDir(String containerCode) {
        /**
         * * 1、根据设定的保养参数，及上次保养时间，计算是否需要保养，如果需要，先保养
         * *   如果保养存放的位置满了，就不再送过去，下次再来计算；
         * * 2、入库接驳口是否需要治具
         * * 3、是否有待呼叫空治具的任务
         * * 4、入库
         */

        //治具是否需要保养
        // 治具保养周期
        int upkeepDay =Integer.valueOf(sysParameterService.selectConfigByKey(ParameterConstants.ParameterSystem.upkeep_day));
        BusinContainerUpkeepQueryBo bo = new BusinContainerUpkeepQueryBo();
        bo.setContainerCode(containerCode);
        BusinContainerUpkeepVo containerUpkeepDay =this.getContainerUpkeepDay(bo);
        int daysUsed = Integer.valueOf(containerUpkeepDay.getDaysUsed());
        //治具使用天数大于等于保养周期，则送去保养
        if (Integer.valueOf(daysUsed)>=upkeepDay){
            //获取维修区点位信息
            String tepairGroup = sysParameterService.selectConfigByKey(ParameterConstants.ParameterSystem.tepair_group);
            BasePointDeviceQueryBo pointQueryBo = new BasePointDeviceQueryBo();
            pointQueryBo.setGroupNo(tepairGroup);

            BasePointDeviceVo pointDevice = basePointDeviceMapper.selectBasePointByGroupno(pointQueryBo);
            //维修区有点位，则送往维修区
            if(pointDevice != null){
                String isDown = pointDevice.getIsDown();
                String pointNo = pointDevice.getPointNo();
                //呼叫AGV

            }
        }

        //入库接驳口是否需要空治具


     /*   //是否有呼叫空治具的任务
        BusinTaskQueryBo queryBo = new BusinTaskQueryBo();
        queryBo.setTaskType("8");
        List<BusinDataTaskVo> businDataTaskVos = iBusinDataTaskService.selectBusinDataTask(queryBo);
        if (businDataTaskVos.size()>0){
            BusinDataTaskVo dataTaskVo = businDataTaskVos.get(0);
            //目标节点
            String endPoint = dataTaskVo.getTaskPath();
            String isDown = dataTaskVo.getIsDown();
            //呼叫AGV
            //TODO ff
        }*/

        //入库
        //获取立库区的空治具存放区域编号

        return null;
    }

    /**
     *  获取治具已使用天数，判断是否需要保养
     * @param bo bo
     * @return
     */

    public BusinContainerUpkeepVo getContainerUpkeepDay(BusinContainerUpkeepQueryBo bo) {
        //治具保养周期
        bo.setBranchId(SecurityUtils.getCurrentBranchId());
        return this.baseMapper.getContainerUpkeepDay(bo);
    }
}
