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

import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasesubunit.model.MeterBaseSubunit;
import com.steerinfo.baseinfo.meterbasesubunit.mapper.MeterBaseSubunitMapper;
import com.steerinfo.baseinfo.meterbasesubunit.service.IMeterBaseSubunitService;
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;

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

    @Autowired
    private MeterBaseSubunitMapper meterBaseSubunitMapper;

    @Override
    protected IBaseMapper<MeterBaseSubunit, String> getMapper() {
        return meterBaseSubunitMapper;
    }

    @Override
    public String checkandadd(MeterBaseSubunit model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("subunitName", model.getSubunitName());
        List<MeterBaseSubunit> com=meterBaseSubunitMapper.selectByParameters(obj);
        if (! com.isEmpty()){
            return "组件名称不允许重复！！";
        }
        String no=meterBaseSubunitMapper.GetNewID();
        model.setSubunitNo(new SimpleDateFormat("yyyyMMdd").format(new Date())+no);
        meterBaseSubunitMapper.insert(model);
        return "";
    }

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

    @Override
    public String checkandupdate(String id,MeterBaseSubunit model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("subunitName", model.getSubunitName());
        obj.put("subunitNo", model.getSubunitNo());
        List<MeterBaseSubunit> com=meterBaseSubunitMapper.selectForUpdate(obj);
        if (! com.isEmpty()){
            return "组件名称不允许重复！！";
        }
        meterBaseSubunitMapper.updateByPrimaryKey(model);
        return "";
    }

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

    private String check (MeterBaseSubunit model){
        if (model == null){
            return "参数为空！";
        }
        if (StringUtils.isEmpty(model.getSubunitName())){
            return "组件名称不能为空！！";
        }
        if (StringUtils.isEmpty(model.getSubunitEntrust())){
            return "组件委托不能为空！！";
        }
        if (StringUtils.isEmpty(model.getSubunitBelong())){
            return "组件归属不能为空！！";
        }
        if (StringUtils.isEmpty(model.getSubunitEffect())){
            return "组件作用不能为空！！";
        }
        if(StringUtils.isEmpty(model.getSubunitNo())){
            model.setCreateTime(new Date());
            model.setValueFlag("0");//有效
        }else {
            model.setUpdateTime(new Date());
        }
        return "";
    }
}
