package com.jwipc.job.handler;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.jwipc.api.constant.RestCode;
import com.jwipc.constant.FreezeConst;
import com.jwipc.job.constant.JobSapClient;
import com.jwipc.job.dto.SupplierFreezeParamDTO;
import com.jwipc.job.dto.SupplierFreezeResultSapDTO;
import com.jwipc.supplier.constant.AuthenticationState;
import com.jwipc.supplier.entity.Supplier;
import com.jwipc.supplier.entity.SupplierPurchaseCategory;
import com.jwipc.supplier.service.ISupplierPurchaseCategoryService;
import com.jwipc.supplier.service.ISupplierService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.quartz.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
@Component
public class SupplierFreezeJob extends QuartzJobBean implements InterruptableJob {
    @Autowired
    private ISupplierPurchaseCategoryService supplierPurchaseCategoryService;
    @Autowired
    private ISupplierService supplierService;
    @Autowired
    private JobSapClient jobSapClient;

    @Override
    public void interrupt() throws UnableToInterruptJobException {

    }

    @Override
    protected void executeInternal(JobExecutionContext context){
        // TODO: 2024/3/25 这个地方是模拟测试 jiez

        final List<SupplierPurchaseCategory> purchaseCategories = supplierPurchaseCategoryService.list(
                new LambdaQueryWrapper<SupplierPurchaseCategory>().le(SupplierPurchaseCategory::getEffectiveTime,
                                LocalDateTime.now())
                        .eq(SupplierPurchaseCategory::getIsFreeze, FreezeConst.NORMAL.getCode())
                        .eq(SupplierPurchaseCategory::getState, AuthenticationState.SMALL_BATCH.getState()));

        if (CollUtil.isNotEmpty(purchaseCategories)) {
            final Set<Long> supplierIds = purchaseCategories.stream().map(SupplierPurchaseCategory::getSupplierId)
                    .collect(Collectors.toSet());
            final List<Supplier> suppliers = supplierService.listByIds(supplierIds);
            final Map<Long, String> supplierCache = suppliers.stream()
                    .collect(Collectors.toMap(Supplier::getId, Supplier::getSupplierErpCode));
            SupplierFreezeParamDTO supplierFreezeParamDTO = new SupplierFreezeParamDTO();
            final SupplierFreezeParamDTO.Item1 item1 = new SupplierFreezeParamDTO.Item1();
            item1.item = Lists.newArrayList();
            for (SupplierPurchaseCategory supplierPurchaseCategory : purchaseCategories) {
                final SupplierFreezeParamDTO.Item item = new SupplierFreezeParamDTO.Item();
                final String supplierCode = supplierCache.get(supplierPurchaseCategory.getSupplierId());
                item.setLIFNR(supplierCode);
                item.setZZRPL(supplierPurchaseCategory.getPurchaseCategoryCode());
                item.setZZRPLX(supplierPurchaseCategory.getPurchaseCategoryName());
                item.setZDEL("X");
                item1.item.add(item);
                supplierPurchaseCategory.setIsFreeze(FreezeConst.FREEZE.getCode());
                supplierPurchaseCategory.setFreezeRemark("小批量供应商到期冻结");
            }
            supplierFreezeParamDTO.setIT_DATA(item1);
            final SupplierFreezeResultSapDTO supplierFreezeResultSapDTO = jobSapClient.jobFreezeSupplier(
                    supplierFreezeParamDTO);
            log.info("{}", supplierFreezeResultSapDTO);
            if (supplierFreezeResultSapDTO.getES_RET().getCODE().equals(RestCode.SAP_SUCCESS.getCode())) {
                supplierPurchaseCategoryService.updateBatchById(purchaseCategories);
            }
        }

    }
}
