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

import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasetimesinfo.model.MeterBaseTimesInfo;
import com.steerinfo.baseinfo.meterbasetimesinfo.mapper.MeterBaseTimesInfoMapper;
import com.steerinfo.baseinfo.meterbasetimesinfo.service.IMeterBaseTimesInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Date;
import java.util.List;

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

    @Autowired
    private MeterBaseTimesInfoMapper meterBaseTimesInfoMapper;

    @Override
    protected IBaseMapper<MeterBaseTimesInfo, String> getMapper() {
        return meterBaseTimesInfoMapper;
    }
    public String checkandadd(MeterBaseTimesInfo model)throws  Exception
    {
        try{
            if (model.getStartTime()!= null && !model.getStartTime().equals("")) {
                if (model.getEndTime()!= null && !model.getEndTime().equals("")) {
                String no=meterBaseTimesInfoMapper.GetNewID();
                model.setTimesNo(no);
                model.setCreateTime(new Date());
                model.setValidFlag("1");//有效
                int num=meterBaseTimesInfoMapper.insert(model);
                if(num!=0){return "";}
                } else {
                    return "结束时间不能为空";
                }
            } else {
                return "开始时间不能为空";
            }
        }
        catch(Exception ex){
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return " ";
    }
    public String checkandupdate(String id,MeterBaseTimesInfo model)throws  Exception
    {
        try{
            if (model.getStartTime()!= null && !model.getStartTime().equals("")) {
                if (model.getEndTime()!= null && !model.getEndTime().equals("")) {
                    model.setUpdateTime(new Date());
                    int num=meterBaseTimesInfoMapper.updateByPrimaryKey(model);
                    if(num!=0){return "";}
                } else {
                    return "结束时间不能为空";
                }
            } else {
                return "开始时间不能为空";
            }
        }
        catch(Exception ex){
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }
    public String Recovery(List<MeterBaseTimesInfo> models) throws Exception
    {
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            for (MeterBaseTimesInfo model:models) {
                model.setValidFlag("1");
                model.setUpdateManName(model.getUpdateManName());
                model.setUpdateManNo(model.getUpdateManNo());
                model.setUpdateTime(new Date());
                meterBaseTimesInfoMapper.updateByPrimaryKey(model);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }
    public String abolish(List<MeterBaseTimesInfo> models) throws Exception
    {
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            for (MeterBaseTimesInfo model:models) {
                model.setValidFlag("0");
                model.setUpdateManName(model.getUpdateManName());
                model.setUpdateManNo(model.getUpdateManNo());
                model.setUpdateTime(new Date());
                meterBaseTimesInfoMapper.updateByPrimaryKey(model);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }
}
