package com.jisco.dynamicweight.dynamicworkdata.service.impl;

import com.jisco.dynamicweight.dynamicworkdata.mapper.DynamicWorkDataMapper;
import com.jisco.dynamicweight.dynamicworkdata.model.DynamicWorkData;
import com.jisco.dynamicweight.util.UserUtil;
import com.steerinfo.baseinfo.combaseinfo.model.ComBaseInfo;
import com.steerinfo.baseinfo.combaseinfo.service.IComBaseInfoService;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.jisco.dynamicweight.dynamicworkdata.service.IDynamicWorkDataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * DynamicWorkData服务实现：
 *
 * @author generator
 * @version 1.0-SNAPSHORT 2022-10-26 09:59
 * 类描述
 * 修订历史：
 * 日期：2022-10-26
 * 作者：generator
 * 参考：
 * 描述：DynamicWorkData服务实现
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 * @see null
 */
@Service(value = "dynamicWorkDataService")
public class DynamicWorkDataServiceImpl extends BaseServiceImpl<DynamicWorkData, String> implements IDynamicWorkDataService {

    @Autowired
    private DynamicWorkDataMapper dynamicWorkDataMapper;

    @Autowired
    private IComBaseInfoService comBaseInfoService;

    @Override
    protected IBaseMapper<DynamicWorkData, String> getMapper() {
        return dynamicWorkDataMapper;
    }

    /**
     * 3#煤塔秤分批
     *
     * @param startTime
     * @param endTime
     */
    @Transactional
    public void split3mt(Date startTime, Date endTime) {
        ComBaseInfo receiver = comBaseInfoService.getById("001050004001");
        Assert.isTrue(receiver != null && StringUtils.isNotBlank(receiver.getMemo()), "3#煤塔分批收货库房配置错误，请检查！");
        ComBaseInfo station = comBaseInfoService.getById("001050004002");
        Assert.isTrue(station != null && StringUtils.isNotBlank(station.getMemo()), "3#煤塔分批后站点名称配置错误，请检查！");
        ComBaseInfo material = comBaseInfoService.getById("001050004003");
        Assert.isTrue(material != null && StringUtils.isNotBlank(material.getMemo()), "3#煤塔总量物料名称配置错误，请检查！");
        // 1.计算(M1M1B)去1#-4#焦炉总量
        BigDecimal total = dynamicWorkDataMapper.getTotalWeightOfM1AndM1BTo1_4(receiver.getMemo(), startTime, endTime);
        Assert.isTrue(total != null && total.compareTo(BigDecimal.ZERO) > 0, "M1、M1B去1#-4#焦炉总量为0，无法计算!");
        // 2.计算3煤塔总量
        BigDecimal total3MT = dynamicWorkDataMapper.getTotalWeightOf3MT(material.getMemo(), startTime, endTime);
        Assert.isTrue(total3MT != null && total3MT.compareTo(BigDecimal.ZERO) > 0, "3煤塔总量为0，无法计算!");
        // 3.查询(M1M1B)去1#-4#焦炉按物料分组后的重量
        List<DynamicWorkData> list = dynamicWorkDataMapper.getTotalWeightOfM1AndM1BTo1_4GroupByMaterial(receiver.getMemo(), startTime, endTime);
        // 4.M1M1B)去1#-4#焦炉按物料分组后的重量 / (M1M1B)去1#-4#焦炉总量 * 3#煤塔秤总量 , 发、收货单位保留原发、收货单位
        List<DynamicWorkData> dataToAdd = new ArrayList<>();
        list.forEach(item -> {
            DynamicWorkData data = new DynamicWorkData();
            data.setStationName(station.getMemo());
            data.setMaterialName(item.getMaterialName());
            data.setStartTime(startTime);
            data.setEndTime(endTime);
            data.setReceiver(item.getReceiver());
            data.setSender(item.getSender());
            data.setExpireFlag("0");
            data.setValidFlag("1");
            data.setWeight(item.getWeight().multiply(total3MT).divide(total, 2, BigDecimal.ROUND_HALF_UP));
            data.setCreateManNo(UserUtil.getCurrentUserId());
            data.setCreateManName(UserUtil.getCurrentUserName());
            data.setCreateTime(new Date());
            dataToAdd.add(data);
        });
        int i = this.batchAdd(dataToAdd);
        Assert.isTrue(i == dataToAdd.size(), "保存批分数据失败");
        dynamicWorkDataMapper.recalc();
    }

    /**
     * 重新获取动态秤称重数据
     */
    @Override
    public void recalc() {
        dynamicWorkDataMapper.recalc();
    }
}
