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

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.mapper.MeterFixedAssetsMapper;
import com.casic.missiles.mapper.equipment.EquipmentStandardApplyEquipmentMapper;
import com.casic.missiles.mapper.equipment.EquipmentStandardApplyMapper;
import com.casic.missiles.mapper.equipment.EquipmentStandardEquipmentMapper;
import com.casic.missiles.model.equipment.EquipmentFixedAssets;
import com.casic.missiles.model.equipment.EquipmentStandardApply;
import com.casic.missiles.model.equipment.EquipmentStandardApplyEquipment;
import com.casic.missiles.model.equipment.EquipmentStandardEquipment;
import com.casic.missiles.service.listeners.NotifyEventPostProcessor;
import com.casic.missiles.utils.SpringContextUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 * @author xzz
 *
 *   审批通过后修改标准装置业务状态
 */
@Slf4j
@Service(ActionStatusBeanEnum.APPLY_BEAN_NAME)
public class EquipmentApplyEventPostProcessor implements NotifyEventPostProcessor {

    @Override
    public void doHandleNotifyEvent( NotifyEventStatusDTO notifyEventStatusDTO) {
        MeterFixedAssetsMapper meterFixedAssetsMapper = SpringContextUtil.getBean(MeterFixedAssetsMapper.class);
        EquipmentStandardApplyEquipmentMapper applyEquipmentMapper = SpringContextUtil.getBean(EquipmentStandardApplyEquipmentMapper.class);
        EquipmentStandardApplyMapper applyMapper = SpringContextUtil.getBean(EquipmentStandardApplyMapper.class);
        EquipmentStandardEquipmentMapper equipmentMapper = SpringContextUtil.getBean(EquipmentStandardEquipmentMapper.class);
        //通过申请id查询出标准装置id
        EquipmentStandardApply equipmentStandardApply = applyMapper.selectById(notifyEventStatusDTO.getId());
        //通过申请id查询出申请设备列表
        QueryWrapper<EquipmentStandardApplyEquipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("standard_apply_id", notifyEventStatusDTO.getId());
        List<EquipmentStandardApplyEquipment> equipmentStandardApplyEquipments = applyEquipmentMapper.selectList(queryWrapper);
        List<Long> equipmentIds = equipmentStandardApplyEquipments.stream().map(EquipmentStandardApplyEquipment::getEquipmentId).collect(Collectors.toList());
        //修改对应关联设备状态
        UpdateWrapper<EquipmentFixedAssets> updateWrapper = new UpdateWrapper<>();
        updateWrapper.in("id", equipmentIds).set("manager_state", notifyEventStatusDTO.getStatus());
        //修改标准装置状态
        EquipmentStandardEquipment equipmentStandardEquipment = new EquipmentStandardEquipment();
        equipmentStandardEquipment.setId(Long.valueOf(equipmentStandardApply.getStandardEquipmentId()));
        equipmentStandardEquipment.setManagerState(notifyEventStatusDTO.getStatus());
        if (meterFixedAssetsMapper.update(null, updateWrapper) <= 0 || equipmentMapper.updateById(equipmentStandardEquipment) <= 0) {
            throw new BusinessException(BusinessExceptionEnum.HANDLE_FAILED);
        }
    }

}
