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


import com.sw.mes.core.api.auth.AuthFeignClient;
import com.sw.mes.core.api.system.YbFeignClients;
import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.dto.EapCutCraftDto;
import com.sw.mes.core.dto.UnbindInfoDto;
import com.sw.mes.core.dto.auth.UserDto;
import com.sw.mes.core.entity.User;
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.aio.dao.EapCutRecordDao;
import com.sw.mes.fb.aio.dto.EapCutRecordDto;
import com.sw.mes.fb.aio.entity.EapCutRecord;
import com.sw.mes.fb.aio.service.EapCutCraftService;
import com.sw.mes.fb.aio.service.EapCutRecordService;
import com.sw.mes.base.workinprocess.service.WipStorageService;
import org.modelmapper.ModelMapper;
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.util.Date;


/**
* @Author
* @Description：
* @Date: Create in
* @Modified By:
*/
@Service
@Transactional
public class EapCutRecordServiceImpl extends BaseServiceImpl<EapCutRecord> implements EapCutRecordService {
    EapCutRecordDao dao;
	@Autowired
    public void setDao(EapCutRecordDao dao) {
        super.setBaseDao(dao);
        this.dao=dao;
    }
    @Autowired
    ModelMapper modelMapper;

    @Autowired
    private EapCutRecordService eapCutRecordService;

    @Autowired
    private EapCutCraftService eapCutCraftService;

    @Autowired
    //private UserService userService;
    private AuthFeignClient authFeignClient;

    @Autowired
    private WipStorageService wipStorageService;

    @Autowired
    private YbFeignClients ybFeignClients;

    @Override
    public EapCutRecord getEapCutRecordDtoByRoundSn(String roundSn) {
        return dao.findTopByRoundSnOrderByIdDesc(roundSn);
    }

    @Override
    public Result saveCutRecord(EapCutRecordDto eapCutRecordDto) {
        //自动下料时赋值截断加工记录表上下料用户
        EapCutCraftDto eapCutCraft = new EapCutCraftDto();

        EapCutRecord obj = eapCutRecordService.getEapCutRecordDtoByRoundSn(eapCutRecordDto.getRoundSn());
        if (obj != null && obj.getStartCut() != null && obj.getEndCut() != null){
            obj = null;
        }

        if (obj != null) {
            obj.setRoundSn(eapCutRecordDto.getRoundSn());
            obj.setEquipNo(eapCutRecordDto.getEquipNo());
            obj.setEndCut(eapCutRecordDto.getEndCut());
            obj.setStaffDown(eapCutRecordDto.getStaffDown());
            eapCutRecordService.saveOrUpdate(obj);
            BeanUtils.copyProperties(obj, eapCutCraft, BeanUtil.getIgnoreProperties(obj, "id", null));
            User user = null;
            String cutUpOperator = obj.getStaffUp();//上料用户名
            String cutOperator = obj.getStaffDown();//下料用户名
            //上料用户
            Result<UserDto> upUser = authFeignClient.findByWorkNo(obj.getStaffUp());
            if (upUser != null && upUser.getData().getRealName() != null ){
                cutUpOperator = user.getRealName();
            }
            //下料用户
            Result<UserDto> downUser = authFeignClient.findByWorkNo(obj.getStaffDown());
            if (downUser != null && downUser.getData().getRealName() != null ){
                cutOperator = user.getRealName();
            }
            eapCutCraft.setCutUpOperator(cutUpOperator);
            eapCutCraft.setCutOperator(cutOperator);
        } else {
            EapCutRecord eapCutRecord = fromDto(eapCutRecordDto);
            eapCutRecord.setCreatedTime(new Date());
            eapCutRecordService.saveOrUpdate(eapCutRecord);
            BeanUtils.copyProperties(eapCutRecord, eapCutCraft, BeanUtil.getIgnoreProperties(eapCutRecord, "id", null));
            User user = null;
            String cutUpOperator = eapCutRecord.getStaffUp();//上料用户名
            String cutOperator = eapCutRecord.getStaffDown();//下料用户名
            //上料用户
            Result<UserDto> upUser = authFeignClient.findByWorkNo(eapCutRecord.getStaffUp());
            if (upUser != null && upUser.getData().getRealName() != null ){
                cutUpOperator = user.getRealName();
            }
            //下料用户
            Result<UserDto> downUser = authFeignClient.findByWorkNo(eapCutRecord.getStaffDown());
            if (downUser != null && downUser.getData().getRealName() != null ){
                cutOperator = user.getRealName();
            }
            eapCutCraft.setCutUpOperator(cutUpOperator);
            eapCutCraft.setCutOperator(cutOperator);

            try{
                //截断上料记录上传时自动过站
                if (eapCutRecord.getStartCut() != null) {
                    UnbindInfoDto unbindInfoDto = new UnbindInfoDto();
                    unbindInfoDto.setCrystalNo(eapCutRecord.getRoundSn());
                    unbindInfoDto.setCutNo(eapCutRecord.getEquipNo());
                    ybFeignClients.unbindInfo(unbindInfoDto);
                }
            }catch (Exception e){

            }
        }

        eapCutCraftService.saveEapCutCraftInfo(eapCutCraft,0);

        return ResultGenerator.success();
    }

    public EapCutRecord fromDto(EapCutRecordDto dto) {
        return modelMapper.map(dto, EapCutRecord.class);
    }
}
