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

import cn.hutool.core.util.ObjectUtil;
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.model.equipment.EquipmentFixedAssets;
import com.casic.missiles.service.equipment.IEquipmentStandardApplyEquipmentService;
import com.casic.missiles.service.equipment.IEquipmentStandardSupportEquipmentService;
import com.casic.missiles.service.listeners.NotifyEventPostProcessor;
import com.casic.missiles.mapper.equipment.EquipmentStandardApplyEquipmentMapper;
import com.casic.missiles.mapper.equipment.EquipmentStandardApplyMapper;
import com.casic.missiles.mapper.equipment.EquipmentStandardSupportMapper;
import com.casic.missiles.model.equipment.EquipmentStandardApply;
import com.casic.missiles.model.equipment.EquipmentStandardApplyEquipment;
import com.casic.missiles.model.equipment.EquipmentStandardSupportEquipment;
import com.casic.missiles.utils.SpringContextUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author  xzz
 *      * 审批通过后修改标准装置配套设备状态
 */
@Service(ActionStatusBeanEnum.SUPPORT_BEAN_NAME)
public class EquipmentSupportEventPostProcessor implements NotifyEventPostProcessor {

    @Resource
    private IEquipmentStandardSupportEquipmentService supportEquipmentService;

    @Override
    @Transactional
    public void doHandleNotifyEvent(NotifyEventStatusDTO notifyEventStatusDTO) {
        String id = notifyEventStatusDTO.getId();
        EquipmentStandardApplyMapper standardApplyMapper = SpringContextUtil.getBean(EquipmentStandardApplyMapper.class);
        EquipmentStandardApplyEquipmentMapper applyEquipmentMapper = SpringContextUtil.getBean(EquipmentStandardApplyEquipmentMapper.class);
        List<EquipmentStandardSupportEquipment> supportEquipments = new ArrayList<>();
        // 通过申请id查询申请信息
        EquipmentStandardApply standardApply = standardApplyMapper.selectById(id);
        // 通过申请id查询出申请设备列表
        QueryWrapper<EquipmentStandardApplyEquipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("standard_apply_id", id);
        List<EquipmentStandardApplyEquipment> equipmentStandardApplyEquipments = applyEquipmentMapper.selectList(queryWrapper);
        for (EquipmentStandardApplyEquipment equipment : equipmentStandardApplyEquipments) {
            EquipmentStandardSupportEquipment supportEquipment = new EquipmentStandardSupportEquipment();
            supportEquipment.setId(equipment.getSupportEquipmentId());
            supportEquipment.setEquipmentId(equipment.getEquipmentId());
            supportEquipment.setStandardEquipmentId(Long.valueOf(standardApply.getStandardEquipmentId()));
            supportEquipments.add(supportEquipment);
        }

        if(supportEquipmentService.saveOrUpdateBatch(supportEquipments)){
            //删除替换前的标准配套设备
            int deleteFlag = supportEquipmentService.deleteByList(equipmentStandardApplyEquipments, standardApply.getStandardEquipmentId());
            //将替换成的非标准配套设备的设备置为标准配套设备
            List<Long> equipmentIds = equipmentStandardApplyEquipments.stream().map(EquipmentStandardApplyEquipment::getEquipmentId).collect(Collectors.toList());
            MeterFixedAssetsMapper meterFixedAssetsMapper = SpringContextUtil.getBean(MeterFixedAssetsMapper.class);
            // 修改对应设备为标准配套设备
            UpdateWrapper<EquipmentFixedAssets> updateWrapper = new UpdateWrapper<>();
            updateWrapper.in("id", equipmentIds).set("is_standard_support_equipment", 1);
            if (deleteFlag <= 0 || meterFixedAssetsMapper.update(null, updateWrapper) <= 0) {
                throw new BusinessException(BusinessExceptionEnum.HANDLE_FAILED);
            }
        } else{
            throw new BusinessException(BusinessExceptionEnum.HANDLE_FAILED);
        }
    }
}
