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

import com.steerinfo.baseinfo.meterbasecussupcar.mapper.MeterBaseCusSupCarMapper;
import com.steerinfo.baseinfo.meterbasecussupcar.model.MeterBaseCusSupCar;
import com.steerinfo.baseinfo.meterbasecussupcar.service.IMeterBaseCusSupCarService;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.util.StringUtils;
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.List;

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

    @Resource
    private MeterBaseCusSupCarMapper meterBaseCusSupCarMapper;

    @Override
    protected IBaseMapper<MeterBaseCusSupCar, String> getMapper() {
        return meterBaseCusSupCarMapper;
    }

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

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

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

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

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

    @Override
    public String checkandupdate(String id,MeterBaseCusSupCar 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<MeterBaseCusSupCar> com =meterBaseCusSupCarMapper.selectForUpdate(obj);
//        if(!com.isEmpty()){
//            return "该车号已存在！";
//        }
        meterBaseCusSupCarMapper.updateByPrimaryKeySelective(model);
        return "";
    }

    private String check(MeterBaseCusSupCar model)
    {
        if (model == null){
            return "参数不能为空！！";
        }
        if (StringUtils.isEmpty(model.getCusSupCarNo())){
            return "车号不能为空！！";
        }

        // 20211229 by BourneCao APP自助注册不要求身份证，客商信息
//        if (StringUtils.isEmpty(model.getDriverName())){
//            return "司机姓名不能为空！！";
//        }
//        if (StringUtils.isEmpty(model.getIdNum())){
//            return "身份证号不能为空！！";
//        }
//        if (StringUtils.isEmpty(model.getCustomerSupplierNo())){
//            return "客商编号不能为空！！";
//        }
//        if (StringUtils.isEmpty(model.getCustomerSupplierName())){
//            return "客商名称不能为空！！";
//        }
        if(StringUtils.isEmpty(model.getPkNo())){
            model.setCreateTime(new Date());
            model.setValidFlag("0");//服役
        }else {
            model.setUpdateTime(new Date());
        }
        return "";
    }
}
