package com.bonus.canteen.core.allocation.api;

import cn.hutool.core.util.ObjectUtil;
import com.bonus.canteen.core.allocation.flow.enums.FlowDefinitionEum;
import com.bonus.canteen.core.allocation.flow.model.FlowDefinitionMaping;
import com.bonus.canteen.core.allocation.flow.service.FlowDefinitionAreaService;
import com.bonus.canteen.core.allocation.flow.service.FlowDefinitionMapingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Map;

@Service
public class FlowApi {
//   @Autowired
//   private FlowInstanceService flowInstanceService;
   @Autowired
   private FlowDefinitionMapingService flowDefinitionMapingService;
   @Autowired
   private FlowDefinitionAreaService flowDefinitionAreaService;

//   public Long creat(CreatInstanceDto content) {
//      return this.flowInstanceService.creat(content);
//   }
//
//   public Boolean next(NextInstanceDto content) {
//      return this.flowInstanceService.next(content);
//   }
//
//   public Boolean reject(NextInstanceDto content) {
//      return this.flowInstanceService.reject(content);
//   }
//
//   public Boolean cancel(CancelInstanceDto content) {
//      return this.flowInstanceService.cancel(content);
//   }
//
//   public PageVO<Instance> page(QueryInstanceDto content) {
//      return this.flowInstanceService.page(content);
//   }
//
//   public PageVO<Instance> pageByOperator(QueryInstanceDto content) {
//      return this.flowInstanceService.pageByOperator(content);
//   }
//
//   public Instance getInstance(Long instanceId) {
//      return this.flowInstanceService.getInstance(instanceId);
//   }

   public Long getByMappingIdAndDefinitionType(Long mappingId, FlowDefinitionEum flowDefinitionEum) {
      return this.getByMappingIdAndDefinitionType(mappingId, flowDefinitionEum, (Long)null);
   }

   public Long getByMappingIdAndDefinitionType(Long mappingId, FlowDefinitionEum flowDefinitionEum, Long areaId) {
      Long definitionId = this.flowDefinitionMapingService.getByMappingIdAndDefinitionType(mappingId, flowDefinitionEum);
      if (ObjectUtil.isEmpty(definitionId)) {
         FlowDefinitionMaping defaultByType = this.flowDefinitionMapingService.getDefaultByType(flowDefinitionEum.getType());
         if (ObjectUtil.isNotEmpty(defaultByType)) {
            definitionId = defaultByType.getDefinitionId();
         }
      }

      if (ObjectUtil.isNotEmpty(definitionId) && ObjectUtil.isNotEmpty(areaId)) {
         Map<Long, Long> areaMap = this.flowDefinitionAreaService.queryDefinitionArea(Collections.singletonList(definitionId));
         if (!ObjectUtil.equals(areaMap.get(definitionId), areaId)) {
            definitionId = null;
         }
      }

      return definitionId;
   }

//   public Map<Long, Boolean> getLimitMap(List<Long> instanceIds) {
//      return this.flowInstanceService.getLimitMap(instanceIds);
//   }
//
//   public Map<Long, Boolean> getLimitMap(FlowLimtMapDto flowLimtMapDto) {
//      return this.flowInstanceService.getLimitMap(flowLimtMapDto);
//   }
//
//   public List<Long> getInstanceIdsByState(InstanceFilter.State state, List<FlowDefinitionEum> types) {
//      return (List)(ObjectUtil.isEmpty(state) ? new ArrayList() : this.flowInstanceService.getInstanceIdsByState(state, types));
//   }
//
//   public List<Long> getInstanceIdsByState(List<FlowDefinitionEum> types) {
//      return this.flowInstanceService.getInstanceIdsByState(types);
//   }
//
//   public Map<String, Long> getHomePageFlowNotice() {
//      return this.flowInstanceService.getHomePageFlowNotice();
//   }
}
