package com.sw.mes.mcs.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.sw.mes.core.api.system.YbFeignClients;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.core.dto.CooperateDataInfoDto;
import com.sw.mes.core.dto.GetFbInfoDto;
import com.sw.mes.core.dto.UploadFbMatchComposeInfoDto;
import com.sw.mes.core.entity.FbCheckOutData;
import com.sw.mes.core.response.Result;
import com.sw.mes.core.response.ResultCode;
import com.sw.mes.core.response.ResultGenerator;
import com.sw.mes.core.utils.BeanUtil;
import com.sw.mes.core.utils.StringUtils;
import com.sw.mes.fb.checkout.entity.CooperateData;
import com.sw.mes.fb.checkout.entity.FbCheckOutRecord;
import com.sw.mes.fb.checkout.service.CooperateDataService;
import com.sw.mes.fb.checkout.service.FbCheckOutDataService;
import com.sw.mes.fb.checkout.service.FbCheckOutRecordService;
import com.sw.mes.mcs.dto.FbAppearanceCheckResultDto;
import com.sw.mes.mcs.dto.FbSizeInfoDto;
import com.sw.mes.mcs.dto.FbWeightDto;
import com.sw.mes.mcs.dto.RecutDto;
import com.sw.mes.mcs.service.McsToMesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * @Author: GaoXinJian
 * @Date: 2022-01-14 10:56
 */
@Service
@Transactional
public class McsToMesServiceImpl implements McsToMesService {
    @Autowired
    private FbCheckOutDataService fbCheckOutDataService;

    @Autowired
    private FbCheckOutRecordService fbCheckOutRecordService;
    @Autowired
    private CooperateDataService cooperateDataService;
    @Autowired
    private YbFeignClients ybFeignService;

    @Override
    public Result fbSizeInfo(FbSizeInfoDto fbSizeInfo) {
        FbSizeInfoDto fbSizeInfoDto = new FbSizeInfoDto();
        BeanUtil.copyBeanPropWithBigDecimalHandler(fbSizeInfoDto, fbSizeInfo);
        FbCheckOutRecord fbCheckOutRecord = fbCheckOutRecordService.findFbCheckOutRecord(fbSizeInfoDto.getFbSectionNo());
        if (fbCheckOutRecord != null) {
            fbSizeInfoDto.setSpecification(fbCheckOutRecord.getSpecification());
            BeanUtils.copyProperties(fbSizeInfoDto, fbCheckOutRecord, BeanUtil.getIgnoreProperties(fbSizeInfoDto, "length", null));
            fbCheckOutRecordService.saveOrUpdateFbCheckOutRecord(fbCheckOutRecord, true, true);
        } else {
            fbCheckOutRecord = new FbCheckOutRecord();
            Result<GetFbInfoDto> fbInfoBySectionNofb = fbCheckOutRecordService.getFbInfoBySectionNofb(fbSizeInfoDto.getFbSectionNo());
            if (fbInfoBySectionNofb == null || fbInfoBySectionNofb.getCode() == null || fbInfoBySectionNofb.getCode() != 200) {
                return fbInfoBySectionNofb;
            } else {
                BeanUtils.copyProperties(fbInfoBySectionNofb.getData(), fbCheckOutRecord, BeanUtil.getIgnoreProperties(fbInfoBySectionNofb.getData(), null, null));
            }
            BeanUtils.copyProperties(fbSizeInfoDto, fbCheckOutRecord, BeanUtil.getIgnoreProperties(fbSizeInfoDto, "length", null));
            fbCheckOutRecordService.saveOrUpdateFbCheckOutRecord(fbCheckOutRecord,false,true);
        }
        return ResultGenerator.success();
    }

    @Override
    public Result fbAppearanceCheckResult(FbAppearanceCheckResultDto fbAppearanceCheckResultDto) {
        FbCheckOutRecord  fbCheckOutRecord = fbCheckOutRecordService.findFbCheckOutRecord(fbAppearanceCheckResultDto.getFbSectionNo());
        if (fbCheckOutRecord != null) {
            BeanUtils.copyProperties(fbAppearanceCheckResultDto, fbCheckOutRecord, BeanUtil.getIgnoreProperties(fbAppearanceCheckResultDto, null, null));
            fbCheckOutRecordService.saveOrUpdateFbCheckOutRecord(fbCheckOutRecord,true,true);
        } else {
            fbCheckOutRecord = new FbCheckOutRecord();
            Result<GetFbInfoDto> fbInfoBySectionNofb = fbCheckOutRecordService.getFbInfoBySectionNofb(fbAppearanceCheckResultDto.getFbSectionNo());
            if (fbInfoBySectionNofb == null || fbInfoBySectionNofb.getCode() == null || fbInfoBySectionNofb.getCode() != 200){
                return fbInfoBySectionNofb;
            }else{
                BeanUtils.copyProperties(fbInfoBySectionNofb.getData(), fbCheckOutRecord, BeanUtil.getIgnoreProperties(fbInfoBySectionNofb.getData(), null, null));
            }
            BeanUtils.copyProperties(fbAppearanceCheckResultDto, fbCheckOutRecord, BeanUtil.getIgnoreProperties(fbAppearanceCheckResultDto, null, null));
            fbCheckOutRecord.setLength(fbCheckOutRecord.getLength() == null ? BigDecimal.ZERO : fbCheckOutRecord.getLength().setScale(0,BigDecimal.ROUND_DOWN));
            fbCheckOutRecordService.saveOrUpdateFbCheckOutRecord(fbCheckOutRecord,false,true);
        }
        return ResultGenerator.success();

    }

    @Override
    public Result fbAppearanceCheckResult(String rfId, String sectionNo) {
        return null;
    }

    @Override
    public Result fbWeightResult(FbWeightDto fbWeightDto) {
        FbCheckOutRecord fbCheckOutRecord = fbCheckOutRecordService.findFbCheckOutRecord(fbWeightDto.getFbSectionNo());
        if (fbCheckOutRecord == null) {
            return ResultGenerator.fail("方棒检验数据表不存在！");
        }
        FbCheckOutRecord updateDto = new FbCheckOutRecord();
        updateDto.setId(fbCheckOutRecord.getId());
        updateDto.setWeight(fbWeightDto.getWeight());
        fbCheckOutRecordService.updateWeight(updateDto);
        return ResultGenerator.success();
    }

    @Override
    public Result cooperateDataInfo(CooperateDataInfoDto cooperateDataInfoDto) {
        /**
         * 	1、mcs调佣配棒上传接口
         * 	2、使用母单晶查询  边长直径等12个字段并保存
         * 		调用生成编号接口  配棒编号
         * 	3、画线记录保存
         *
         * 	4、FLS使用母单晶调用分选口接口，告诉FLS 这颗棒子去到反切线上
         * 	flsToMesController/fbSizing
         *
         * 	5、新增方棒检验时，前端配棒编号通过查询配棒记录，
         * 		有记录  查询 数据字典，确认配棒抽检是否开启，页面是否抽检开启，否则是否抽检则置灰，并提示
         * 		有记录  显示直径、边长等信息
         */
        String fbSectionNo = cooperateDataInfoDto.getCrystalNo();

        // 使用母单晶查询  边长直径等12个字段并保存
        FbCheckOutRecord fbCheckOutRecord = fbCheckOutRecordService.findFbCheckOutRecord(fbSectionNo);

        if (null != fbCheckOutRecord) {
            cooperateDataInfoDto.setSpecs(fbCheckOutRecord.getSpecification());
            cooperateDataInfoDto.setProductGrade(fbCheckOutRecord.getProductLevel());
            Result result = ybFeignService.getCooperateInfo(cooperateDataInfoDto);

            if (ResultCode.SUCCESS.getCode() == result.getCode()) {

                Object data = result.getData();
                RecutDto recutDto = JSONObject.parseObject(JSONObject.toJSONString(data), RecutDto.class);
                // 保存配棒记录
                cooperateDataService.saveInfo(fbCheckOutRecord, recutDto);
                return ResultGenerator.success();

            } else {
                return ResultGenerator.fail("配棒调用YB服务失败:" + result.getMessage());
            }
        } else {
            return ResultGenerator.fail("无方棒检验记录");
        }
    }

    @Override
    public Result uploadFbSpellComposeInfo(UploadFbMatchComposeInfoDto uploadFbMatchComposeInfoDto) {

        if (null == uploadFbMatchComposeInfoDto.getFbMatchCompose() || StringUtils.isEmpty(uploadFbMatchComposeInfoDto.getFbMatchCompose())){
            return ResultGenerator.fail("配棒信息为空！");
        }

        String fbMatchCompose = uploadFbMatchComposeInfoDto.getFbMatchCompose();
        String[] fbSectionNos = fbMatchCompose.split(",");
        if (null != fbSectionNos && fbSectionNos.length>0){
            for (String fbSectionNo : fbSectionNos) {
                FbCheckOutData dataByFbSectionNo = fbCheckOutDataService.findDataByFbSectionNo(fbSectionNo);
                if (null != dataByFbSectionNo){
                    dataByFbSectionNo.setFbMatchCompose(fbMatchCompose);
                    try {
                        fbCheckOutDataService.saveOrUpdate(dataByFbSectionNo);
                    } catch (Exception e) {
                        e.printStackTrace();
                        return ResultGenerator.fail(e.getMessage());
                    }
                }
            }
        }

        return ResultGenerator.success();
    }

    @Override
    public Result findByCooperateNo(CooperateDataInfoDto cooperateDataInfoDto) {
        // 配棒编号
        String cooperateNo = cooperateDataInfoDto.getCrystalNo();
        CooperateData cooperateData = cooperateDataService.findByCooperateNo(cooperateNo);
        if (null != cooperateData) {
            return ResultGenerator.success(cooperateData);
        }
        return ResultGenerator.fail("无配棒信息");
    }
}
