package com.rongwei.bscommon.sys.service.impl;

import com.rongwei.bscommon.sys.service.*;
import com.rongwei.bscommon.sys.utils.ExceptionUtils;
import com.rongwei.bsentity.domain.AspCheckItemsAreaDo;
import com.rongwei.bsentity.dto.EquipmentDisposeDTO;
import com.rongwei.rwcommon.base.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author shangmi
 * @title EquipmentArchivesServiceImpl
 * @date 2024/1/16 14:19
 * @description 设备档案
 */
@Service
public class EquipmentArchivesServiceImpl implements EquipmentArchivesService {

    private final String[] HANDLING_TYPE = {"10","20"};

    private final String[] OBJECT_TYPE = {"1","3"};

    @Autowired
    private AspCheckItemsService aspCheckItemsService;

    @Autowired
    private EquFrockListService equFrockListService;

    @Autowired
    private EquMaintenanceScheduleService maintenanceScheduleService;

    @Autowired
    private EquLubricationPlanService lubricationPlanService;

    @Autowired
    private AspCheckItemsAreaService checkItemsAreaService;

    @Autowired
    private EquipmentSendNotifyService sendNotifyService;

    /**
     * 更新设备库
     *
     * @param equipmentDisposeDTO
     * @return {@link R}
     * @date 2024/1/16 17:43
     * @author shangmi
     *
     */

    @Override
    public R updateEquipmentData(EquipmentDisposeDTO equipmentDisposeDTO) {
        Date newDate = new Date();
        //处置状态  报废/移交
        boolean isState = HANDLING_TYPE[0].equals(equipmentDisposeDTO.getState());
        //对象类型  设备/工装
        boolean isObject = OBJECT_TYPE[0].equals(equipmentDisposeDTO.getObjectType());
        if (isState){
            //更新停用状态
            String equipmentId = equipmentDisposeDTO.getEquipmentId();
            if (isObject) {
                aspCheckItemsService.updateStateById(equipmentId,newDate);
                lubricationPlanService.updateStateByEquId(equipmentId);

                //设备报废审批通过生成一条提醒任务
                try {
                    sendNotifyService.equScrapAuditNotify(equipmentDisposeDTO);
                }catch (Exception e){
                    ExceptionUtils.printExceptionDetail(e,"发送设备报废审批通过提醒异常");
                }
            } else {
                equFrockListService.updateStateById(equipmentId,newDate);
            }
            maintenanceScheduleService.updateStateByEquId(equipmentId);

        }else {
            //修改组织结构
            if (isObject) {
                AspCheckItemsAreaDo checkItemsArea = checkItemsAreaService.getByDeptName(equipmentDisposeDTO.getRollInPlantId(), equipmentDisposeDTO.getRollInDept());
                aspCheckItemsService.updateSiteById(equipmentDisposeDTO,checkItemsArea);
            } else {
                equFrockListService.updateSiteById(equipmentDisposeDTO);
            }
        }
        return R.ok();
    }
}
