package com.sw.mes.yb.accessory.validatedData;

import com.sw.mes.core.enums.EnumValues;
import com.sw.mes.yb.accessory.dto.EquipmentAccessoryDto;
import com.sw.mes.yb.accessory.entity.AccessoryApply;
import com.sw.mes.yb.accessory.entity.AccessoryReturn;
import com.sw.mes.yb.accessory.entity.EquipmentAccessory;
import com.sw.mes.yb.accessory.service.AccessoryApplyService;
import com.sw.mes.yb.accessory.service.AccessoryReturnService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author liuzemin
 * @date 2021/12/3 13:13
 * @description
 */
@Component
public class ValidateOperate {

    @Autowired
    private AccessoryReturnService accessoryReturnService;

    @Autowired
    private AccessoryApplyService accessoryApplyService;

    /**
     * 校验是否在出入库状态
     */
    public void validateListByPage (EquipmentAccessoryDto dto, EquipmentAccessory entity){
        AccessoryReturn aReturn = accessoryReturnService.findByAccessoryId(entity.getId().intValue()); //查询监控最近的退库请求
        if (aReturn != null){
            AccessoryApply aApply =
                    accessoryApplyService.findByApplyDateAndGoodsCode(aReturn.getApplyDate(), aReturn.getGoodsCode());//查询监控最近退库请求对应的要料请求

            if(aApply == null){
                if (EnumValues.AccessoryReturnStatus.NOTCONFIRMED.getKey().equals(aReturn.getState())
                        || EnumValues.AccessoryReturnStatus.CONFIRMED.getKey().equals(aReturn.getState())){
                    dto.setButtonStatus(0); //没申请要料，只申请了退库且未被处理
                }
            }
            if(aApply != null){
                if (!aApply.getGoodsCode().equals(aReturn.getGoodsCode())){
                    if (EnumValues.AccessoryReturnStatus.NOTCONFIRMED.getKey().equals(aReturn.getState())){
                        dto.setButtonStatus(0); //没申请要料，只申请了退库且未被处理
                    }
                }else { //else保证退库请求与要料请求是一对
                    if (EnumValues.PickInfoStatus.NOTCONFIRMED.getKey().equals(aApply.getState())){
                        dto.setButtonStatus(0); //要料申请且未被处理
                    }
                }
            }
        }
    }

    /**
     * 校验监控，如果正在退库要料中，无法辅料交换-对调-删除
     */
    public void validateInOrOut (EquipmentAccessory entity){
        AccessoryReturn aReturn = accessoryReturnService.findByAccessoryId(entity.getId().intValue()); //查询监控最近的退库请求
        if (aReturn != null){
            AccessoryApply aApply =
                    accessoryApplyService.findByApplyDateAndGoodsCode(aReturn.getApplyDate(), aReturn.getGoodsCode());//查询监控最近退库请求对应的要料请求

            if(aApply == null){
                if (EnumValues.AccessoryReturnStatus.NOTCONFIRMED.getKey().equals(aReturn.getState())
                        || EnumValues.AccessoryReturnStatus.CONFIRMED.getKey().equals(aReturn.getState())){
                    throw new RuntimeException("正在退库要料中，无法变动"); //没申请要料，只申请了退库且未被处理
                }
            }
            if(aApply != null){
                if (!aApply.getGoodsCode().equals(aReturn.getGoodsCode())){
                    if (EnumValues.AccessoryReturnStatus.NOTCONFIRMED.getKey().equals(aReturn.getState())){
                        throw new RuntimeException("正在退库要料中，无法变动"); //没申请要料，只申请了退库且未被处理
                    }
                }else { //else保证退库请求与要料请求是一对
                    if (EnumValues.PickInfoStatus.NOTCONFIRMED.getKey().equals(aApply.getState())){
                        throw new RuntimeException("正在退库要料中，无法变动"); //要料申请且未被处理
                    }
                }
            }
        }
    }
}
