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.wms.bo.BusinContainerUpkeepAddBo;
import com.shkj.wms.bo.BusinContainerUpkeepQueryBo;
import com.shkj.wms.constants.ParameterConstants;
import com.shkj.wms.domain.BusinContainerUpkeep;
import com.shkj.wms.domain.WcsBasePoint;
import com.shkj.wms.mapper.BusinContainerUpkeepMapper;
import com.shkj.wms.vo.BusinContainerUpkeepVo;
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 IBusinContainerUpkeepServiceImpl extends ServiceImpl<BusinContainerUpkeepMapper, BusinContainerUpkeep> implements IBusinContainerUpkeepService {

    @Autowired
    private ISysParameterService sysParameterService;

    @Autowired
    IBasePointService iBasePointService;

    @Override
    public List<BusinContainerUpkeepVo> getContainerUpkeepDate(BusinContainerUpkeepQueryBo bo) {
        //治具保养周期
        bo.setBranchId(SecurityUtils.getCurrentBranchId());
        return this.baseMapper.getContainerUpkeepDate(bo);
    }

    /**
     *  App治具入库-确认入库(保养完成)
     * @param bo bo
     * @return
     */
    @Override
    public Result<Boolean> saveBusintContainerUpkeepEnd(BusinContainerUpkeepAddBo bo) {

        //更新保养的记录，新增
        BusinContainerUpkeep businContainerUpkeep = this.baseMapper.selectById(bo.getId());
        if(businContainerUpkeep != null){
            businContainerUpkeep.setIsUpkeep("1");
            this.baseMapper.updateById(businContainerUpkeep);
        }else {
            businContainerUpkeep = new BusinContainerUpkeep();
        }
        businContainerUpkeep.setIsUpkeep("0");
        businContainerUpkeep.setContainerId(bo.getContainerId());
        businContainerUpkeep.setUpkeepTime(DateUtils.getNowDate());

        String upkeepDay = sysParameterService.selectConfigByKey(ParameterConstants.ParameterSystem.upkeep_day);
        businContainerUpkeep.setNextUpkeep(DateUtils.addDateDay(Integer.valueOf(upkeepDay)));
        businContainerUpkeep.setUpkeepDay(Integer.valueOf(upkeepDay));

        this.baseMapper.insert(businContainerUpkeep);

        //更新点位状态信息，
        //把占用的点位改为已占用
        BasePoint basePoint = new BasePoint();
        basePoint.setPointNo(bo.getPointNo());
        basePoint = iBasePointService.selectBasePoint(basePoint);
        basePoint.setIsOccupy("0");
        iBasePointService.updateById(basePoint);

        //TODO 呼叫AGV，判断任务是否有需要空治具的任务，如果有，则送达

        return Result.ok();
    }

    /**
     * App治具保养-确认取出（去保养）
     *
     * @param bo bo
     * @return 结果
     */
    @Override
    public Result<Boolean> saveBusintContainerUpkeepStart(BusinContainerUpkeepAddBo bo) {

        //获取需要保养治具的点位信息；
        String pointNo = bo.getPointNo();
        BasePoint basePoint = new BasePoint();
        basePoint.setPointNo(pointNo);
        basePoint = iBasePointService.selectBasePoint(basePoint);
        basePoint.setIsOccupy("0");
        iBasePointService.updateById(basePoint);
        return Result.ok();
    }
}
