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

import com.steerinfo.baseinfo.meterbasehistaredata.mapper.MeterBaseHisTareDataMapper;
import com.steerinfo.baseinfo.meterbasehistaredata.model.MeterBaseHisTareData;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasehistaredetail.model.MeterBaseHisTareDetail;
import com.steerinfo.baseinfo.meterbasehistaredetail.mapper.MeterBaseHisTareDetailMapper;
import com.steerinfo.baseinfo.meterbasehistaredetail.service.IMeterBaseHisTareDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

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

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

    @Resource
    private MeterBaseHisTareDetailMapper meterBaseHisTareDetailMapper;

    @Resource
    private MeterBaseHisTareDataMapper meterBaseHisTareDataMapper;

    @Override
    protected IBaseMapper<MeterBaseHisTareDetail, String> getMapper() {
        return meterBaseHisTareDetailMapper;
    }

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

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