package com.sw.mes.itemclean.clean.jobhandler;



import com.sw.mes.core.api.system.YbFeignClients;
import com.sw.mes.core.dto.DictionaryItemDto;
import com.sw.mes.core.response.Result;
import com.sw.mes.itemclean.clean.domain.MaterialApplyOrder;
import com.sw.mes.itemclean.clean.domain.ReportErpXlInputRecord;
import com.sw.mes.itemclean.clean.domain.ReportErpXlInputRecordDetail;
import com.sw.mes.itemclean.clean.service.MaterialApplyOrderService;
import com.sw.mes.itemclean.clean.service.ReportErpXlInputRecordDetailService;
import com.sw.mes.itemclean.clean.service.ReportErpXlInputRecordService;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author: JS111816
 * @Description:
 * @Date: 2022/11/14
 * @Modified By:
 **/
@Component
@Slf4j
public class PullErpDataHandler {

    @Autowired
    private MaterialApplyOrderService materialApplyOrderService;

    @Autowired
    private ReportErpXlInputRecordService reportErpXlInputRecordService;

    @Autowired
    private ReportErpXlInputRecordDetailService reportErpXlInputRecordDetailService;

    @Autowired
    private YbFeignClients ybFeignClients;



    @XxlJob("pullErpDataHandler")
    public void pullErpDataHandler() {
        try {
            List<MaterialApplyOrder> list = materialApplyOrderService.pullMaterialData();
            //组装数据
            Map<String,List<MaterialApplyOrder>>  map = list.stream().collect(Collectors.groupingBy(dto->dto.getAufnr()));
            Set set = map.keySet();
            Iterator iterator = set.iterator();
            //合并后插入新建的主表
            //组装批次号
            //插入新建的子表
            while (iterator.hasNext()) {
                String key = iterator.next().toString();
                List<MaterialApplyOrder>  materialApplyOrders = map.get(key);
                Map<String,List<MaterialApplyOrder>>  map1 = materialApplyOrders.stream().collect(Collectors.groupingBy(dto->dto.getClanProductBatchNo()));
                Set set1 = map1.keySet();
                Iterator iterator1 = set1.iterator();
                while (iterator1.hasNext()) {
                    String key1 = iterator1.next().toString();
                    List<MaterialApplyOrder>  materialApplyOrders1 = map1.get(key1);
                    MaterialApplyOrder temp = materialApplyOrders1.get(0);
                    ReportErpXlInputRecord reportErpXlInputRecord = new ReportErpXlInputRecord();
                    reportErpXlInputRecord.setCode(temp.getAufnr());
                    reportErpXlInputRecord.setOrderNo(temp.getClanProductBatchNo());
                    Result<DictionaryItemDto> result = ybFeignClients.findByThemeCodeAndItemName("WarehouseCode","清洗车间");
                    String warehouseCode = Optional.ofNullable(result).map(Result::getData).map(DictionaryItemDto::getValue).orElse("");
                    reportErpXlInputRecord.setWarehouseCode(warehouseCode);
                    Result<DictionaryItemDto> result1 = ybFeignClients.findByThemeCodeAndItemName("departmentCode","清洗车间");
                    Result<DictionaryItemDto> result2 = ybFeignClients.findByThemeCodeAndItemName("receiveCode","清洗领用");
                    String receiveCode = Optional.ofNullable(result1).map(Result::getData).map(DictionaryItemDto::getValue).orElse("");
                    reportErpXlInputRecord.setReceiveCode(receiveCode);
                    String departmentCode = Optional.ofNullable(result2).map(Result::getData).map(DictionaryItemDto::getValue).orElse("");
                    reportErpXlInputRecord.setDepartmentCode(departmentCode);
//                reportErpXlInputRecord.setInventoryCode();
                    reportErpXlInputRecord.setCost(BigDecimal.ZERO);
                    //reportErpXlInputRecord.setSerial();
                    reportErpXlInputRecord.setMaterialCode(temp.getMaterialCode());
                    reportErpXlInputRecord.setMaterialName(temp.getMaterialName());
                    reportErpXlInputRecord.setCreateTime(new Date());
                    reportErpXlInputRecord.setPushed(0);
                    reportErpXlInputRecord.setType(3);
                    reportErpXlInputRecordService.saveOrUpdate(reportErpXlInputRecord);
                    BigDecimal weight =  BigDecimal.ZERO;
                    List<ReportErpXlInputRecordDetail> reportErpXlInputRecordDetails = new ArrayList<>();
                    for(MaterialApplyOrder order:materialApplyOrders1){
                        ReportErpXlInputRecordDetail detail = new ReportErpXlInputRecordDetail();
                        detail.setRid(reportErpXlInputRecord.getId());
                        detail.setBid(order.getId().longValue());
                        detail.setWeight(order.getWeight());
                        detail.setBatchNo(order.getProductBatchNo());
                        detail.setMaterialName(order.getMaterialName());
                        detail.setBoxNo(order.getMaterialBoxNo());
                        detail.setPushed(0);
                        detail.setMaterialCode(order.getMaterialCode());
                        BigDecimal weight1 = order.getWeight();
                        weight = weight.add(weight1);
                        reportErpXlInputRecordDetails.add(detail);
                    }
                    reportErpXlInputRecordDetailService.saveOrUpdateBatch(reportErpXlInputRecordDetails);
                    reportErpXlInputRecord.setQuantity(weight);
                    reportErpXlInputRecordService.saveOrUpdate(reportErpXlInputRecord);
                }
            }
            //更新状态
            materialApplyOrderService.updatePulled(list);

        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
