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


import com.sw.mes.base.factory.init.entity.Equipment;
import com.sw.mes.base.factory.init.service.EquipmentService;
import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.dto.MaterialOutDto;
import com.sw.mes.core.enums.EnumValues;
import com.sw.mes.yb.accessory.dao.AccessoryApplyFeedbackDao;
import com.sw.mes.yb.accessory.dto.AccessoryApplyFeedbackDto;
import com.sw.mes.yb.accessory.entity.*;
import com.sw.mes.yb.accessory.service.*;
import org.modelmapper.ModelMapper;
import org.modelmapper.convention.MatchingStrategies;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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


/**
* @Author
* @Description：
* @Date: Create in
* @Modified By:
*/
@Service
@Transactional
public class AccessoryApplyFeedbackServiceImpl extends BaseServiceImpl<AccessoryApplyFeedback> implements AccessoryApplyFeedbackService {
    AccessoryApplyFeedbackDao dao;
	@Autowired
    public void setDao(AccessoryApplyFeedbackDao dao) {
        super.setBaseDao(dao);
        this.dao=dao;
    }

    @Autowired
    private AccessoryApplyService accessoryApplyService;

	@Autowired
    private EquipmentAccessoryService equipmentAccessoryService;

	@Autowired
    private AccessoryReturnService accessoryReturnService;

	@Autowired
    private ModelMapper modelMapper;

	@Autowired
    private AccessoryApplyFeedbackService accessoryApplyFeedbackService;

    @Autowired
    private EquipmentService equipmentService;

    @Autowired
    private AccessoryMaterialOutService accessoryMaterialOutService;

    @Autowired
    private AccessoryMaterialService accessoryMaterialService;

    @Override
    @Transactional
    public void saveFeedback(AccessoryApplyFeedback accessoryApplyFeedback) {
        AccessoryApply accessoryApply = accessoryApplyService.findByPickNo(accessoryApplyFeedback.getOrderNum());
        if (accessoryApply == null){
            throw new RuntimeException("退料单号"+accessoryApplyFeedback.getOrderNum()+"有误！");
        }
        if (!EnumValues.PickInfoStatus.NOTCONFIRMED.getKey().equals(accessoryApply.getState())){
            throw new RuntimeException(accessoryApplyFeedback.getOrderNum()+"请不要重复处理！");
        }

        EquipmentAccessory equipmentAccessory = accessoryApply.getEquipmentAccessory();
        //AccessoryReturn accessoryReturn = accessoryReturnService.findByAccessoryId(equipmentAccessory.getId().intValue());
        if (accessoryApplyFeedback.getType() == 1){
//            if (accessoryReturn != null && EnumValues.AccessoryReturnStatus.REJECTED.getKey().equals(accessoryReturn.getState())){
//                throw new RuntimeException(accessoryReturn.getOrderNum()+"辅料退库申请已拒绝！请拒绝辅料申请！");
//            }
            //申请记录状态改为 CONFIRMED("CONFIRMED","接收") 在发料（AccessoryMaterialOut）
            accessoryApply.setAccessoryApplyFeedback(accessoryApplyFeedback);
        }else {
//            if (accessoryReturn != null && EnumValues.AccessoryReturnStatus.CONFIRMED.getKey().equals(accessoryReturn.getState())){
//                //辅料退库已通过，要料申请拒绝
//            }
//            if (accessoryReturn != null && EnumValues.AccessoryReturnStatus.NOTCONFIRMED.getKey().equals(accessoryReturn.getState())){
//                //辅料退库未处理，要料申请拒绝
//            }
//            if (accessoryReturn != null && EnumValues.AccessoryReturnStatus.REJECTED.getKey().equals(accessoryReturn.getState())){
//                equipmentAccessory.setButtonStatus(1); //退库拒绝,要料申请也拒绝,也开放删除-对调-更换按钮
//            }
            accessoryApply.setState(EnumValues.PickInfoStatus.REJECTED.getKey());
            accessoryApply.setStateDate(new Date());
            accessoryApply.setAccessoryApplyFeedback(accessoryApplyFeedback);
        }
        accessoryApplyFeedback.setId(null);
        saveOrUpdate(accessoryApplyFeedback);
        accessoryApplyService.saveOrUpdate(accessoryApply);
    }

    @Override
    @Transactional
    public void batchSaveFeedback(List<AccessoryApplyFeedbackDto> dtos) {
        for (AccessoryApplyFeedbackDto dto : dtos) {
            AccessoryApplyFeedback accessoryApplyFeedback = fromDto(dto);
            accessoryApplyFeedbackService.saveFeedback(accessoryApplyFeedback);

            if (dto.getType() == 1){
                MaterialOutDto out = dto.getOut();
                AccessoryMaterialOut accessoryMaterialOut = fromDto(out);

                Equipment equipment = equipmentService.findByCode(accessoryMaterialOut.getDeviceNo());
                if (equipment == null) {
                    throw new RuntimeException("该设备编号"+accessoryMaterialOut.getDeviceNo()+"在mes中不存在");
                }
                AccessoryMaterial accessoryMaterial = accessoryMaterialService.findByCodes(accessoryMaterialOut.getProductNo());
                if (accessoryMaterial == null) {
                    throw new RuntimeException("该辅料模型"+accessoryMaterialOut.getProductNo()+"在mes易耗品模型(拉晶车间)中不存在");
                }
                List<EquipmentAccessory> list = equipmentAccessoryService.findByGoodCodesWithBind(out.getUniqueCode());
                if (list != null && list.size() > 0) {
                    throw new RuntimeException("uniqueCode唯一编码"+out.getUniqueCode()+"已存在");
                }
                accessoryMaterialOut.setEquipment(equipment);
                accessoryMaterialOut.setMaterial(accessoryMaterial);

                accessoryMaterialOutService.saveMaterialOut(accessoryMaterialOut);
            }
        }
    }

    private AccessoryMaterialOut fromDto(MaterialOutDto dto) {
        //ModelMapper modelMapper = new ModelMapper();
        modelMapper.getConfiguration().setFullTypeMatchingRequired(true);
        modelMapper.getConfiguration().setMatchingStrategy(MatchingStrategies.STRICT);
        return modelMapper.map(dto, AccessoryMaterialOut.class);
    }

    public AccessoryApplyFeedback fromDto(AccessoryApplyFeedbackDto dto) {
        return modelMapper.map(dto, AccessoryApplyFeedback.class);
    }
}
