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

import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.casic.missiles.dto.business.outsource.OrderSampleDTO;
import com.casic.missiles.dto.flowable.NotifyEventStatusDTO;
import com.casic.missiles.enums.BusinessExceptionEnum;
import com.casic.missiles.enums.business.MeasureTypeEnum;
import com.casic.missiles.enums.notifyevent.ActionStatusBeanEnum;
import com.casic.missiles.exception.BusinessException;
import com.casic.missiles.mapper.business.BusinessOrderSampleRelationMapper;
import com.casic.missiles.mapper.business.BusinessOutsourceProjectSampleRelationMapper;
import com.casic.missiles.mapper.customer.CustomerSampleMapper;
import com.casic.missiles.model.business.BusinessOutsourceProjectSampleRelation;
import com.casic.missiles.model.customer.CustomerSampleInfo;
import com.casic.missiles.service.listeners.NotifyEventPostProcessor;
import com.casic.missiles.utils.ConvertUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

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

/**
 * @Description: 分包项目申请审批通过处理
 *               分包项目申请通过后更新样品检定方式为外包（关联表和样品库表都更新）
 * @Author: wangpeng
 * @Date: 2023/3/29 14:28
 */
@Service(ActionStatusBeanEnum.OUTSOURCE_PROJECT_APPLY_BEAN_NAME)
public class OutsourceProjectApplyEventPostProcessor implements NotifyEventPostProcessor {
    @Autowired
    private BusinessOutsourceProjectSampleRelationMapper projectSampleRelationMapper;
    @Autowired
    private BusinessOrderSampleRelationMapper orderSampleRelationMapper;
    @Autowired
    private CustomerSampleMapper sampleMapper;

    @Override
    public void doHandleNotifyEvent(NotifyEventStatusDTO notifyEventStatusDTO) {
        Long id = Long.valueOf(notifyEventStatusDTO.getId());
        Assert.isFalse(Objects.isNull(id), () -> {
            throw new BusinessException(BusinessExceptionEnum.ID_NULL);
        });
        //查询分包项目&样品关联表得到委托书id、样品id，按委托书、样品id更新委托书&样品关联表和样品表检定方式为外包
        QueryWrapper<BusinessOutsourceProjectSampleRelation> wrapper = new QueryWrapper<>();
        wrapper.eq("project_id", id);
        List<BusinessOutsourceProjectSampleRelation> outsourceProjectSampleRelations = projectSampleRelationMapper.selectList(wrapper);
        if(CollectionUtils.isEmpty(outsourceProjectSampleRelations)){
            return;
        }
        List<OrderSampleDTO> orderSampleDTOS = ConvertUtils.sourceToTarget(outsourceProjectSampleRelations, OrderSampleDTO.class);
        int updateBatchFlag = orderSampleRelationMapper.updateTypeBatchByOrderAndSample(orderSampleDTOS);
        CustomerSampleInfo sampleInfo = new CustomerSampleInfo();
        sampleInfo.setMeasureType(MeasureTypeEnum.OTHERS_UNDERTAKE);
        QueryWrapper<CustomerSampleInfo> updateWrapper = new QueryWrapper<>();
        List<Long> sampleIds = outsourceProjectSampleRelations.stream().map(BusinessOutsourceProjectSampleRelation::getSampleId).collect(Collectors.toList());
        updateWrapper.in("id", sampleIds);
        int updateFlag = sampleMapper.update(sampleInfo, updateWrapper);
        if (updateBatchFlag <= 0 || updateFlag <= 0) {
            throw new BusinessException(BusinessExceptionEnum.HANDLE_FAILED);
        }
    }

}
