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

import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasepredictiondiff.model.MeterBasePredictionDiff;
import com.steerinfo.baseinfo.meterbasepredictiondiff.mapper.MeterBasePredictionDiffMapper;
import com.steerinfo.baseinfo.meterbasepredictiondiff.service.IMeterBasePredictionDiffService;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

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

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

    @Autowired
    private MeterBasePredictionDiffMapper meterBasePredictionDiffMapper;

    @Override
    protected IBaseMapper<MeterBasePredictionDiff, String> getMapper() {
        return meterBasePredictionDiffMapper;
    }

    @Override
    public String checkandadd(MeterBasePredictionDiff model){
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        switch (model.getDiffTypeName())
        {
            case "固重":if (model.getFixedWeightValue() == null)
            {
                return "固重值不能为空";
            }
                break;
            case "比例":if (model.getRateValue() == null)
            {
                return "比例值不能为空";
            }
                break;
            default:break;
        }
        String no = meterBasePredictionDiffMapper.GetNewID();
        model.setDiffNo(new SimpleDateFormat("yyyyMMdd").format(new Date()) + no);
        meterBasePredictionDiffMapper.insertSelective(model);
        return "";
    }

    @Override
    public String Recovery(MeterBasePredictionDiff model){
        try
        {
            model.setValueFlag("0");
            model.setUpdateManName(model.getUpdateManName());
            model.setUpdateManNo(model.getUpdateManNo());
            model.setUpdateTime(new Date());
            meterBasePredictionDiffMapper.updateByPrimaryKey(model);
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    public String checkandupdate(String id, MeterBasePredictionDiff model){
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        switch (model.getDiffTypeName())
        {
            case "固重":if (model.getFixedWeightValue() == null)
            {
                return "固重值不能为空";
            }
                break;
            case "比例":if (model.getRateValue() == null)
            {
                return "比例值不能为空";
            }
                break;
            default:break;
        }
        meterBasePredictionDiffMapper.updateByPrimaryKey(model);
        return "";
    }

    @Override
    public String abolish(MeterBasePredictionDiff model)
    {
        try {
            model.setValueFlag("1");
            model.setUpdateManName(model.getUpdateManName());
            model.setUpdateManNo(model.getUpdateManNo());
            model.setUpdateTime(new Date());
            meterBasePredictionDiffMapper.updateByPrimaryKey(model);
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    private String check (MeterBasePredictionDiff model){
        if (model == null){
            return "数据为空";
        }
        if (StringUtils.isEmpty(model.getDiffTypeNo())){
            return "超差方式编号不能为空！！";
        }
        if (StringUtils.isEmpty(model.getDiffTypeName())){
            return "超差方式名称不能为空！！";
        }
        if (StringUtils.isEmpty(model.getMeterTypeNo())){
            return "计量类型编号不能为空！！";
        }
        if (StringUtils.isEmpty(model.getMeterTypeName())){
            return "计量类型名称不能为空！！";
        }
        if(StringUtils.isEmpty(model.getDiffNo())){
            model.setCreateTime(new Date());
            model.setValueFlag("0");//有效
        }else {
            model.setUpdateTime(new Date());
        }
        return "";
    }
}
