package com.casic.missiles.service.listeners.processor;

import com.alibaba.excel.util.DateUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.casic.missiles.dto.flowable.NotifyEventStatusDTO;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.enums.notifyevent.ActionStatusBeanEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.service.listeners.NotifyEventPostProcessor;
import com.casic.missiles.mapper.MeterFixedAssetsMapper;
import com.casic.missiles.mapper.equipment.EquipmentApplyEquipmentMapper;
import com.casic.missiles.mapper.equipment.EquipmentApplyMapper;
import com.casic.missiles.mapper.equipment.EquipmentStateChangeLogMapper;
import com.casic.missiles.model.equipment.EquipmentApply;
import com.casic.missiles.model.equipment.EquipmentApplyEquipment;
import com.casic.missiles.model.equipment.EquipmentFixedAssets;
import com.casic.missiles.model.equipment.EquipmentStateChangeLog;
import com.casic.missiles.utils.SpringContextUtil;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author xzz
 *      * 审批通过后修改设备管理状态
 *      * 新增状态变更记录表
 */
@Service(ActionStatusBeanEnum.MANAGING_BEAN_NAME)
public class EquipmentManagingEventPostProcessor implements NotifyEventPostProcessor {
    @Override
    public void doHandleNotifyEvent(NotifyEventStatusDTO notifyEventStatusDTO) {
        String state=notifyEventStatusDTO.getStatus();
        String id=notifyEventStatusDTO.getId();
        EquipmentApplyEquipmentMapper equipmentMapper = SpringContextUtil.getBean(EquipmentApplyEquipmentMapper.class);
        MeterFixedAssetsMapper meterFixedAssetsMapper = SpringContextUtil.getBean(MeterFixedAssetsMapper.class);
        EquipmentStateChangeLogMapper stateChangeLogMapper = SpringContextUtil.getBean(EquipmentStateChangeLogMapper.class);
        EquipmentApplyMapper equipmentApplyMapper = SpringContextUtil.getBean(EquipmentApplyMapper.class);
        // 获取申请关联的设备id
        QueryWrapper<EquipmentApplyEquipment> wrapper = new QueryWrapper<>();
        wrapper.eq("apply_id", id);
        List<EquipmentApplyEquipment> equipmentApplyEquipments = equipmentMapper.selectList(wrapper);
        // 获取设备idList
        List<String> equipmentIds = equipmentApplyEquipments.stream().map(EquipmentApplyEquipment::getEquipmentId).collect(Collectors.toList());
        UpdateWrapper<EquipmentFixedAssets> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", equipmentIds).set("manager_state", state);
        if (meterFixedAssetsMapper.update(null, updateWrapper) > 0) {
            UpdateWrapper<EquipmentStateChangeLog> logUpdateWrapper = new UpdateWrapper<>();
            // 修改状态变更记录结束时间
            logUpdateWrapper.in("equipment_id", equipmentIds).isNull("end_date").set("end_date", DateUtils.format(new Date()));
            stateChangeLogMapper.update(null, logUpdateWrapper);
            // 获取当前申请的信息
            EquipmentApply equipmentApply = equipmentApplyMapper.selectById(id);
            // 新增状态变更记录
            for (EquipmentApplyEquipment equipment : equipmentApplyEquipments) {
                EquipmentStateChangeLog stateChangeLog = new EquipmentStateChangeLog(Long.parseLong(id), Long.parseLong(equipment.getEquipmentId()), state, DateUtils.format(new Date()), null, equipmentApply.getApplyPerson());
                if (stateChangeLogMapper.insert(stateChangeLog) <= 0) {
                    throw new BusinessException(BusinessExceptionEnum.HANDLE_FAILED);
                }
            }
        } else {
            throw new BusinessException(BusinessExceptionEnum.HANDLE_FAILED);
        }
    }

}
