package com.sw.mes.fb.accessory.domain;

import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.base.factory.material.entity.MaterialModel;
import com.sw.mes.base.factory.product.entity.FbAccessoryMaterial;
import com.sw.mes.fb.accessory.dto.EquipmentClassTeam;
import com.sw.mes.fb.accessory.dto.FbEquipmentAccessoryUpdateDto;
import com.sw.mes.fb.accessory.entity.FbEquipmentAccessory;
import lombok.Data;

import java.util.Date;
import java.util.Optional;

/**
 * @author liuzemin
 * @date 2022/7/5 14:02
 * @description
 */
@Data
public class OrderUpdate {

    /**
     * 设备编号
     */
    String deviceNo;

    /**
     * 设备位置
     */
    String deviceSeat;

    /**
     * 设备类型
     */
    String deviceType;

    /**
     * 更换前
     */
    OrderUpdateBefore before;

    /**
     * 更换后
     */
    OrderUpdateAfter after;

    /**
     * 换上时间
     */
    Date upDate;

    /**
     * 换上人（上个更换人）
     */
    String upUser;

    /**
     * 换上人id
     */
    Integer upUserId;

    /**
     * 换上人班次
     */
    String upClassNo;

    /**
     * 换上人班组
     */
    String upTeamGroup;

    /**
     * 换下时间
     */
    Date downDate;

    /**
     * 换下人-当前更换人
     */
    String downUser;

    /**
     * 换下人id
     */
    Integer downUserId;

    /**
     * 换下人班次
     */
    String downClassNo;

    /**
     * 换下人班组
     */
    String downTeamGroup;


    /**
     * 更换原因
     */
    String remark;

    /**
     * 创建时间
     */
    Date createDate;

    public OrderUpdate() {
    }

    /**
     * @param equipmentAccessory 更换前的辅料监控
     * @param accessoryMaterial  将要更换的辅料
     * @param updateDto          将要更换的更换时间、条码、已使用时间、厂家等等。。。
     * @param equipmentClassTeam 换上、换下班组
     */
    public OrderUpdate(FbEquipmentAccessory equipmentAccessory, FbAccessoryMaterial accessoryMaterial, FbEquipmentAccessoryUpdateDto updateDto, EquipmentClassTeam equipmentClassTeam) {
        this.deviceNo = equipmentAccessory.getEquipment().getCode();
        this.deviceSeat = equipmentAccessory.getDeviceSeat();
        this.deviceType = equipmentAccessory.getEquipment().getDevice().getName();
        this.upDate = equipmentAccessory.getOperateDate();
        this.upUser = equipmentAccessory.getOperateUser();
        this.upUserId = equipmentAccessory.getOperateUserId();
        this.upClassNo = equipmentClassTeam.getUpClassNo();
        this.upTeamGroup = equipmentClassTeam.getUpTeamGroup();
        this.downDate = updateDto.getOperateTime();
        this.downUser = BaseContextHandler.getUsername();
        this.downUserId = BaseContextHandler.getUserID();
        this.downClassNo = equipmentClassTeam.getDownClassNo();
        this.downTeamGroup = equipmentClassTeam.getDownTeamGroup();
        this.remark = updateDto.getRemark();
        this.createDate = new Date();

        OrderUpdateBefore orderUpdateBefore = new OrderUpdateBefore();
        orderUpdateBefore.setMaterialId(Optional.ofNullable(equipmentAccessory).map(FbEquipmentAccessory::getMaterial).map(FbAccessoryMaterial::getId).orElse(null));
        orderUpdateBefore.setFactory(equipmentAccessory.getFactory());
        orderUpdateBefore.setGoodsCode(equipmentAccessory.getGoodsCode());
        orderUpdateBefore.setMaterialCode(Optional.ofNullable(equipmentAccessory).map(FbEquipmentAccessory::getMaterial).map(FbAccessoryMaterial::getCodes).orElse(null));
        orderUpdateBefore.setMaterialName(Optional.ofNullable(equipmentAccessory).map(FbEquipmentAccessory::getMaterial).map(FbAccessoryMaterial::getAccessoryTypeName).orElse(null));
        orderUpdateBefore.setMaterialType(Optional.ofNullable(equipmentAccessory).map(FbEquipmentAccessory::getMaterial).map(FbAccessoryMaterial::getAccessoryTypeValue).orElse(null));
        orderUpdateBefore.setSpecification(Optional.ofNullable(equipmentAccessory).map(FbEquipmentAccessory::getMaterial).map(FbAccessoryMaterial::getNorms).orElse(null));
        Double useLifeBefore = Optional.ofNullable(equipmentAccessory).map(FbEquipmentAccessory::getMaterial).map(FbAccessoryMaterial::getUseLife).orElse(null);
        Double useTimeBefore = equipmentAccessory.getUseTime();
        if (useLifeBefore != null && useTimeBefore != null){
           orderUpdateBefore.setRemainLife(useLifeBefore - useTimeBefore);
        }
        orderUpdateBefore.setUseLife(useLifeBefore);
        orderUpdateBefore.setUseTime(useTimeBefore);
        this.before = orderUpdateBefore;

        OrderUpdateAfter orderUpdateAfter = new OrderUpdateAfter();
        orderUpdateAfter.setMaterialId(accessoryMaterial.getId());
        orderUpdateAfter.setFactory(updateDto.getFactory());
        orderUpdateAfter.setGoodsCode(updateDto.getGoodsCode());
        orderUpdateAfter.setMaterialCode(accessoryMaterial.getCodes());
        orderUpdateAfter.setMaterialName(Optional.ofNullable(accessoryMaterial).map(FbAccessoryMaterial::getAccessoryTypeName).orElse(null));
        orderUpdateAfter.setMaterialType(Optional.ofNullable(accessoryMaterial).map(FbAccessoryMaterial::getAccessoryTypeValue).orElse(null));
        orderUpdateAfter.setSpecification(Optional.ofNullable(accessoryMaterial).map(FbAccessoryMaterial::getNorms).orElse(null));
        Double useLifeAfter = Optional.ofNullable(accessoryMaterial).map(FbAccessoryMaterial::getUseLife).orElse(null);
        Double useTimeAfter = updateDto.getUseTime();
        if (useLifeAfter != null && useTimeAfter != null){
            orderUpdateAfter.setRemainLife(useLifeAfter - useTimeAfter);
        }
        orderUpdateAfter.setUseLife(useLifeAfter);
        orderUpdateAfter.setUseTime(useTimeAfter);
        this.after = orderUpdateAfter;
    }
}
