package com.bonus.canteen.core.drp.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.bonus.canteen.core.allocation.alloc.spare.AllocDrpModel;
import com.bonus.canteen.core.common.enums.MetadataModelTypeEnum;
import com.bonus.canteen.core.allocation.api.AllocMetadataApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Component
public class DrpMetadataService {
   @Autowired
   @Lazy
   private AllocMetadataApi allocMetadataApi;
//   @Autowired
//   @Lazy
//   private AllocCanteenApi allocCanteenApi;
//   @Autowired
//   @Lazy
//   private XxlJobApi xxlJobApi;

   public AllocDrpModel queryMetadata(Long areaId) {
      AllocDrpModel allocDrpModel = (AllocDrpModel)this.allocMetadataApi.queryMetadataModelByAreaId(MetadataModelTypeEnum.DRP
              , new AllocDrpModel(), areaId);
      allocDrpModel.setAreaId(areaId);
      return allocDrpModel;
   }

//   public AllocDrpModel queryMetadataByCanteenId(Long canteenId) {
//      AllocCanteen allocCanteen = this.allocCanteenApi.getAllocCanteen(canteenId);
//      if (ObjectUtil.isEmpty(allocCanteen)) {
//         throw new LeException("没有查询到食堂信息");
//      } else {
//         AllocDrpModel allocDrpModel = (AllocDrpModel)this.allocMetadataApi.queryMetadataModelByAreaId(MetadataModelTypeEnum.DRP, new AllocDrpModel(), allocCanteen.getAreaId());
//         allocDrpModel.setAreaId(allocCanteen.getAreaId());
//         return allocDrpModel;
//      }
//   }
//
//   public void saveMetadata(AllocDrpModel content) {
//      this.allocMetadataApi.saveMetadataModelByAreaId(MetadataModelTypeEnum.DRP, content, content.getAreaId());
//   }
//
//   public String querySupplierScorePeriod(Long areaId) {
//      return this.allocMetadataApi.queryMetadataValueByAreaId(MetadataModelTypeEnum.DRP_SUPPLIER_SCORE, areaId, "supplierScorePeriod");
//   }
//
//   @Transactional(
//      rollbackFor = {Exception.class}
//   )
//   public void saveSupplierScorePeriod(DrpSupplierScorePeriodDTO dto) {
//      List<AllocMetadata> payMetadataList = new ArrayList();
//      AllocMetadata payMetadata = new AllocMetadata(Id.next(), MetadataModelTypeEnum.DRP_SUPPLIER_SCORE, "supplierScorePeriod", dto.getMetadata(), dto.getAreaId());
//      payMetadataList.add(payMetadata);
//      this.allocMetadataApi.saveList(payMetadataList);
//      XxlJobInfo xxlJobInfo = new XxlJobInfo();
//      xxlJobInfo.setId(50001);
//      xxlJobInfo.setJobGroup(2);
//      xxlJobInfo.setJobDesc("出入库-供应商自动周期评分");
//      xxlJobInfo.setAuthor("郭亚鹏");
//      xxlJobInfo.setScheduleType("CRON");
//      xxlJobInfo.setExecutorRouteStrategy("RANDOM");
//      xxlJobInfo.setExecutorHandler("drpAutoSupplierScore");
//      xxlJobInfo.setExecutorBlockStrategy("SERIAL_EXECUTION");
//      xxlJobInfo.setGlueType("BEAN");
//      xxlJobInfo.setMisfireStrategy("DO_NOTHING");
//      Integer supplierScorePeriod = Integer.valueOf(dto.getMetadata());
//      if (ObjectUtil.equal(supplierScorePeriod, DrpSupplierScorePeriodEnum.MONTH.getKey())) {
//         xxlJobInfo.setScheduleConf("0 30 23 L * ?");
//      } else if (ObjectUtil.equal(supplierScorePeriod, DrpSupplierScorePeriodEnum.QUARTER.getKey())) {
//         xxlJobInfo.setScheduleConf("0 30 23 L 3,6,9,12 ?");
//      } else if (ObjectUtil.equal(supplierScorePeriod, DrpSupplierScorePeriodEnum.HALF_YEAR.getKey())) {
//         xxlJobInfo.setScheduleConf("0 30 23 L 6,12 ?");
//      } else if (ObjectUtil.equal(supplierScorePeriod, DrpSupplierScorePeriodEnum.YEAR.getKey())) {
//         xxlJobInfo.setScheduleConf("0 30 23 L 12 ?");
//      }
//
//      this.xxlJobApi.update(xxlJobInfo);
//   }
}
