package com.shkj.wcs.service.impl;

import cn.hutool.core.bean.BeanUtil;
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.wcs.domain.BasePoint;
import com.shkj.wcs.service.IBasePointService;
import com.shkj.wms.bo.BusinPickupAddBo;
import com.shkj.wms.bo.BusinPickupDtlAddBo;
import com.shkj.wms.bo.BusinPickupQueryBo;
import com.shkj.wms.domain.BusinPickup;
import com.shkj.wms.domain.BusinPickupDtl;
import com.shkj.wms.domain.WcsBasePoint;
import com.shkj.wms.mapper.BusinPickupMapper;
import com.shkj.wcs.service.IBusinPickupDtlService;
import com.shkj.wms.service.IBusinPickupService;
import com.shkj.wms.vo.BusinPickupVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zjx
 */
@Slf4j
@Service
public class BusinPickupServiceImpl extends ServiceImpl<BusinPickupMapper, BusinPickup> implements IBusinPickupService {


    @Autowired
    IBusinPickupDtlService iBusinPickupDtlService;

    @Autowired
    IBasePointService basePointService;

    @Override
    public List<BusinPickupVo> getBusinPickupList(BusinPickupQueryBo bo) {
        return baseMapper.getBusinPickupList(bo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<Boolean> saveBusinPickupRecord(BusinPickupAddBo bo) {

        List<BusinPickupDtlAddBo> dtlAddBos = bo.getDtlAddBos();
        BusinPickup businPickup = new BusinPickup();
        businPickup.setPointNo(bo.getPointNo());
        businPickup.setTaskCode(bo.getTaskCode());
        businPickup.setContainerId(bo.getContainerId());
        businPickup.setCreateTime(DateUtils.getNowDate());
        ;businPickup.setBranchId(SecurityUtils.getCurrentBranchId());
        businPickup.setCreateBy(SecurityUtils.getUsername());
        Long puckupId = this.baseMapper.insertPuckup(businPickup);
        String taskType = bo.getTaskType();

        List<BusinPickupDtl> dtlList = new ArrayList<>();
        for (BusinPickupDtlAddBo dtlAddBo : dtlAddBos) {
            BusinPickupDtl businPickupDtl = BeanUtil.toBean(dtlAddBo, BusinPickupDtl.class);
            businPickupDtl.setPickupId(puckupId);
            businPickupDtl.setPickupTime(DateUtils.getNowDate());
            businPickupDtl.setBranchId(SecurityUtils.getCurrentBranchId());
            dtlList.add(businPickupDtl);
        }

        iBusinPickupDtlService.saveBusinPickupDtl(dtlList);

        //AGV搬运到冲孔区，取货完成后，需要处理治具
        if ("3".equals(taskType)){

        }else {
            // 更新点位状态 为空闲
            String pointNo = bo.getPointNo();
            BasePoint basePoint = basePointService.selectBasePointByPointNo(pointNo);
            basePoint.setIsOccupy("0");
            return  basePointService.updateById(basePoint)?Result.ok():Result.err();
        }

        return Result.ok();
    }


}
