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


import com.sw.mes.core.base.BaseServiceImpl;
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.fb.turnstore.dao.RecheckInspectReceiveDao;
import com.sw.mes.fb.turnstore.dao.RecheckInspectReceiveDetailDao;
import com.sw.mes.fb.turnstore.dto.FbReturnReceiptDto;
import com.sw.mes.fb.turnstore.entity.FbTurnstoreTrayItem;
import com.sw.mes.fb.turnstore.entity.RecheckInspectReceive;
import com.sw.mes.fb.turnstore.entity.RecheckInspectReceiveDetail;
import com.sw.mes.fb.turnstore.service.FbTurnstoreTrayItemService;
import com.sw.mes.fb.turnstore.service.RecheckInspectReceiveDetailService;
import com.sw.mes.fb.turnstore.service.RecheckInspectReceiveService;
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
 * @Description：
 * @Date: Create in
 * @Modified By:
 */
@Service
@Transactional
public class RecheckInspectReceiveDetailServiceImpl extends BaseServiceImpl<RecheckInspectReceiveDetail> implements RecheckInspectReceiveDetailService {

    RecheckInspectReceiveDetailDao dao;

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

    @Autowired
    private RecheckInspectReceiveService recheckInspectReceiveService;

    @Autowired
    private FbTurnstoreTrayItemService fbTurnstoreTrayItemService;

    @Autowired
    private RecheckInspectReceiveDao recheckInspectReceiveDao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result normal(FbReturnReceiptDto 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 {
                    //获取单晶出库数据
                    FbTurnstoreTrayItem trayItem = fbTurnstoreTrayItemService.findByTrayNoAndSectionNo(receiptDto.getBoxCode(), crystalNo);
                    if (null != trayItem && trayItem.getState() == 1) {
                        RecheckInspectReceiveDetail entity = new RecheckInspectReceiveDetail();
                        BeanUtil.copyProperties(trayItem, entity, BeanUtil.getIgnoreProperties(trayItem, null, null));
                        entity.setTrayNo(receiptDto.getBoxCode());
                        entity.setCrystalNo(trayItem.getSectionNo());
                        entity.setYbProductLevel(trayItem.getProductLevel());
                        entity.setFbProductLevel(trayItem.getProductLevelFb());
                        entity.setExperGrade(trayItem.getFbExperiLevel());
                        entity.setSpecs(trayItem.getSpecification());
                        entity.setTheoryWeight(trayItem.getTheoryWeightFb());
                        entity.setConductiveType(trayItem.getConductionType());
                        entity.setFbCrystalGrayness(trayItem.getSquareRodGrayness());
                        entity.setInputType(trayItem.getProductLevelFb());
                        entity.setReturnRemark("综计退库");
                        //保存详情
                        receiveDetailList.add(entity);
                        //更新站点信息--WMS出站,方棒检验待进站
                        //wipStorageService.outStation(crystalNo, "A", null);
                    } else {
                        return ResultGenerator.fail(crystalNo + " 该晶段编号不存在组托记录");
                    }
                }
            }
            //封装退库主表
            if (!receiveDetailList.isEmpty()) {
                RecheckInspectReceive receive = new RecheckInspectReceive();
                receive.setTrayNo(receiptDto.getBoxCode());
                receive.setOutOrderNo(receiptDto.getDefine1());
                receive.setUser(receiptDto.getDefine2());
                receive.setCrystalNum(receiveDetailList.size());
                receive.setState(0);
                receive.setSource("综计退库");
                receive.setApplyTime(receiptDto.getDefine3());
                receive.setRemark(receiptDto.getDefine4());
                receive.setCustomer(receiptDto.getCustomer());
                RecheckInspectReceive save = recheckInspectReceiveDao.save(receive);
                //保存列表
                for (RecheckInspectReceiveDetail receiveDetail : receiveDetailList) {
                    receiveDetail.setRecheckReceiveId(save.getId());
                    saveOrUpdate(receiveDetail);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result writeOff(String[] crystalNoList, FbReturnReceiptDto receiptDto) {
        //遍历晶段编号列表匹配退库记录
        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);
                            receive.setConfirmUser(receiptDto.getDefine2());
                            receive.setConfirmTime(receiptDto.getDefine3());
                            recheckInspectReceiveService.saveOrUpdate(receive);
                            //执行退站操作
                            //wipStorageService.exitStation(crystalNo);
                        }
                        receiveDetail.setState(4);
                        saveOrUpdate(receiveDetail);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.fail(e.getMessage());
        }
        return ResultGenerator.success();
    }

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

    @Override
    public RecheckInspectReceiveDetail findByCrystalNoAndState(String crystalNo,Integer state) {
        List<RecheckInspectReceiveDetail> list = dao.findByCrystalNoAndStateOrderByConfirmTimeDesc(crystalNo, state);
        if (list != null && list.size() > 0){
            return list.get(0);
        }
        return null;
    }



    @Override
    public Integer isSprework(String sectionNo) {
        return dao.isSprework(sectionNo);
    }

    @Override
    public List<RecheckInspectReceiveDetail> findByRecheckReceiveId(Integer id) {
        return dao.findByRecheckReceiveId(id);
    }
}
