package com.sw.mes.yb.turnstore.service.impl;


import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.entity.TurnstoreTrayItem;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.core.utils.BeanUtil;
import com.sw.mes.yb.turnstore.dao.RecheckInspectReceiveDetailDao;
import com.sw.mes.yb.turnstore.dto.YbReturnReceiptDto;
import com.sw.mes.yb.turnstore.entity.RecheckInspectReceive;
import com.sw.mes.yb.turnstore.entity.RecheckInspectReceiveDetail;
import com.sw.mes.yb.turnstore.service.RecheckInspectReceiveDetailService;
import com.sw.mes.yb.turnstore.service.RecheckInspectReceiveService;
import com.sw.mes.yb.turnstore.service.TurnstoreTrayItemService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

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


/**
 * @Author
 * @Description：
 * @Date: Create in
 * @Modified By:
 */
@Service
@Transactional
public class RecheckInspectReceiveDetailServiceImpl extends BaseServiceImpl<RecheckInspectReceiveDetail> implements RecheckInspectReceiveDetailService {

    RecheckInspectReceiveDetailDao dao;

    @Autowired
    private RecheckInspectReceiveService recheckInspectReceiveService;

    @Autowired
    private TurnstoreTrayItemService turnstoreTrayItemService;

    @Autowired
    public void setDao(RecheckInspectReceiveDetailDao dao) {
        super.setBaseDao(dao);
        this.dao = dao;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result normal(YbReturnReceiptDto receiptDto, String[] crystalNoList) {
        //遍历晶段编号列表匹配退库记录
        try {
            List<RecheckInspectReceiveDetail> receiveDetailList = new ArrayList<>();
            for (String crystalNo : crystalNoList) {
                RecheckInspectReceiveDetail receiveDetail = findByCrystalNo(crystalNo);
                if (null != receiveDetail && null == receiveDetail.getQuitType()) {
                    //已发生退库
                    return ResultGenerator.fail("单晶编号: " + crystalNo + "已发生退库并且待确认,不能进行重复退库");
                } else {
                    //获取单晶出库数据
                    TurnstoreTrayItem trayItem = turnstoreTrayItemService.findByTrayNoAndSectionNo(receiptDto.getBoxCode(), crystalNo);
                    if (null != trayItem && trayItem.getState() == 1) {
                        RecheckInspectReceiveDetail entity = new RecheckInspectReceiveDetail();
                        BeanUtils.copyProperties(trayItem, entity, BeanUtil.getIgnoreProperties(trayItem, null, null));
                        entity.setTrayNo(receiptDto.getBoxCode());
                        entity.setConfirmTime(new Date());
                        entity.setCrystalNo(trayItem.getSectionNo());
                        entity.setProductLevel(trayItem.getProductLevel());
                        entity.setExperGrade(trayItem.getProductLevelExperiment());
                        entity.setSpecs(trayItem.getProductTypeSecond());
                        entity.setInputLength(trayItem.getWareHouseLength());
                        entity.setInstoreWeight(trayItem.getActualWeight());
                        entity.setWeight(trayItem.getActualWeight());
                        entity.setLength(trayItem.getActualLength());
                        entity.setReturnRemark("综计退库");
                        //保存详情
                        receiveDetailList.add(entity);
                    } else {
                        //手动回滚事务
                        TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                        return ResultGenerator.fail(crystalNo + " 该晶段编号不存在组托记录");
                    }
                }
            }
            //封装退库主表
            if (!receiveDetailList.isEmpty()) {
                RecheckInspectReceive receive = new RecheckInspectReceive();
                // 退库主表保存一条记录
                RecheckInspectReceive receive1 = recheckInspectReceiveService.findByTrayNo(receiptDto.getBoxCode());
                if(null != receive1){
                    receive.setId(receive1.getId());
                    if(null != receive1.getCrystalNum()) {
                        receive.setCrystalNum(receive1.getCrystalNum() + receiveDetailList.size());
                    } else {
                        receive.setCrystalNum(receiveDetailList.size());
                    }
                }
                receive.setTrayNo(receiptDto.getBoxCode());
                receive.setOutOrderNo(receiptDto.getDefine1());
//                receive.setCrystalNum(receiveDetailList.size());
                receive.setState(0);
                receive.setSource("综计退库");
                receive.setApplyTime(new Date());
                recheckInspectReceiveService.saveOrUpdate(receive);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result writeOff(String[] crystalNoList) {
        //遍历晶段编号列表匹配退库记录
        try {
            for (String crystalNo : crystalNoList) {
                RecheckInspectReceiveDetail receiveDetail = findByCrystalNo(crystalNo);
                if (null == receiveDetail) {
                    //未发生退库
                    return ResultGenerator.fail("单晶编号: " + crystalNo + "未发生过退库,不能进行冲销");
                } else {
                    //已确认退库
                    if (null != receiveDetail.getQuitType()) {
                        return ResultGenerator.fail("单晶编号: " + crystalNo + "已确认接收,不能进行冲销");
                    } else {
                        //执行冲销
                        RecheckInspectReceive receive = recheckInspectReceiveService.findByTrayNo(receiveDetail.getTrayNo());
                        if (null != receive) {
                            receive.setState(4);
                            recheckInspectReceiveService.saveOrUpdate(receive);
                            //执行退站操作
                            //wipStorageService.exitStation(crystalNo);
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success();
    }

    @Override
    public RecheckInspectReceiveDetail findByCrystalNo(String crystalNo) {
        return dao.findByCrystalNo(crystalNo);
    }
}
