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

import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasetermtaretime.model.MeterBaseTermTareTime;
import com.steerinfo.baseinfo.meterbasetermtaretime.mapper.MeterBaseTermTareTimeMapper;
import com.steerinfo.baseinfo.meterbasetermtaretime.service.IMeterBaseTermTareTimeService;
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.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

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

    @Autowired
    private MeterBaseTermTareTimeMapper meterBaseTermTareTimeMapper;

    @Override
    protected IBaseMapper<MeterBaseTermTareTime, String> getMapper() {
        return meterBaseTermTareTimeMapper;
    }

    @Override
    public String checkandadd(MeterBaseTermTareTime model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("valueFlag","0");//有效
        List<MeterBaseTermTareTime> list=meterBaseTermTareTimeMapper.selectByParameters(obj);
        for (MeterBaseTermTareTime list1:list) {
            list1.setTimeNo(list1.getTimeNo());
            list1.setValueFlag("1");//无效
            meterBaseTermTareTimeMapper.updateByPrimaryKeySelective(list1);
        }
        String no=meterBaseTermTareTimeMapper.GetNewID();
        model.setId(new SimpleDateFormat("yyyyMMdd").format(new Date())+no);
        model.setCreateTime(new Date());
        model.setValueFlag("0");//有效
        model.setTimeEdition(new BigDecimal("1.0"));
        meterBaseTermTareTimeMapper.insert(model);
        return "";
    }

    @Override
    public String Recovery(List<MeterBaseTermTareTime> models)
    {
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            for (MeterBaseTermTareTime model:models) {
                HashMap<String, Object> obj = new HashMap<>();
                obj.put("valueFlag","0");//有效
                List<MeterBaseTermTareTime> list=meterBaseTermTareTimeMapper.selectByParameters(obj);
                for (MeterBaseTermTareTime list1:list)
                {
                    list1.setTimeNo(list1.getTimeNo());
                    list1.setValueFlag("1");//无效
                    meterBaseTermTareTimeMapper.updateByPrimaryKeySelective(list1);
                }
                model.setTimeNo(model.getTimeNo());
                model.setValueFlag("0");
                model.setUpdateManName(model.getUpdateManName());
                model.setUpdateManNo(model.getUpdateManNo());
                model.setUpdateTime(new Date());
                meterBaseTermTareTimeMapper.updateByPrimaryKey(model);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    public String checkandupdate(String id,MeterBaseTermTareTime model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        model.setUpdateTime(new Date());
        model.setTimeEdition(model.getTimeEdition().add(new BigDecimal(0.1)));
        meterBaseTermTareTimeMapper.updateByPrimaryKey(model);
        return "";
    }

    @Override
    public String abolish(List<MeterBaseTermTareTime> models)
    {
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            for (MeterBaseTermTareTime model:models) {
                model.setId(model.getId());
                model.setValueFlag("1");
                model.setUpdateManName(model.getUpdateManName());
                model.setUpdateManNo(model.getUpdateManNo());
                model.setUpdateTime(new Date());
                meterBaseTermTareTimeMapper.updateByPrimaryKey(model);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    private String check (MeterBaseTermTareTime model){
        if(model == null){
            return "数据为空！";
        }
        if (StringUtils.isEmpty(model.getTimeTypeNo())){
            return "配置方式编号不能为空";
        }
        if (StringUtils.isEmpty(model.getTimeTypeName())){
            return "配置方式名称不能为空";
        }
        if (model.getWarnValue() == null){
            return "提醒间隔不能为空";
        }
        return "";
    }
}
