/*
 * The Hefei JingTong RDC(Research and Development Centre) Group.
 * __________________
 *
 *    Copyright 2015-2019
 *    All Rights Reserved.
 *
 *    NOTICE:  All information contained herein is, and remains
 *    the property of JingTong Company and its suppliers,
 *    if any.
 */

package com.jwipc.job.handler;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jwipc.api.constant.RestCode;
import com.jwipc.api.dto.ApiResult;
import com.jwipc.job.constant.MaterialBatchMesClient;
import com.jwipc.job.dto.MaterialBatchQualifiedQueryDTO;
import com.jwipc.job.dto.MaterialBatchToMesDTO;
import com.jwipc.spe.entity.MaterialBatchQualified;
import com.jwipc.spe.service.IMaterialBatchQualifiedService;
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 org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.YearMonth;
import java.util.ArrayList;
import java.util.List;

/**
 * <p> </p>
 *
 * @author ZhangJie
 * @version 1.0
 * @since JDK 1.8
 */
@PersistJobDataAfterExecution
@DisallowConcurrentExecution
@Slf4j
@Component
public class MaterialBatchPullJob extends QuartzJobBean implements InterruptableJob {
    @Autowired
    private MaterialBatchMesClient materialBatchMesClient;
    @Autowired
    private IMaterialBatchQualifiedService materialBatchQualifiedService;

    @Override
    public void interrupt() throws UnableToInterruptJobException {


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info("拉取物料合格率任务开始");
        MaterialBatchQualifiedQueryDTO mes = new MaterialBatchQualifiedQueryDTO();
//        mes.setTime(LocalDate.now().plusMonths(-1L));
        mes.setTime(DateUtil.format(LocalDateTime.now().plusMonths(-1L), "yyyy-MM"));
        ApiResult result = materialBatchMesClient.pullDMaterialBatchQualified(mes);
        if (result.getCode().equals(RestCode.MES_SUCCESS.getCode())) {
            String s = DateUtil.format(LocalDateTime.now().plusMonths(-1L), "yyyy-MM") + "-01";
            materialBatchQualifiedService.remove(new LambdaQueryWrapper<MaterialBatchQualified>().eq(MaterialBatchQualified::getBatchTime, s));

            List<MaterialBatchToMesDTO> itItemItems = JSON.parseArray(JSON.toJSONString(result.getData()), MaterialBatchToMesDTO.class);
            List<MaterialBatchQualified> materialBatchQualifiedList = new ArrayList<>();
            for (MaterialBatchToMesDTO itItemItem : itItemItems) {
                final MaterialBatchQualified materialBatchQualified = new MaterialBatchQualified();
                materialBatchQualified.setSupplierCode(itItemItem.getSupplier());//供应商
                materialBatchQualified.setMaterialCode(itItemItem.getPartNo());//料号
                // 将years字段和months字段转换为整数
                int year = Integer.parseInt(itItemItem.getYears());
                int month = Integer.parseInt(itItemItem.getMonths());
                // 使用YearMonth类创建表示年份和月份的对象
                YearMonth yearMonth = YearMonth.of(year, month);
                // 根据YearMonth对象创建LocalDate对象，设置日期的天数为1
                LocalDate date = yearMonth.atDay(1);
                // 将得到的LocalDate对象设置到materialBatchQualified对象的batchTime字段中
                materialBatchQualified.setBatchTime(date);//年月
                //materialBatchQualified.setBatchTime(itItemItem.getYears());
                materialBatchQualified.setGetTotalBatch(Long.valueOf(itItemItem.getIncomingBatch()));//来料总批次
                materialBatchQualified.setQualifiedBatch(Long.valueOf(itItemItem.getQualifiedBatch()));//合格批次
                materialBatchQualified.setRejectionBatch(Long.valueOf(itItemItem.getREJECTBatch()));//判退批次
                materialBatchQualified.setExemptionBatch(Long.valueOf(itItemItem.getFreeBatch()));//免检批次
                materialBatchQualified.setLar(new BigDecimal(itItemItem.getLAR().replace("%", "")));//LAR
                materialBatchQualified.setCreateTime(LocalDateTime.now());
                materialBatchQualifiedList.add(materialBatchQualified);
//                materialBatchQualifiedService.save(materialBatchQualified);
            }
            materialBatchQualifiedService.saveBatch(materialBatchQualifiedList);
            log.info("拉取物料合格率任务结束",materialBatchQualifiedList);
        }
    }
}
