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

import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasefunction.model.MeterBaseFunction;
import com.steerinfo.baseinfo.meterbasefunction.mapper.MeterBaseFunctionMapper;
import com.steerinfo.baseinfo.meterbasefunction.service.IMeterBaseFunctionService;
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.List;

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

    @Autowired
    private MeterBaseFunctionMapper meterBaseFunctionMapper;

    @Override
    protected IBaseMapper<MeterBaseFunction, String> getMapper() {
        return meterBaseFunctionMapper;
    }

    @Override
    public String checkandadd(MeterBaseFunction model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        String no=meterBaseFunctionMapper.GetNewID();
        model.setFunctionNo(new SimpleDateFormat("yyyyMMdd").format(new Date())+no);
        meterBaseFunctionMapper.insert(model);
        return "";
    }

    @Override
    public String Recovery(List<MeterBaseFunction> models)
    {
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            for (MeterBaseFunction model:models) {
                model.setId(model.getId());
                model.setValueFlag("0");
                model.setUpdateManName(model.getUpdateManName());
                model.setUpdateManNo(model.getUpdateManNo());
                model.setUpdateTime(new Date());
                meterBaseFunctionMapper.updateByPrimaryKey(model);

            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    public String checkandupdate(String id,MeterBaseFunction model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        meterBaseFunctionMapper.updateByPrimaryKey(model);
        return "";
    }

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

    private String check (MeterBaseFunction model){
        if (model == null){
            return "参数为空！";
        }
        if (StringUtils.isEmpty(model.getFunctionName())){
            return "函数名称不能为空！！";
        }
        if (StringUtils.isEmpty(model.getFunctionParam())){
            return "函数参数不能为空！！";
        }
        if (StringUtils.isEmpty(model.getFunctionOutput())){
            return "函数输出不能为空！！";
        }
        if (StringUtils.isEmpty(model.getFunctionBelong())){
            return "函数归属不能为空！！";
        }
        if (StringUtils.isEmpty(model.getFunctionEffect())){
            return "函数作用不能为空！！";
        }
        if(StringUtils.isEmpty(model.getFunctionNo())){
            model.setCreateTime(new Date());
            model.setValueFlag("0");//有效
        }else {
            model.setUpdateTime(new Date());
        }
        return "";
    }
}
