package com.sw.mes.yb.report.service.impl;

import com.sw.mes.base.system.entity.DictionaryItem;
import com.sw.mes.base.system.service.DictionaryItemService;
import com.sw.mes.core.base.BaseServiceImpl;
import com.sw.mes.core.context.BaseContextHandler;
import com.sw.mes.yb.plan.entity.ProductionOrder;
import com.sw.mes.yb.plan.service.ProductionOrderService;
import com.sw.mes.yb.report.dao.ScheduledProductionConfigDao;
import com.sw.mes.yb.report.dto.ScheduleProductionConfigDto;
import com.sw.mes.yb.report.entity.ScheduledProductionConfig;
import com.sw.mes.yb.report.service.ScheduleProductionConfigService;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author JS107742
 */
@Service
@Transactional
public class ScheduleProductionConfigServiceImpl extends BaseServiceImpl<ScheduledProductionConfig> implements ScheduleProductionConfigService {

    @Autowired
    ModelMapper modelMapper;

    @Autowired
    private ProductionOrderService productionOrderService;

    @Autowired
    private DictionaryItemService dictionaryItemService;

    ScheduledProductionConfigDao dao;

    @Autowired
    public void setDao(ScheduledProductionConfigDao dao) {
        super.setBaseDao(dao);
        this.dao = dao;
    }


    @Override
    public ScheduledProductionConfig findByOrderNo(String orderNo) {
        return dao.findByOrderNo(orderNo);
    }

    @Override
    public List<ScheduledProductionConfig> plannedInitialization(List<ScheduleProductionConfigDto> scheduleProductionConfigDtos) {

        List<DictionaryItem> product_category = dictionaryItemService.findByThemeValueAndEnable("product_category", true);
        Map<String, String> collect = product_category.stream().collect(Collectors.toMap(DictionaryItem::getValue, DictionaryItem::getName));

        List<ProductionOrder> productionOrders = productionOrderService.findAll();
        Map<String, ProductionOrder> collect1 = productionOrders.stream().collect(Collectors.toMap(ProductionOrder::getAufnr, productionOrder -> productionOrder));

        List<ScheduledProductionConfig> scheduleProductionConfigList = new ArrayList<>();

        for (ScheduleProductionConfigDto scheduleProductionConfigDto : scheduleProductionConfigDtos) {
            if(collect1.containsKey(scheduleProductionConfigDto.getOrderNo())){
                ProductionOrder byAufnr = collect1.get(scheduleProductionConfigDto.getOrderNo());
                scheduleProductionConfigDto.setCreateTime(new Date());
                scheduleProductionConfigDto.setCreateUser(BaseContextHandler.getUsername());
                scheduleProductionConfigDto.setHourPlan(scheduleProductionConfigDto.getDayPlan().divide(new BigDecimal(6), 0, BigDecimal.ROUND_HALF_UP));
                scheduleProductionConfigDto.setProductType(collect.get(byAufnr.getProductType()));
                scheduleProductionConfigDto.setSide(byAufnr.getSideLengthRange());
                scheduleProductionConfigDto.setOrderType(byAufnr.getAuart());
                ScheduledProductionConfig scheduledProductionConfig = fromDto(scheduleProductionConfigDto);
                scheduleProductionConfigList.add(scheduledProductionConfig);
            } else {
                throw new RuntimeException("未查询到单晶生产订单【" + scheduleProductionConfigDto.getOrderNo() + "】");
            }
        }
        return scheduleProductionConfigList;
    }

    public ScheduledProductionConfig fromDto(ScheduleProductionConfigDto entity) {
        return modelMapper.map(entity, ScheduledProductionConfig.class);
    }
}
