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

import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasepluginunit.model.MeterBasePluginUnit;
import com.steerinfo.baseinfo.meterbasepluginunit.mapper.MeterBasePluginUnitMapper;
import com.steerinfo.baseinfo.meterbasepluginunit.service.IMeterBasePluginUnitService;
import com.steerinfo.framework.utils.base.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;

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

    @Autowired
    private MeterBasePluginUnitMapper meterBasePluginUnitMapper;

    @Override
    protected IBaseMapper<MeterBasePluginUnit, String> getMapper() {
        return meterBasePluginUnitMapper;
    }

    @Override
    public String checkandadd(MeterBasePluginUnit model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("pluginUnitName", model.getPluginUnitName());
        obj.put("pluginUnitEdition", model.getPluginUnitEdition());
        List<MeterBasePluginUnit> com = meterBasePluginUnitMapper.selectByParameters(obj);
        if (! com.isEmpty()){
            return "相同版本的组件名称不允许重复！！";
        }
        String no=meterBasePluginUnitMapper.GetNewID();
        model.setPluginUnitNo(new SimpleDateFormat("yyyyMMdd").format(new Date())+no);
        meterBasePluginUnitMapper.insert(model);

        return "";
    }

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

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

    @Override
    public String checkandupdate(String id,MeterBasePluginUnit model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("pluginUnitName", model.getPluginUnitName());
        obj.put("pluginUnitEdition", model.getPluginUnitEdition());
        obj.put("pluginUnitNo", model.getPluginUnitNo());
        List<MeterBasePluginUnit> com = meterBasePluginUnitMapper.selectForUpdate(obj);
        if (! com.isEmpty()){
            return "相同版本的组件名称不允许重复！！";
        }
        meterBasePluginUnitMapper.updateByPrimaryKey(model);
        return "";
    }

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

    private String check(MeterBasePluginUnit model)
    {
        if( model == null){
            return "参数为空！！";
        }
        if (StringUtils.isEmpty(model.getPluginUnitName())){
            return "插件名称不能为空！！";
        }
        if (model.getPluginUnitEdition() == null){
            return "插件版本不能为空！！";
        }
        if (StringUtils.isEmpty(model.getPluginUnitBelong())){
            return "插件归属不能为空！！";
        }
        if (StringUtils.isEmpty(model.getPluginUnitUse())){
            return "使用条件不能为空！！";
        }
        if (StringUtils.isEmpty(model.getPluginUnitEffect())){
            return "插件作用不能为空！！";
        }
        if(StringUtils.isEmpty(model.getPluginUnitNo())){
            model.setCreateTime(new Date());
            model.setValueFlag("0");//有效
        }else {
            model.setUpdateTime(new Date());
        }
        return "";
    }
}
