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

import com.steerinfo.baseinfo.meterbasehistaredata.model.MeterBaseHisTareData;
import com.steerinfo.baseinfo.meterbaserailwayhistaredata.mapper.MeterBaseRailwayHisTareDataMapper;
import com.steerinfo.baseinfo.meterbaserailwayhistaredata.model.MeterBaseRailwayHisTareData;
import com.steerinfo.baseinfo.meterbaserailwayhistaredetail.mapper.MeterBaseRailwayHisTareDetailMapper;
import com.steerinfo.baseinfo.meterbaserailwayhistaredetail.model.MeterBaseRailwayHisTareDetail;
import com.steerinfo.baseinfo.meterbaserailwayhistaredetail.service.IMeterBaseRailwayHisTareDetailService;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.framework.service.pagehelper.PageHelper;
import com.steerinfo.framework.service.pagehelper.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

    @Autowired(required = false)
    private MeterBaseRailwayHisTareDetailMapper meterBaseRailwayHisTareDetailMapper;

    @Resource
    private MeterBaseRailwayHisTareDataMapper meterBaseRailwayHisTareDataMapper;

    @Override
    protected IBaseMapper<MeterBaseRailwayHisTareDetail, String> getMapper() {
        return meterBaseRailwayHisTareDetailMapper;
    }

    public PageList<MeterBaseRailwayHisTareDetail> listByDesc(Map<String, Object> parameters, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<MeterBaseRailwayHisTareDetail> rows = meterBaseRailwayHisTareDetailMapper.selectByParameter(parameters);
        PageList pageInfo = new PageList(rows);
        return pageInfo;
    }

    @Override
    public String Recovery(MeterBaseRailwayHisTareDetail model) {
        try{
            if(!model.getValueFlag().equals("1")) {
                return "请选择作废数据";
            }
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("carNo", model.getCarNo());
            obj.put("valueFlag", "0");
            List<MeterBaseRailwayHisTareData> hisTareDataList = meterBaseRailwayHisTareDataMapper.selectByParameters(obj);
            if(obj.size() >= 1) {
                MeterBaseRailwayHisTareData hisData = hisTareDataList.get(0);
                long avg = ((hisData.getAvgWeight()*hisData.getMeterNum())+model.getMeterWeight())/(hisData.getMeterNum()+1);
                hisData.setAvgWeight(avg);
                hisData.setMeterNum(hisData.getMeterNum()+1);
                meterBaseRailwayHisTareDataMapper.updateByPrimaryKeySelective(hisData);
            }
            model.setValueFlag("0");
            meterBaseRailwayHisTareDetailMapper.updateByPrimaryKeySelective(model);
        }catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    public String abolish(MeterBaseRailwayHisTareDetail model) {
        try{
            if(!model.getValueFlag().equals("0")) {
                return "请选择有效数据";
            }
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("carNo", model.getCarNo());
            obj.put("valueFlag", "0");
            List<MeterBaseRailwayHisTareData> hisTareDataList = meterBaseRailwayHisTareDataMapper.selectByParameters(obj);
            if(obj.size() >= 1) {
                MeterBaseRailwayHisTareData hisData = hisTareDataList.get(0);
                long avg = ((hisData.getAvgWeight()*hisData.getMeterNum())-model.getMeterWeight())/(hisData.getMeterNum()-1);
                hisData.setAvgWeight(avg);
                hisData.setMeterNum(hisData.getMeterNum()-1);
                meterBaseRailwayHisTareDataMapper.updateByPrimaryKeySelective(hisData);
            }
            model.setValueFlag("1");
            meterBaseRailwayHisTareDetailMapper.updateByPrimaryKeySelective(model);
        }catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }
}
