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

import com.steerinfo.baseinfo.meterbasecussupcar.model.MeterBaseCusSupCar;
import com.steerinfo.framework.mapper.IBaseMapper;
import com.steerinfo.framework.service.impl.BaseServiceImpl;
import com.steerinfo.baseinfo.meterbasedriver.model.MeterBaseDriver;
import com.steerinfo.baseinfo.meterbasedriver.mapper.MeterBaseDriverMapper;
import com.steerinfo.baseinfo.meterbasedriver.service.IMeterBaseDriverService;
import com.steerinfo.framework.user.UserPayload;
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.math.BigDecimal;
import java.util.List;

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

    @Resource
    private MeterBaseDriverMapper meterBaseDriverMapper;

    @Override
    protected IBaseMapper<MeterBaseDriver, String> getMapper() {
        return meterBaseDriverMapper;
    }

    @Override
    public String checkandadd(MeterBaseDriver model)
    {

        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }
        String no=model.getCarId()+model.getTelNum();
        model.setDriverNo(no);
        try{
            UserPayload payload = UserPayload.getCurrUser();
            String userId = payload.getUserCode();
            String userName = payload.getUserName();
            model.setCreateManNo(userId);
            model.setCreateManName(userName);
        }catch(Exception e){
            model.setCreateManNo("admin");
            model.setCreateManName("admins");
        }
        model.setCreateTime(new Date());
        model.setValidFlag("0");//服役
        meterBaseDriverMapper.insert(model);
        return "";
    }

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

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

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

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

    @Override
    public String checkandupdate(String id,MeterBaseDriver model)
    {
        String msg=check(model);
        if (StringUtils.isNotEmpty(msg)){
            return msg;
        }

        meterBaseDriverMapper.updateByPrimaryKeySelective(model);
        return "";
    }

    @Override
    public int deleteByBatch(String[] array) {
        return 0;
    }

    @Override
    public void deleteDriverById(List<String> driverNos) {
        meterBaseDriverMapper.deleteDriverById(driverNos);
    }

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