package com.steerinfo.ems.emsprodplanroundweight.service.impl;

import com.steerinfo.ems.Utils.DateUtils;
import com.steerinfo.feigen.model.GxWeight;
import com.steerinfo.feigen.model.LgWeight;
import com.steerinfo.feigen.service.GxWeightFeigenService;
import com.steerinfo.feigen.service.LgWeightFeigenService;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.ems.emsprodplanroundweight.model.EmsProdplanRoundWeight;
import com.steerinfo.ems.emsprodplanroundweight.mapper.EmsProdplanRoundWeightMapper;
import com.steerinfo.ems.emsprodplanroundweight.service.IEmsProdplanRoundWeightService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * EmsProdplanRoundWeight服务实现：
 * @author generator
 * @version 1.0-SNAPSHORT 2021-12-01 04:14
 * 类描述
 * 修订历史：
 * 日期：2021-12-01
 * 作者：generator
 * 参考：
 * 描述：EmsProdplanRoundWeight服务实现
 * @see null
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 */
@Service(value = "emsProdplanRoundWeightService")
public class EmsProdplanRoundWeightServiceImpl extends BaseServiceImpl<EmsProdplanRoundWeight, String> implements IEmsProdplanRoundWeightService {

    @Autowired
    private EmsProdplanRoundWeightMapper emsProdplanRoundWeightMapper;
    @Autowired
    private LgWeightFeigenService lgWeightFeigenService;
    @Autowired
    private GxWeightFeigenService gxWeightFeigenService;
    @Override
    protected IBaseMapper<EmsProdplanRoundWeight, String> getMapper() {
        return emsProdplanRoundWeightMapper;
    }

    //获取炼钢计划的重量
    public void getWeightForLg() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = sdf.parse(DateUtils.dateStr(new Date(), "yyyy-MM-dd"));
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.DAY_OF_MONTH, -7);
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);
        List<LgWeight> lgWeights = lgWeightFeigenService.QueryWeightforRound(reStr, DateUtils.dateStr(new Date(), "yyyy-MM-dd"));
        lgWeights.forEach(item -> {
            EmsProdplanRoundWeight model = new EmsProdplanRoundWeight();
            model.setFid(item.getFid());
            model.setLcId(item.getRoundid());
            model.setQty(item.getWeight());
            model.setMaterialcode(item.getMaterialcode());
            model.setMaterialname(item.getMaterialname());
            model.setProdate(item.getProdate());
            model.setUnit(item.getUnit());
            EmsProdplanRoundWeight emsProdplanRoundWeight = emsProdplanRoundWeightMapper.selectByPrimaryKey(model.getFid());
            if(emsProdplanRoundWeight !=  null){
                emsProdplanRoundWeightMapper.updateByPrimaryKeySelective(emsProdplanRoundWeight);
            }
            else {
                emsProdplanRoundWeightMapper.insert(model);
            }
        });

    }
    //获取高线计划的重量
    @Override
    public void getWeightForGx() throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date dt = sdf.parse(DateUtils.dateStr(new Date(), "yyyy-MM-dd"));
        Calendar rightNow = Calendar.getInstance();
        rightNow.setTime(dt);
        rightNow.add(Calendar.DAY_OF_MONTH, -7);
        Date dt1 = rightNow.getTime();
        String reStr = sdf.format(dt1);
        List<GxWeight> gxWeights = gxWeightFeigenService.QueryWeightforRound(reStr, DateUtils.dateStr(new Date(), "yyyy-MM-dd"));
        gxWeights.forEach(item -> {
            EmsProdplanRoundWeight model = new EmsProdplanRoundWeight();
            model.setFid(item.getFid());
            model.setLcId(item.getRoundid());
            model.setQty(item.getWeight());
            model.setMaterialcode(item.getMaterialcode());
            model.setMaterialname(item.getMaterialname());
            model.setProdate(item.getProdate());
            model.setUnit(item.getUnit());
            EmsProdplanRoundWeight emsProdplanRoundWeight = emsProdplanRoundWeightMapper.selectByPrimaryKey(model.getFid());
            if(emsProdplanRoundWeight !=  null){
                emsProdplanRoundWeightMapper.updateByPrimaryKeySelective(model);
            }
            else {
                emsProdplanRoundWeightMapper.insert(model);
            }
        });
    }

}
