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

import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasecarrierunitcar.model.MeterBaseCarrierUnitCar;
import com.steerinfo.baseinfo.meterbasecarrierunitcar.mapper.MeterBaseCarrierUnitCarMapper;
import com.steerinfo.baseinfo.meterbasecarrierunitcar.service.IMeterBaseCarrierUnitCarService;
import com.steerinfo.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

    @Resource
    private MeterBaseCarrierUnitCarMapper meterBaseCarrierUnitCarMapper;

    @Override
    protected IBaseMapper<MeterBaseCarrierUnitCar, String> getMapper() {
        return meterBaseCarrierUnitCarMapper;
    }

    @Override
    public String checkandadd(MeterBaseCarrierUnitCar model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("cusSupCarNo", model.getCusSupCarNo());
        List<MeterBaseCarrierUnitCar> com =meterBaseCarrierUnitCarMapper.selectByParameters(obj);
        if( !com.isEmpty() ){
            return "该车号已存在！";
        }
        String no=meterBaseCarrierUnitCarMapper.GetNewID();
        model.setId(new SimpleDateFormat("yyyyMMdd").format(new Date())+no);
        meterBaseCarrierUnitCarMapper.insert(model);
        return "";
    }

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

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

    @Override
    public String checkandupdate(String id,MeterBaseCarrierUnitCar model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        HashMap<String, Object> obj = new HashMap<>();
        obj.put("cusSupCarNo", model.getCusSupCarNo());
        obj.put("pkNo", id);
        List<MeterBaseCarrierUnitCar> com =meterBaseCarrierUnitCarMapper.selectForUpdate(obj);
        if( !com.isEmpty() ){
            return "该车号已存在！";
        }
        meterBaseCarrierUnitCarMapper.updateByPrimaryKey(model);
        return "";
    }



    private String check(MeterBaseCarrierUnitCar model)
    {
        if (model == null){
            return "参数不能为空！！";
        }
        if (StringUtils.isEmpty(model.getCusSupCarNo())){
            return "车号不能为空！！";
        }
        if (StringUtils.isEmpty(model.getDriverName())){
            return "司机姓名不能为空！！";
        }
        if (StringUtils.isEmpty(model.getIdNum())){
            return "身份证号不能为空！！";
        }
        if (StringUtils.isEmpty(model.getCarrierUnitNo())){
            return "承运单位编号不能为空！！";
        }
        if (StringUtils.isEmpty(model.getCarrierUnitName())){
            return "承运单位名称不能为空！！";
        }
        if(StringUtils.isEmpty(model.getPkNo())){
            model.setCreateTime(new Date());
            model.setValidFlag("0");//服役
        }else {
            model.setUpdateTime(new Date());
        }
        return "";
    }
}
