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

import cn.hutool.core.util.StrUtil;
import com.prolog.eis.dao.out.BizOutboundTaskMapper;
import com.prolog.eis.dao.out.ParcelRecordMapper;
import com.prolog.eis.dao.shelves.GridInfoMapper;
import com.prolog.eis.dto.outbound.GirdScanReqDto;
import com.prolog.eis.dto.outbound.GirdScanRespDto;
import com.prolog.eis.model.outbound.BizOutboundTask;
import com.prolog.eis.model.pack.ParcelRecord;
import com.prolog.eis.model.shelves.GridInfo;
import com.prolog.eis.service.out.ParcelRecordService;
import com.prolog.framework.core.exception.BizException;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.utils.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

@Service
public class ParcelRecordServiceImpl implements ParcelRecordService {

    @Resource
    private ParcelRecordMapper parcelRecordMapper;
    @Resource
    private GridInfoMapper gridInfoMapper;
    @Resource
    private BizOutboundTaskMapper bizOutboundTaskMapper;


    @Override
    public GirdScanRespDto scanGrid(GirdScanReqDto reqDto) {
        if(StringUtils.isEmpty(reqDto.getGridNo())){
            throw new BizException("格口号不能为空");
        }
        if(StringUtils.isEmpty(reqDto.getNickName())){
            throw new BizException("工号不能为空");
        }
        Criteria c1 = Criteria.forClass(GridInfo.class);
        c1.restrictionBuilder().eq(GridInfo :: getGridNo,reqDto.getGridNo()).build();
        List<GridInfo> gridInfos = gridInfoMapper.findByCriteria(c1);
        if(CollectionUtils.isEmpty(gridInfos)){
            throw new BizException("格口不存在");
        }
        GridInfo gridInfo = gridInfos.get(0);
        if(null == gridInfo.getOrderId()){
            throw new BizException("该格口无订单绑定");
        }
        Criteria c2 = Criteria.forClass(ParcelRecord.class);
        c2.restrictionBuilder().eq(ParcelRecord :: getGridNo,reqDto.getGridNo())
                .eq(ParcelRecord :: getBillId,gridInfo.getOrderId())
                .build();
        List<ParcelRecord> parcelRecords = parcelRecordMapper.findByCriteria(c2);
        GirdScanRespDto respDto = null;
        if(CollectionUtils.isNotEmpty(parcelRecords)){
            ParcelRecord parcelRecord = parcelRecords.get(0);
            if(reqDto.getUserName().equals(parcelRecord.getStaffName())){
                respDto =getRespDto(parcelRecord.getBillNo(),parcelRecord.getStaffCode(),parcelRecord.getStaffName(),parcelRecord.getScanDate(),"",gridInfo.getGridNo());
            }else{
                respDto =getRespDto(parcelRecord.getBillNo(),parcelRecord.getStaffCode(),parcelRecord.getStaffName(),parcelRecord.getScanDate(),"登记失败，已被其他人绑定！",gridInfo.getGridNo());
            }
        }else{
            ParcelRecord parcelRecord = new ParcelRecord();
            parcelRecord.setBillId(gridInfo.getOrderId());
            BizOutboundTask bizOutboundTask = bizOutboundTaskMapper.findById(gridInfo.getOrderId(), BizOutboundTask.class);
            parcelRecord.setBillNo(bizOutboundTask.getBillNo());
            parcelRecord.setGridNo(gridInfo.getGridNo());
            parcelRecord.setStaffCode(reqDto.getNickName());
            parcelRecord.setStaffName(reqDto.getUserName());
            parcelRecord.setScanDate(new Date());
            parcelRecord.setCreateTime(new Date());
            parcelRecordMapper.save(parcelRecord);
            respDto = getRespDto(parcelRecord.getBillNo(), parcelRecord.getStaffCode(), parcelRecord.getStaffName(), parcelRecord.getScanDate(), "",gridInfo.getGridNo());
        }
        return respDto;
    }

    private GirdScanRespDto getRespDto(String billNo,String nickName,String userName,Date scanDate,String errMsg,String girdNo) {
        GirdScanRespDto respDto = new GirdScanRespDto();
        respDto.setGridNo(girdNo);
        respDto.setBillNo(billNo);
        respDto.setNickName(nickName);
        respDto.setUserName(userName);
        respDto.setScanDate(scanDate);
        respDto.setErrMsg(errMsg);
        return respDto;
    }

}
