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

import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasecarcard.model.MeterBaseCarCard;
import com.steerinfo.baseinfo.meterbasecarcard.mapper.MeterBaseCarCardMapper;
import com.steerinfo.baseinfo.meterbasecarcard.service.IMeterBaseCarCardService;
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;

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

    @Autowired
    private MeterBaseCarCardMapper meterBaseCarCardMapper;

    @Override
    protected IBaseMapper<MeterBaseCarCard, String> getMapper() {
        return meterBaseCarCardMapper;
    }

    @Override
    public String checkandadd(MeterBaseCarCard model)
    {
        try{
            if (model.getCarNo()!= null && !model.getCarNo().equals("")) {
                HashMap<String, Object> obj = new HashMap<>();
                obj.put("carNo", model.getCarNo());
                List<MeterBaseCarCard> com=meterBaseCarCardMapper.selectByParameters(obj);
                if (! com.isEmpty()) {
                    return "车号重复！！";
                }
                String no=meterBaseCarCardMapper.GetNewID();
                model.setCarCardNo(new SimpleDateFormat("yyyyMMdd").format(new Date())+no);
                model.setCreateTime(new Date());
                model.setValueFlag("1");
                int num=meterBaseCarCardMapper.insert(model);
                if(num!=0){return "";}
            } else {
                return "车号不能为空";
            }
        }
        catch(Exception ex){
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }

    @Override
    public String checkandaddlist(List<MeterBaseCarCard> models)
    {
        try{
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            for (MeterBaseCarCard model : models) {
                if (model.getCarNo() != null && !model.getCarNo().equals("")) {
                    String no = meterBaseCarCardMapper.GetNewID();
                    model.setCarCardNo(new SimpleDateFormat("yyyyMMdd").format(new Date()) + no);
                    model.setCreateTime(new Date());
                    if(model.getValueFlag()==""||model.getValueFlag().equals(""))
                    {model.setValueFlag("1");}
                    meterBaseCarCardMapper.insert(model);
                } else {
                    return "车号不能为空";
                }
            }
        }
        catch(Exception ex){
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "操作成功";
    }

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

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

    @Override
    public String checkandupdate(String id,MeterBaseCarCard model)
    {
        try{
            if (model.getCarNo()!= null && !model.getCarNo().equals("")) {
                HashMap<String, Object> obj = new HashMap<>();
                obj.put("carNo", model.getCarNo());
                obj.put("carCardNo",id);
                List<MeterBaseCarCard> com=meterBaseCarCardMapper.selectForUpdate(obj);
                if (! com.isEmpty()) {
                    return "车号重复！！";
                }
                model.setUpdateTime(new Date());
                int num=meterBaseCarCardMapper.updateByPrimaryKey(model);
                if(num!=0){return "";}
            } else {
                return "车号不能为空";
            }
        }
        catch(Exception ex){
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return " ";
    }

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

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

    @Override
    public String allocation(MeterBaseCarCard model)
    {
        try{
            model.setUpdateTime(new Date());
            model.setCardNo(model.getCardNo());
            meterBaseCarCardMapper.updatecardnotothree(model.getCardNo());
            int num=meterBaseCarCardMapper.updateByPrimaryKey(model);
            if(num!=0){return "";}
        }
        catch(Exception ex){
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return " ";
    }

    @Override
    public String separate(List<MeterBaseCarCard> models)
    {
        try{
            if (models == null || models.size() < 1) {
                return "请传入参数!";
            }
            for (MeterBaseCarCard model:models) {
                model.setUpdateTime(new Date());
                meterBaseCarCardMapper.updatecardnototwo(model.getCardNo());
                model.setCardNo("");
                meterBaseCarCardMapper.updateByPrimaryKey(model);
            }
        }
        catch(Exception ex){
            ex.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "操作异常，请联系管理员";
        }
        return "";
    }
}
