package com.steerinfo.baseinfo.meterbaserailwayhistaredata.service.impl;

import com.steerinfo.baseinfo.meterbaserailwayhistaredata.mapper.MeterBaseRailwayHisTareDataMapper;
import com.steerinfo.baseinfo.meterbaserailwayhistaredata.model.MeterBaseRailwayHisTareData;
import com.steerinfo.baseinfo.meterbaserailwayhistaredata.service.IMeterBaseRailwayHisTareDataService;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.framework.user.UserPayload;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

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

    @Autowired
    private MeterBaseRailwayHisTareDataMapper meterBaseRailwayHisTareDataMapper;

    @Override
    protected IBaseMapper<MeterBaseRailwayHisTareData, String> getMapper() {
        return meterBaseRailwayHisTareDataMapper;
    }

    @Override
    public RESTfulResult railwayHisTare(String railwayNo, String meterWeight,String meterName, String baseSpotNo, String baseSpotName) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try {
            if (railwayNo.equals("")) {
                rm.setMessage("火车车号不能为空！！");
                return rm;
            }
            if (meterWeight.equals("")) {
                rm.setMessage("计量重量不能为空！！");
                return rm;
            }
            HashMap<String, Object> map = new HashMap<>();
            map.put("railwayNo", railwayNo);
            List<MeterBaseRailwayHisTareData> model = meterBaseRailwayHisTareDataMapper.selectByParameters(map);
            if (model.size() >= 1) {
                MeterBaseRailwayHisTareData hisTaremodel = model.get(0);
                hisTaremodel.setMeterWeight(Long.parseLong(meterWeight));
                hisTaremodel.setMeterTime(new Date());
                hisTaremodel.setMeterName(meterName);
                hisTaremodel.setValueFlag("0");
                hisTaremodel.setUpWeight(hisTaremodel.getMeterWeight());
                //新平均重量 = 当前平均重量*总次数+当前计量重量/(总次数+1)
                Long num = hisTaremodel.getMeterNum() + 1;
                Long avg = (hisTaremodel.getAvgWeight() * hisTaremodel.getMeterNum() + Long.parseLong(meterWeight)) / num;
                hisTaremodel.setAvgWeight(avg);
                hisTaremodel.setMeterNum(num);
                hisTaremodel.setBaseSpotNo(baseSpotNo);
                hisTaremodel.setBaseSpotName(baseSpotName);
                int i = meterBaseRailwayHisTareDataMapper.updateByPrimaryKeySelective(hisTaremodel);
                if (i >= 1) {
                    rm.setSucceed();
                    rm.setData(hisTaremodel);
                    rm.setMessage("操作成功！！");
                    return rm;
                }
            } else {
                MeterBaseRailwayHisTareData hisTareData = new MeterBaseRailwayHisTareData();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
                String no = meterBaseRailwayHisTareDataMapper.getNewID(sdf.format(new Date()));
                hisTareData.setDataNo(sdf.format(new Date()) + no);
                hisTareData.setRailwayNo(railwayNo);
                hisTareData.setMeterWeight(Long.parseLong(meterWeight));
                hisTareData.setMeterNum(new Long(1));
                hisTareData.setAvgWeight(Long.parseLong(meterWeight));
                hisTareData.setUpWeight(Long.parseLong(meterWeight));
                hisTareData.setValueFlag("0");
                hisTareData.setMeterName(meterName);
                hisTareData.setMeterTime(new Date());
                hisTareData.setCreateTime(new Date());
                UserPayload payload = UserPayload.getCurrUser();
                String userId = payload.getId();
                String userName = payload.getUserName();
                hisTareData.setCreateManNo(userId);
                hisTareData.setCreateManName(userName);
                /*hisTareData.setCreateManNo("userId");
                hisTareData.setCreateManName("userName");*/
                hisTareData.setBaseSpotNo(baseSpotNo);
                hisTareData.setBaseSpotName(baseSpotName);
                int i = meterBaseRailwayHisTareDataMapper.insertSelective(hisTareData);
                if (i >= 1) {
                    rm.setSucceed();
                    rm.setData(hisTareData);
                    rm.setMessage("操作成功！！");
                    return rm;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return rm;
        }
        return rm;
    }

}
