package com.bonus.canteen.core.allocation.flow.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bonus.common.houqin.constant.LeConstants;
import com.bonus.canteen.core.allocation.flow.enums.FlowDefinitionEum;
import com.bonus.canteen.core.allocation.flow.mapper.FlowDefinitionMapingMapper;
import com.bonus.canteen.core.allocation.flow.model.FlowDefinitionMaping;
import com.bonus.canteen.core.allocation.flow.service.FlowDefinitionMapingService;
import org.springframework.stereotype.Service;

import java.lang.invoke.SerializedLambda;
import java.util.ArrayList;
import java.util.List;

@Service
public class FlowDefinitionMapingServiceImpl extends ServiceImpl<FlowDefinitionMapingMapper, FlowDefinitionMaping> implements FlowDefinitionMapingService {
   public void deleteByDefinition(Long definitionId) {
      this.update(Wrappers.lambdaUpdate(FlowDefinitionMaping.class).set(FlowDefinitionMaping::getDelFlag, LeConstants.COMMON_YES)
              .eq(FlowDefinitionMaping::getDefinitionId, definitionId));
   }

   public List<FlowDefinitionMaping> getByDefinitionIds(List<Long> definitionIds) {
      return (CollUtil.isEmpty(definitionIds) ? new ArrayList() : this.list(Wrappers.lambdaQuery(FlowDefinitionMaping.class)
              .eq(FlowDefinitionMaping::getDelFlag, LeConstants.COMMON_NO).in(FlowDefinitionMaping::getDefinitionId, definitionIds)));
   }

   public List<FlowDefinitionMaping> getByMappingIdAndType(Long mappingId, String mappingIdType) {
      return this.list(Wrappers.lambdaQuery(FlowDefinitionMaping.class).eq(FlowDefinitionMaping::getDelFlag, LeConstants.COMMON_NO)
              .eq(FlowDefinitionMaping::getMappingIdType, mappingIdType).eq(FlowDefinitionMaping::getMappingId, mappingId));
   }

   public List<FlowDefinitionMaping> getByMappingIdAndDefinitionType(Long mappingId, String definitionType) {
      return this.list(Wrappers.lambdaQuery(FlowDefinitionMaping.class).eq(FlowDefinitionMaping::getDelFlag, LeConstants.COMMON_NO).eq(FlowDefinitionMaping::getStatus, LeConstants.COMMON_YES).eq(FlowDefinitionMaping::getDefinitionType, definitionType).eq(FlowDefinitionMaping::getMappingId, mappingId));
   }

   public void changeStatusByDefinitionId(Long definitionId, Integer status) {
      this.update(Wrappers.lambdaUpdate(FlowDefinitionMaping.class).set(FlowDefinitionMaping::getStatus, status)
              .eq(FlowDefinitionMaping::getDelFlag, LeConstants.COMMON_NO).eq(FlowDefinitionMaping::getDefinitionId, definitionId));
   }

   public Long getByMappingIdAndDefinitionType(Long mappingId, FlowDefinitionEum flowDefinitionEum) {
      List<FlowDefinitionMaping> byMappingIdAndDefinitionType = this.getByMappingIdAndDefinitionType(mappingId, flowDefinitionEum.getType());
      return CollUtil.isEmpty(byMappingIdAndDefinitionType) ? null : byMappingIdAndDefinitionType.get(0).getDefinitionId();
   }

   public List<FlowDefinitionMaping> getByMappingIdsAndDefinitionType(List<Long> mappingIds, String definitionType) {
      return this.list(Wrappers.lambdaQuery(FlowDefinitionMaping.class).eq(FlowDefinitionMaping::getDelFlag, LeConstants.COMMON_NO)
              .eq(FlowDefinitionMaping::getStatus, LeConstants.COMMON_YES).eq(FlowDefinitionMaping::getDefinitionType, definitionType)
              .in(FlowDefinitionMaping::getMappingId, mappingIds));
   }

   public List<FlowDefinitionMaping> getNotIdByMappingIdsAndDefinitionType(Long definitionId, List<Long> mappingIds, String definitionType) {
      return this.list(Wrappers
              .lambdaQuery(FlowDefinitionMaping.class).eq(FlowDefinitionMaping::getDelFlag, LeConstants.COMMON_NO)
              .eq(FlowDefinitionMaping::getStatus, LeConstants.COMMON_YES).eq(FlowDefinitionMaping::getDefinitionType, definitionType)
              .ne(FlowDefinitionMaping::getDefinitionId, definitionId).in(FlowDefinitionMaping::getMappingId, mappingIds));
   }

   public FlowDefinitionMaping getDefaultByType(String definitionType) {
      return this.getOne(Wrappers
              .lambdaQuery(FlowDefinitionMaping.class).eq(FlowDefinitionMaping::getDelFlag, LeConstants.COMMON_NO)
              .eq(FlowDefinitionMaping::getStatus, LeConstants.COMMON_YES).isNull(FlowDefinitionMaping::getMappingId)
              .eq(FlowDefinitionMaping::getDefinitionType, definitionType));
   }
}
