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

import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasecard.model.MeterBaseCard;
import com.steerinfo.baseinfo.meterbasecard.mapper.MeterBaseCardMapper;
import com.steerinfo.baseinfo.meterbasecard.service.IMeterBaseCardService;
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.util.Date;
import java.util.List;

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

    @Autowired
    private MeterBaseCardMapper meterBaseCardMapper;

    @Override
    protected IBaseMapper<MeterBaseCard, String> getMapper() {
        return meterBaseCardMapper;
    }

    @Override
    public String checkandadd(MeterBaseCard model)  {
        try {
            MeterBaseCard mbc=meterBaseCardMapper.selectByPrimaryKey(model.getCardNo());
            if (mbc != null){
                return "卡号重复，请确认！";
            }
            model.setValueFlag("2");
            model.setCreateTime(new Date());
            meterBaseCardMapper.insert(model);
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "操作成功";
    }

    @Override
    public String checkandaddlist(List<MeterBaseCard> models){
        try {
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            for (MeterBaseCard model : models) {
                MeterBaseCard mbc=meterBaseCardMapper.selectByPrimaryKey(model.getCardNo());
                if (mbc != null){
                    return "卡号重复，请确认！";
                }
                if(StringUtils.isEmpty(model.getValueFlag())) {
                    model.setValueFlag("2");
                }
                model.setCreateTime(new Date());
                meterBaseCardMapper.insert(model);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "操作成功";
    }

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

    }

    @Override
    public String checkandupdate(String id, MeterBaseCard model) {
        try {
            MeterBaseCard mbc=meterBaseCardMapper.selectByPrimaryKey(model.getCardNo());
            if (mbc != null && !mbc.getCardNo().equals(id)){
                return "卡号重复，请确认！";
            }
            model.setUpdateTime(new Date());
            meterBaseCardMapper.updateByPrimaryKey(model);
            return "修改成功";
        } catch (Exception ex) {
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
    }

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

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

}
