package com.steerinfo.baseinfo.meterbasecar.controller;

import com.alibaba.fastjson.JSONObject;
import com.jisco.dynamicweight.util.UserUtil;
import com.steerinfo.baseinfo.appverificationcode.mapper.AppVerificationCodeMapper;
import com.steerinfo.baseinfo.appverificationcode.model.AppVerificationCode;
import com.steerinfo.baseinfo.meterbasecar.mapper.MeterBaseCarMapper;
import com.steerinfo.baseinfo.meterbasecussupcar.model.MeterBaseCusSupCar;
import com.steerinfo.baseinfo.meterbasecustomersupplier.mapper.MeterBaseCustomerSupplierMapper;
import com.steerinfo.baseinfo.meterbasecustomersupplier.model.MeterBaseCustomerSupplier;
import com.steerinfo.baseinfo.meterbasedriver.mapper.MeterBaseDriverMapper;
import com.steerinfo.baseinfo.meterbasedriver.model.MeterBaseDriver;
import com.steerinfo.baseinfo.meterbasedriver.service.IMeterBaseDriverService;
import com.steerinfo.baseinfo.meterbasespecialunit.mapper.MeterBaseSpecialUnitMapper;
import com.steerinfo.baseinfo.meterbasespecialunit.model.MeterBaseSpecialUnit;
import com.steerinfo.baseinfo.meterbasetravellingmerchant.mapper.MeterBaseTravellingMerchantMapper;
import com.steerinfo.baseinfo.meterbasetravellingmerchant.model.MeterBaseTravellingMerchant;
import com.steerinfo.baseinfo.meterbaseverificationcode.mapper.MeterBaseVerificationCodeMapper;
import com.steerinfo.framework.controller.BaseRESTfulController;
import com.steerinfo.framework.controller.RESTfulResult;
import com.steerinfo.framework.service.pagehelper.PageList;
import com.steerinfo.framework.user.UserPayload;
import com.steerinfo.framework.utils.collection.ListUtils;
import com.steerinfo.baseinfo.meterbasecar.model.MeterBaseCar;
import com.steerinfo.baseinfo.meterbasecar.service.IMeterBaseCarService;
import com.steerinfo.meterwork.except.MarkerMetException;
import com.steerinfo.util.CommonPage;
import com.steerinfo.util.SSOUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.DecimalFormat;
import java.util.*;
import java.math.BigDecimal;

/**
 * MeterBaseCar RESTful接口：
 *
 * @author generator
 * @version 1.0-SNAPSHORT 2022-01-10 10:09
 * 类描述
 * 修订历史：
 * 日期：2022-01-10
 * 作者：generator
 * 参考：
 * 描述：MeterBaseCar RESTful接口
 * @Copyright 湖南视拓信息技术股份有限公司. All rights reserved.
 * @see null
 */
@RestController
@RequestMapping("/${api.version}/meterbasecars")
@Api(value = "/${api.version}/meterbasecars", tags = "车辆信息 - MeterBaseCarControllerv")
public class MeterBaseCarController extends BaseRESTfulController {

    @Autowired
    IMeterBaseCarService meterBaseCarService;

    @Resource
    MeterBaseSpecialUnitMapper meterBaseSpecialUnitMapper;

    @Autowired
    IMeterBaseDriverService meterBaseDriverService;

    @Resource
    MeterBaseCarMapper meterBaseCarMapper;

    @Resource
    MeterBaseDriverMapper meterBaseDriverMapper;

    @Resource
    MeterBaseCustomerSupplierMapper meterBaseCustomerSupplierMapper;

    @Resource
    MeterBaseTravellingMerchantMapper meterBaseTravellingMerchantMapper;

    @Resource
    AppVerificationCodeMapper appVerificationCodeMapper;

    @Resource
    MeterBaseVerificationCodeMapper meterBaseVerificationCodeMapper;

    @ApiOperation(value = "获取列表", notes = "分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("meterbasecar:view")
    @GetMapping(value = "/")
    public RESTfulResult list(@RequestParam HashMap parmas, Integer pageNum, Integer pageSize) {
        PageList<MeterBaseCar> list = meterBaseCarService.queryForPage(parmas, pageNum, pageSize);
        return success(list);
    }

    @ApiOperation(value = "获取列表", notes = "不分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("meterbasecar:view")
    @GetMapping(value = "/queryForNoPage")
    public RESTfulResult list(@RequestParam HashMap parmas) {
        List<MeterBaseCar> list = meterBaseCarService.queryForNoPage(parmas);
        return success(list);
    }

    @ApiOperation(value = "获取列表", notes = "分页模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "查询页数", required = false, dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页记录数", required = false, dataType = "Integer")
    })
    //@RequiresPermissions("meterbasecar:view")
    @GetMapping(value = "/like/")
    public RESTfulResult listLike(@RequestParam HashMap parmas, Integer pageNum, Integer pageSize) {
        PageList<MeterBaseCar> list = meterBaseCarService.queryLikeForPage(parmas, pageNum, pageSize);
        return success(list);
    }

    @ApiOperation(value = "创建", notes = "根据MeterBaseCar对象创建")
    @ApiImplicitParam(name = "meterBaseCar", value = "详细实体meterBaseCar", required = true, dataType = "MeterBaseCar")
    //@RequiresPermissions("meterbasecar:create")
    @PostMapping(value = "/")
    public RESTfulResult add(@ModelAttribute MeterBaseCar model) {
        MeterBaseCar meterBaseCar = meterBaseCarService.add(model);
        return success(meterBaseCar);
    }

    @ApiOperation(value = "APP修改车辆信息", notes = "根据MeterBaseCar对象创建")
    @ApiImplicitParam(name = "meterBaseCar", value = "详细实体meterBaseCar", required = true, dataType = "MeterBaseCar")
    @PostMapping(value = "/updateCar")
    public RESTfulResult updateCar(@RequestBody MeterBaseCar model) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        try{
            rm = meterBaseCarService.updateCar(model);
        }catch(Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，" + rm.getMessage());
        }
        return rm;
    }

    @ApiOperation(value = "获取详细信息", notes = "根据url的id来获取详细信息")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("meterbasecar:view")
    @GetMapping(value = "/{id}")
    public RESTfulResult get(@PathVariable String id) {
        MeterBaseCar meterBaseCar = meterBaseCarService.getById(id);
        return success(meterBaseCar);
    }

    @ApiOperation(value = "更新详细信息", notes = "根据url的id来指定更新对象，并根据传过来的meterBaseCar信息来更新详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String"),
            @ApiImplicitParam(name = "meterBaseCar", value = "详细实体meterBaseCar", required = true, dataType = "MeterBaseCar")
    })
    //@RequiresPermissions("meterbasecar:update")
    @PutMapping(value = "/{id}", produces = "application/json;charset=UTF-8")
    public RESTfulResult update(@PathVariable String id, @RequestBody MeterBaseCar model) {
        model.setId(id);
        MeterBaseCar meterBaseCar = meterBaseCarService.modify(model);
        meterBaseCar.setUpdateManNo("admin");
        meterBaseCar.setUpdateManName("admins");
        meterBaseCar.setUpdateTime(new Date());
        if(StringUtils.isNotEmpty(meterBaseCar.getUsetDepartment())) {
            HashMap<String, Object> map = new HashMap<>();
            map.put("unitNo", meterBaseCar.getUsetDepartment());
            List<MeterBaseSpecialUnit> specialUnit = meterBaseSpecialUnitMapper.selectByParameters(map);
            if(specialUnit.size() >= 1) {
                if(!specialUnit.get(0).getUnitNo().equals("888888")){
                    meterBaseCar.setUsetDepartmentNo(specialUnit.get(0).getUnitNo());
                    meterBaseCar.setUsetDepartment(specialUnit.get(0).getUnitName());
                    meterBaseCar.setIsSpecialunit("1");
                }else{
                    meterBaseCar.setUsetDepartmentNo(specialUnit.get(0).getUnitNo());
                    meterBaseCar.setUsetDepartment(specialUnit.get(0).getUnitName());
                    meterBaseCar.setIsSpecialunit("0");
                }
            }
            meterBaseCarMapper.updateByPrimaryKeySelective(meterBaseCar);
        }
        return success(meterBaseCar);
    }

    @ApiOperation(value = "删除", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("meterbasecar:delete")
    @DeleteMapping(value = "/{id}")//String
    public RESTfulResult delete(@PathVariable String id) {
        List<String> list = Arrays.asList(id.split(","));
        if (ListUtils.isNotEmpty(list)) {
            List<String> ids = ListUtils.convertList(list);
            meterBaseCarService.delete(ids);
        }
        return success();
    }

    @ApiOperation(value = "启用", notes = "根据传过来的MeterBaseCusSupCar集合信息来更新详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "MeterBaseCar", value = "详细实体MeterBaseCar", required = true, dataType = "MeterBaseCar")
    })
    @Transactional
    @PostMapping(value = "/Recovery")
    public RESTfulResult Recovery(@RequestBody List<MeterBaseCar> models) {
        try {
            String msg = meterBaseCarService.Recovery(models);
            if (com.steerinfo.util.StringUtils.isNotEmpty(msg)) {
                return failed(null, msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "操作异常，请确认！");
        }
        return success();
    }

    @ApiOperation(value = "停用", notes = "根据传过来的MeterBaseCar集合信息来更新详细信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "MeterBaseCar", value = "详细实体MeterBaseCar", required = true, dataType = "MeterBaseCar")
    })
    @Transactional
    @PostMapping(value = "/abolish")
    public RESTfulResult abolish(@RequestBody List<MeterBaseCar> models) {
        try {
            String msg = meterBaseCarService.abolish(models);
            if (StringUtils.isNotEmpty(msg)) {
                return failed(null, msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "操作异常，请确认！");
        }
        return success();
    }

    @ApiOperation(value = "app自助注册", notes = "app自助注册")
    @Transactional
    @PostMapping(value = "/appregister")
    public RESTfulResult appregister(@RequestBody HashMap params) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        //重置密码是否报错
        String msg = "";
        //记录注册或者更新了多少个司机
        int count = 0;
        try {
            SSOUtil ssoUtil = new SSOUtil();
            HashMap ssoMap = ssoUtil.ssoRegister(params);

            String carNo = params.get("carNo").toString();
            HashMap queryCarMap = new HashMap();
            queryCarMap.put("carNo", carNo);
            queryCarMap.put("validFlag", "0");
            //查询服役状态的车辆信息
            List<MeterBaseCar> carList = meterBaseCarService.query(queryCarMap);
            // sso注册成功或者没有注册成功都新增车辆和司机信息(保证测试机也能注册，并且车辆停役的也能启用)
            if (ssoMap.get("code").equals("1") && "账号已被注册".equals(ssoMap.get("msg").toString()) && carList.size() >= 1) {
                rm.setMessage(ssoMap.get("msg").toString());
                return rm;
            } else {
                HashMap queryCar = new HashMap();
                queryCar.put("carNo", carNo);
                queryCar.put("validFlag", "0");
                //查询服役状态的车辆信息
                List<MeterBaseCar> queryList01 = meterBaseCarService.query(queryCar);
                //查询停役、删除状态的车辆信息
                List<MeterBaseCar> queryList02 = meterBaseCarMapper.selectByMap(queryCar);
                MeterBaseCar car = new MeterBaseCar();
                // 车辆表未查询到车辆信息，则新增车辆信息和司机信息
                if (queryList01.size()+queryList02.size() == 0) {
                    // 新增车辆
                    car.setCarNo(params.get("carNo").toString());
                    car.setValidFlag("0");
                    HashMap<String, Object> unitMap = new HashMap<>();
                    unitMap.put("unitNo", params.get("unitNo").toString());
                    List<MeterBaseSpecialUnit> unitList = meterBaseSpecialUnitMapper.selectByParameters(unitMap);
                    //判断使用单位是否为特殊单位
                    car.setIsSpecialunit(unitList.size() >= 1 ? "1": "0");
                    car.setUsetDepartmentNo(params.get("unitNo").toString());
                    car.setUsetDepartment(params.get("unitName").toString());
                    car.setCarTypeNo(params.get("carTypeNo").toString());
                    car.setCarTypeName(params.get("carTypeName").toString());
                    if(car.getUsetDepartmentNo() != null && StringUtils.isNotEmpty(car.getUsetDepartmentNo())) {
                        //111111：对应内部倒运;
                        if(car.getUsetDepartmentNo().equals("111111")) {
                            car.setUsetDepartmentNo("888888");
                            car.setCarTypeNo("001004003");
                            car.setCarTypeName("内部");
                        }
                    }
                    if(params.get("carSelfWeight") != null ) {
                        if(!StringUtils.isEmpty(params.get("carSelfWeight").toString())) {
                            car.setCarSelfWeight(new BigDecimal(params.get("carSelfWeight").toString()));
                        }
                    }
                    if(params.get("carCarrierWeight") != null ) {
                        if(!StringUtils.isEmpty(params.get("carCarrierWeight").toString())) {
                            car.setCarCarrierWeight(new BigDecimal(params.get("carCarrierWeight").toString()));
                        }
                    }
                    // 将sso id写入车辆表，便于以后删除和重置密码
//                    if(!ssoMap.get("code").toString().equals("1")){
//                        car.setSsoId(ssoMap.get("data").toString());
//                    }else{
//                        msg = ssoUtil.ssoResetPwd(car.getSsoId(), params.get("password").toString(), "");
//                    }
                    if(ssoMap.get("code").equals("1")) {
                        HashMap<String, Object> carTemp = new HashMap<>();
                        carTemp.put("userCode", params.get("carNo").toString());
                        String ssoId = meterBaseCarMapper.ssoIdBySysUser(carTemp);
                        car.setSsoId(ssoId);
                    }else if(ssoMap.get("code").equals("0")) {
                        car.setSsoId(ssoMap.get("data").toString());
                        msg = ssoUtil.ssoResetPwd(car.getSsoId(), params.get("password").toString(), "");
                    }
                    car.setCreateManNo("admin");
                    car.setCreateManName("admins");
                    car.setCreateTime(new Date());
                    meterBaseCarService.checkandadd(car);
                }else if(queryList02.size() >= 1){
                    //将所有的停役车辆或者司机启用服役
                    for (MeterBaseCar model: queryList02) {
                        HashMap map = new HashMap();
                        queryCar.put("carId",model.getCarNo());
                        queryCar.put("pkNo", model.getPkNo());
                        //停役或者删除状态的车辆信息更新为服役状态
                        meterBaseCarMapper.updateValidFlagById(map);
                        //停役或者删除状态的司机信息更新为服役状态
                        meterBaseDriverMapper.updateValidFlagByCarId(map);
                    }
                    // 修改车辆
                    queryList01 = meterBaseCarService.query(queryCar);
                    car = queryList01.get(0);
                    car.setValidFlag("0");
                    HashMap<String, Object> unitMap = new HashMap<>();
                    unitMap.put("unitNo", params.get("unitNo").toString());
                    List<MeterBaseSpecialUnit> unitList = meterBaseSpecialUnitMapper.selectByParameters(unitMap);
                    //判断使用单位是否为特殊单位
                    car.setIsSpecialunit(unitList.size() >= 1 ? "1": "0");
                    car.setUsetDepartmentNo(params.get("unitNo").toString());
                    car.setUsetDepartment(params.get("unitName").toString());
                    car.setCarTypeNo(params.get("carTypeNo").toString());
                    car.setCarTypeName(params.get("carTypeName").toString());

                    if(car.getUsetDepartmentNo() != null && StringUtils.isNotEmpty(car.getUsetDepartmentNo())) {
                        //111111：对应内部倒运;888888：对应外进外发
                        if(car.getUsetDepartmentNo().equals("111111")) {
                            car.setUsetDepartmentNo("888888");
                            car.setCarTypeNo("001004003");
                            car.setCarTypeName("内部");
                        }
                    }
                    if(params.get("carSelfWeight") != null ) {
                        if(!StringUtils.isEmpty(params.get("carSelfWeight").toString())) {
                            car.setCarSelfWeight(new BigDecimal(params.get("carSelfWeight").toString()));
                        }
                    }
                    if(params.get("carCarrierWeight") != null ) {
                        if(!StringUtils.isEmpty(params.get("carCarrierWeight").toString())) {
                            car.setCarCarrierWeight(new BigDecimal(params.get("carCarrierWeight").toString()));
                        }
                    }
                    car.setUpdateManNo("admin");
                    car.setUpdateManName("admins");
                    car.setUpdateTime(new Date());
                    meterBaseCarMapper.updateByPrimaryKeySelective(car);
                    // 将sso id写入车辆表，便于以后删除和重置密码
                    if(StringUtils.isNotEmpty(car.getSsoId())){
                        msg = ssoUtil.ssoResetPwd(car.getSsoId(), params.get("password").toString(), "");
                    }
                }

                //润源公司（吉瑞）
//                if(car.getUsetDepartmentNo() != null && StringUtils.isNotEmpty(car.getUsetDepartmentNo())) {
//                    if(!car.getUsetDepartmentNo().equals("123456")) {
//                        //查询该车辆是否为内部车辆，内部车辆不允许批量注册司机和修改司机信息
//                        HashMap<String, Object> findCar = new HashMap<>();
//                        findCar.put("carNo", carNo);
//                        findCar.put("validFlag", "0");
//                        findCar.put("carTypeNo", "001004003");
//                        List<MeterBaseCar> carModel = meterBaseCarMapper.selectByParameters(findCar);
//                        if(carModel.size() >= 1) {
//                            rm.setMessage("内部车辆不允许批量注册司机和修改司机信息，请联系车辆管理部门");
//                            return rm;
//                        }
//                    }
//                }

                // 拆分司机list，新增司机表
                List<MeterBaseDriver> driverList = JSONObject.parseArray(params.get("driverList").toString(), MeterBaseDriver.class);
                for (MeterBaseDriver driver : driverList) {
                    //主键:车号+电话号码
                    driver.setId(params.get("carNo").toString() + driver.getTelNum());
                    driver.setDriverNo(params.get("carNo").toString() + driver.getTelNum());
                    driver.setCarId(params.get("carNo").toString());
                    driver.setDriverName(driver.getDriverName());
                    driver.setTelNum(driver.getTelNum());

                    HashMap query = new HashMap();
                    query.put("carId", params.get("carNo").toString());
                    query.put("telNum", driver.getTelNum());
                    List<MeterBaseDriver> list = meterBaseDriverService.query(query);
                    // 车辆信息已存在,则更新；车辆信息不存在，则插入
                    if (list.size() >= 1) {
                        driver.setUpdateManNo("admin");
                        driver.setUpdateManName("admins");
                        driver.setUpdateTime(new Date());
                        meterBaseDriverMapper.updateByPrimaryKeySelective(driver);
                    }else{
                        meterBaseDriverService.checkandadd(driver);
                    }
                    count ++;
                }
            }
            if (com.steerinfo.util.StringUtils.isNotEmpty(msg)) {
                rm.setMessage("重置密码失败，请确认！！");
                return rm;
            }
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常，请确认！！！>>>" + e.getMessage());
            return rm;
        }
        rm.setSucceed();
        rm.setMessage(params.get("carNo") + "车辆包含"+ count +"名司机注册成功");
        return rm;
    }

    @ApiOperation(value = "app登录", notes = "app登录")
    @Transactional
    @PostMapping(value = "/applogin")
    public RESTfulResult applogin(@RequestBody HashMap params) {
        RESTfulResult rm = failed();
        Object ssodata = "";
        HashMap<String, Object> info = new HashMap();
        try {
            if (params.get("carNo") == null || StringUtils.isEmpty(params.get("carNo").toString())) {
                rm.setMessage("车号不能为空！！");
            //} else if (params.get("telNum") == null || StringUtils.isEmpty(params.get("telNum").toString())) {
            //    rm.setMessage("手机号不能为空！！");
            } else {
                // sso登录
                SSOUtil ssoUtil = new SSOUtil();
                HashMap map = ssoUtil.ssoLogin(params, "");
                if (map.get("code").toString().equals("0")) {
                    ssodata = map.get("data").toString();
                    info.put("ssoData", ssodata);
                    HashMap<String, Object> carInfoMap = new HashMap<>();
                    carInfoMap.put("validFlag", "0");
                    carInfoMap.put("carNo", params.get("carNo"));
                    List<MeterBaseCar> carInfo = meterBaseCarMapper.selectByParameters(carInfoMap);
                    //车辆配置表是否有服役车辆
                    if(carInfo.size() >= 1){
                        info.put("carInfo", carInfo);
                        HashMap<String, Object> driverInfoMap = new HashMap();
                        driverInfoMap.put("carId", params.get("carNo"));
                        driverInfoMap.put("validFlag", "0");
                        driverInfoMap.put("telNum", params.get("telNum"));
                        //List<MeterBaseDriver> driverInfo = meterBaseDriverMapper.selectByParameters(driverInfoMap);
                        //司机配置表是否有服役的司机信息
                        //if(driverInfo.size() >= 1){
                        //    info.put("driverInfo", driverInfo);
                            rm = success();
                            rm.setData(info);
                            rm.setMessage("操作成功");
                        //}else{
                        //    rm.setMessage("司机配置表没有服役的司机信息");
                        //}
                    }else{
                        rm.setMessage("车辆配置表没有服役的车辆信息");
                    }
                } else {
                    rm.setMessage(map.get("msg").toString());
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "操作异常，请确认！");
        }
        return rm;
    }

    @ApiOperation(value = "app非司机登录", notes = "app非司机登录")
    @Transactional
    @PostMapping(value = "/notApplogin")
    public RESTfulResult notApplogin(@RequestBody HashMap params) {

        RESTfulResult rm = new RESTfulResult();
        String msg = "";
        Object ssodata = "";
        String userType = "1";
        HashMap returnObj = new HashMap();
        try {
            SSOUtil ssoUtil = new SSOUtil();
            HashMap map = ssoUtil.ssoLogin(params, "");

            if (map.get("code").toString().equals("0")) {
                ssodata = map.get("data").toString();
            } else {
                msg = map.get("msg").toString();
            }
            HashMap trave = new HashMap();
            trave.put("travellingMerchantName",params.get("carNo"));
            trave.put("travellingMerchantPossword",params.get("password"));
            List<MeterBaseTravellingMerchant> meterBaseTravellingMerchant = meterBaseTravellingMerchantMapper.selectByParameters(trave);
            if (meterBaseTravellingMerchant.size()>0){
                MeterBaseTravellingMerchant mbtm = meterBaseTravellingMerchant.get(0);
                ssodata = mbtm;
                userType = "2";
            }

            returnObj.put("ssodata",ssodata);
            returnObj.put("userType",userType);

            if (com.steerinfo.util.StringUtils.isNotEmpty(msg) && meterBaseTravellingMerchant.size()<0) {
                return failed(null, msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "操作异常，请确认！");
        }
        return success(returnObj);
    }

    @ApiOperation(value = "app修改密码", notes = "app修改密码")
    @Transactional
    @PostMapping(value = "/appchangepwd")
    public RESTfulResult appchangepwd(@RequestBody HashMap params) {

        RESTfulResult rm = failed();
        String carNo = params.get("carNo").toString();
        String telNum = params.get("telNum").toString();
        String passWord = params.get("newpassword").toString();
        String passCode = params.get("code").toString();
        try {
            if (carNo == null || com.steerinfo.util.StringUtils.isEmpty(carNo)) {
                rm.setMessage("车号不能为空！！");
                return rm;
            } else if (telNum == null || com.steerinfo.util.StringUtils.isEmpty(telNum)) {
                rm.setMessage("手机号不能为空！！");
                return rm;
            } else if (passWord == null || com.steerinfo.util.StringUtils.isEmpty(passWord)) {
                rm.setMessage("新密码不能为空！！");
                return rm;
            } else if(passCode == null || com.steerinfo.util.StringUtils.isEmpty(passCode)) {
                rm.setMessage("验证码不能为空！！");
                return rm;
            }else{
                //通过手机获取验证码列表
                AppVerificationCode modelTemp =  appVerificationCodeMapper.selectByPhone(telNum);
                //手机号验证是否存在
                if(modelTemp!=null){
                    //当前时间是否在过期时间之内
                    Date date = new Date();
                    //验证码是否过期
                    int flag = date.compareTo(modelTemp.getExpirationTime());
                    if(flag > 0 || modelTemp.getValueFlag().equals("0")){
                        rm.setMessage("验证码已过期，请重新发送验证码！！");
                        modelTemp.setValueFlag("0");
                        appVerificationCodeMapper.updateByPrimaryKey(modelTemp);
                        return rm;
                    }
                    //验证码没过期且相等
                    if(modelTemp.getCode().equals(passCode) && modelTemp.getValueFlag().equals("1")){
                        modelTemp.setValueFlag("0");
                        appVerificationCodeMapper.updateByPrimaryKey(modelTemp);
                        // 判断车号，司机手机号是否存在
                        List<MeterBaseCusSupCar> list = meterBaseDriverService.query(params);
                        if (list.size() >= 1) {
                            // SSO修改密码
                            SSOUtil ssoUtil = new SSOUtil();
                            HashMap map = ssoUtil.ssoChangePwd(params);

                            if (map.get("code").toString().equals("0")) {
                            } else {
                                rm.setMessage(map.get("msg").toString());
                            }
                        } else {
                            rm.setMessage("没有此车号或手机号");
                        }
                    }else{
                        return failed(null, "验证码错误");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "操作异常，请确认！");
        }
        return success();
    }

    @ApiOperation(value = "通过手机发送验证码重置密码", notes = "app重置密码")
    @Transactional
    @PostMapping(value = "/appreSetPwdByCode")
    public RESTfulResult appreSetPwdByCode(@RequestBody HashMap params) {
        RESTfulResult rm = failed();

        rm.setMessage("");
        String carNo = params.get("carNo") == null? "": params.get("carNo").toString();
        String telNum = params.get("telNum") == null? "": params.get("telNum").toString();
        String passWord = params.get("password") == null? "": params.get("password").toString();
        String passCode = params.get("code")== null? "": params.get("code").toString();
        try {
            if (carNo == null || com.steerinfo.util.StringUtils.isEmpty(carNo)) {
                rm.setMessage("车号不能为空！！");
                return rm;
            } else if (telNum == null || com.steerinfo.util.StringUtils.isEmpty(telNum)) {
                rm.setMessage("手机号不能为空！！");
                return rm;
            } else if (passWord == null || com.steerinfo.util.StringUtils.isEmpty(passWord)) {
                rm.setMessage("新密码不能为空！！");
                return rm;
            } else if(passCode == null || com.steerinfo.util.StringUtils.isEmpty(passCode)) {
                rm.setMessage("验证码不能为空！！");
                return rm;
            } else {
                HashMap<String, Object> carMap = new HashMap<>();
                carMap.put("carNo", carNo);
                carMap.put("carTypeNo", "001004003");
                List<MeterBaseCar> carModel = meterBaseCarMapper.selectByParameters(carMap);
                if(carModel.size() >= 1) {
                    //润源公司（吉瑞）
                    if(carModel.get(0).getUsetDepartmentNo() != null && StringUtils.isNotEmpty(carModel.get(0).getUsetDepartmentNo())) {
                        if (!carModel.get(0).getUsetDepartmentNo().equals("123456")) {
                            rm.setMessage("内倒车辆不允许重置密码！！");
                            return rm;
                        }
                    }
                }
                HashMap<String, Object> map = new HashMap<>();
                map.put("carNo", carNo);
                map.put("telNum", telNum);
                List<MeterBaseDriver> driverModel = meterBaseDriverMapper.selectByParameters(map);
                if(driverModel.size() >= 1){
                    //通过手机获取验证码列表
                    AppVerificationCode modelTemp =  appVerificationCodeMapper.selectByPhone(telNum);
                    if(modelTemp!=null){
                        //当前时间是否在过期时间之内
                        Date date = new Date();
                        //验证码是否过期
                        int flag = date.compareTo(modelTemp.getExpirationTime());
                        if(flag > 0 || modelTemp.getValueFlag().equals("0")){
                            rm.setMessage("验证码已过期，请重新发送验证码！！");
                            modelTemp.setValueFlag("0");
                            appVerificationCodeMapper.updateByPrimaryKey(modelTemp);
                            return rm;
                        }
                        //验证码没过期且相等
                        if(modelTemp.getCode().equals(passCode) && modelTemp.getValueFlag().equals("1")){
                            modelTemp.setValueFlag("0");
                            appVerificationCodeMapper.updateByPrimaryKey(modelTemp);
                            // 判断车号，司机手机号是否存在
                            List<MeterBaseDriver> list = meterBaseDriverService.query(params);
                            if (list.size() >= 1) {
                                // 获取车辆SSO ID
                                HashMap queryMap = new HashMap();
                                queryMap.put("carNo", params.get("carNo").toString());

                                MeterBaseCar car = meterBaseCarService.getByParams(queryMap);

                                // 通过车辆SSO ID重置密码
                                if (car != null) {
                                    // SSO重置密码
                                    SSOUtil ssoUtil = new SSOUtil();
                                    rm.setMessage(ssoUtil.ssoResetPwd(car.getSsoId(), params.get("password").toString(), ""));
                                }
                            } else {
                                rm.setMessage("没有此车号或手机号");
                            }
                        }
                        if (com.steerinfo.util.StringUtils.isNotEmpty(rm.getMessage())) {
                            return failed(null, rm.getMessage());
                        }
                    }else{
                        rm.setMessage("验证码有误！！");
                        return rm;
                    }
                }else {
                    rm.setMessage("重置密码失败，根据手机号和车号未查询到相应的账号！！");
                    return rm;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "操作异常，请确认！");
        }
        return success();
    }

    @ApiOperation(value = "app重置密码", notes = "app重置密码")
    @Transactional
    @PostMapping(value = "/appresetpwd")
    public RESTfulResult appresetpwd(@RequestBody HashMap params) {

        String msg = "";

        try {
            if (params.get("carNo") == null || com.steerinfo.util.StringUtils.isEmpty(params.get("carNo").toString())) {
                msg = "车号不能为空！！";
            } else if (params.get("telNum") == null || com.steerinfo.util.StringUtils.isEmpty(params.get("telNum").toString())) {
                msg = "手机号不能为空！！";
            } else {
                // 状态（0：服役；1:停役;2:删除）
                params.put("validFlag", "0");

                // 判断车号，司机手机号是否存在
                List<MeterBaseDriver> list = meterBaseDriverService.query(params);
                if (list.size() >= 1) {
                    // 获取车辆SSO ID
                    HashMap queryMap = new HashMap();
                    queryMap.put("carNo", params.get("carNo").toString());

                    MeterBaseCar car = meterBaseCarService.getByParams(queryMap);

                    // 通过车辆SSO ID重置密码
                    if (car != null) {
                        // SSO重置密码
                        SSOUtil ssoUtil = new SSOUtil();
                        msg = ssoUtil.ssoResetPwd(car.getSsoId(), params.get("newpassword").toString(), "");
                    }
                } else {
                    msg = "没有此车号或手机号";
                }
            }

            if (com.steerinfo.util.StringUtils.isNotEmpty(msg)) {
                return failed(null, msg);
            }

        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "操作异常，请确认！");
        }

        return success();
    }

    @ApiOperation(value = "web重置密码", notes = "web重置密码")
    @Transactional
    @PostMapping(value = "/webresetpwd")
    public RESTfulResult webresetpwd(@RequestBody HashMap params) {

        String msg = "";

        try {
            // 获取车辆SSO ID
            HashMap queryMap = new HashMap();
            queryMap.put("carNo", params.get("carNo").toString());

            MeterBaseCar car = meterBaseCarService.getByParams(queryMap);

            // 通过车辆SSO ID重置密码
            if (car != null) {
                // SSO重置密码
                SSOUtil ssoUtil = new SSOUtil();
                msg = ssoUtil.ssoResetPwd(car.getSsoId(), params.get("newpassword").toString(), "");
            }

            if (com.steerinfo.util.StringUtils.isNotEmpty(msg)) {
                return failed(null, msg);
            }

        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "操作异常，请确认！");
        }

        return success();
    }

    @ApiOperation(value = "PC新增车辆", notes = "PC新增车辆")
    @Transactional
    @PostMapping(value = "/addcar")
    public RESTfulResult addcar(@RequestBody HashMap params) {
        RESTfulResult rm = new RESTfulResult();
        rm.setFailed();
        //重置密码信息报错
        String msg = "";
        try {
            String userId = "";
            String userName = "";
            try {
                UserPayload userPayload = UserPayload.getCurrUser();
                userId = userPayload.getUserCode();
                userName = userPayload.getUserName();
            }catch (Exception e) {
                userId = "admin";
                userName = "admins";
            }
            // 检查车辆是否存在
            HashMap query = new HashMap();
            query.put("carNo", params.get("carNo").toString());
            List<MeterBaseCar> list = meterBaseCarService.query(query);
            if (list.size() == 0) {
                // sso注册
                SSOUtil ssoUtil = new SSOUtil();
                HashMap ssoMap = ssoUtil.ssoRegister(params);
                // sso注册成功时，增加车辆表
                if (ssoMap.get("code").equals("1") && false) {
                    return failed(null, ssoMap.get("msg").toString());
                } else {
                    // sso注册成功时，增加车辆表
                    MeterBaseCar car = new MeterBaseCar();
                    // 将车辆类型，载重信息加入实体
                    HashMap<String, Object> obj = new HashMap<>();
                    obj.put("unitNo", params.get("usetDepartment"));
                    List<MeterBaseSpecialUnit> specialUnit = meterBaseSpecialUnitMapper.selectByParameters(obj);
                    if(specialUnit.size() >= 1) {
                        car.setUsetDepartmentNo(specialUnit.get(0).getUnitNo());
                        car.setUsetDepartment(specialUnit.get(0).getUnitName());
                        if(specialUnit.get(0).getUnitNo().equals("888888")){
                           car.setIsSpecialunit("0");
                        }else{
                            car.setIsSpecialunit("1");
                        }
                    }else{
                        msg = "特殊单位表未找到相应的单位";
                    }
                    car.setCarNo(params.get("carNo").toString());
                    car.setCarTypeNo(params.get("carTypeNo").toString());
                    car.setCarTypeName(params.get("carTypeName").toString());
                    car.setValidFlag("0");
                    //自重
                    if(params.get("carSelfWeight") != null ) {
                        if(!StringUtils.isEmpty(params.get("carSelfWeight").toString())) {
                            car.setCarSelfWeight(new BigDecimal(params.get("carSelfWeight").toString()));
                        }
                    }
                    //载重
                    if(params.get("carCarrierWeight") != null ) {
                        if(!StringUtils.isEmpty(params.get("carCarrierWeight").toString())) {
                            car.setCarCarrierWeight(new BigDecimal(params.get("carCarrierWeight").toString()));
                        }
                    }
                    //挂号
                    if(params.get("mnemonicCode") != null ) {
                        if(!StringUtils.isEmpty(params.get("mnemonicCode").toString())) {
                            car.setMnemonicCode(params.get("mnemonicCode").toString());
                        }
                    }
                    //备注
                    if(params.get("memo") != null ) {
                        if(!StringUtils.isEmpty(params.get("memo").toString())) {
                            car.setMemo(params.get("memo").toString());
                        }
                    }
                    //客商编号
                    if(params.get("customerSupplierNo") != null ) {
                        if(!StringUtils.isEmpty(params.get("customerSupplierNo").toString())) {
                            car.setCustomerSupplierNo(params.get("customerSupplierNo").toString());
                            MeterBaseCustomerSupplier supplier = meterBaseCustomerSupplierMapper.selectByPrimaryKey(car.getCustomerSupplierNo());
                            if(supplier != null) {
                                car.setCustomerSupplierName(supplier.getCustomerSupplierName());
                            }
                        }
                    }
                    //客商名称
                    if(params.get("customerSupplierName") != null ) {
                        if(!StringUtils.isEmpty(params.get("customerSupplierName").toString())) {
                            car.setCustomerSupplierName(params.get("customerSupplierName").toString());
                        }
                    }
                    car.setCreateManNo(userId);
                    car.setCreateManName(userName);
                    car.setCreateTime(new Date());
                    // 将sso id写入车辆表，便于以后删除和重置密码
                    if(ssoMap.get("code").equals("1")) {
                        HashMap<String, Object> carTemp = new HashMap<>();
                        carTemp.put("userCode", params.get("carNo").toString());
                        String ssoId = meterBaseCarMapper.ssoIdBySysUser(carTemp);
                        car.setSsoId(ssoId);
                    }else if(ssoMap.get("code").equals("0")) {
                        car.setSsoId(ssoMap.get("data").toString());
                    }
                    msg = meterBaseCarService.checkandadd(car);
                }
            } else {
                // 车辆已存在，更新车辆表
                MeterBaseCar car = list.get(0);
                // 将车辆类型，载重信息加入实体
                car.setCarTypeNo(params.get("carTypeNo").toString());
                car.setCarTypeName(params.get("carTypeName").toString());
                car.setValidFlag("0");
                HashMap<String, Object> obj = new HashMap<>();
                obj.put("unitNo", params.get("usetDepartment"));
                List<MeterBaseSpecialUnit> specialUnit = meterBaseSpecialUnitMapper.selectByParameters(obj);
                if(specialUnit.size() >= 1) {
                    car.setUsetDepartmentNo(specialUnit.get(0).getUnitNo());
                    car.setUsetDepartment(specialUnit.get(0).getUnitName());
                    if(specialUnit.get(0).getUnitNo().equals("888888")){
                        car.setIsSpecialunit("0");
                    }else{
                        car.setIsSpecialunit("1");
                    }
                }else{
                    msg = "特殊单位表未找到相应的单位";
                }
                //自重
                if(params.get("carSelfWeight") != null ) {
                    if(!StringUtils.isEmpty(params.get("carSelfWeight").toString())) {
                        car.setCarSelfWeight(new BigDecimal(params.get("carSelfWeight").toString()));
                    }
                }
                //载重
                if(params.get("carCarrierWeight") != null ) {
                    if(!StringUtils.isEmpty(params.get("carCarrierWeight").toString())) {
                        car.setCarCarrierWeight(new BigDecimal(params.get("carCarrierWeight").toString()));
                    }
                }
                //挂号
                if(params.get("mnemonicCode") != null ) {
                    if(!StringUtils.isEmpty(params.get("mnemonicCode").toString())) {
                        car.setMnemonicCode(params.get("mnemonicCode").toString());
                    }
                }
                //备注
                if(params.get("memo") != null ) {
                    if(!StringUtils.isEmpty(params.get("memo").toString())) {
                        car.setMemo(params.get("memo").toString());
                    }
                }
                //客商编号
                if(params.get("customerSupplierNo") != null ) {
                    if(!StringUtils.isEmpty(params.get("customerSupplierNo").toString())) {
                        car.setCustomerSupplierNo(params.get("customerSupplierNo").toString());
                        MeterBaseCustomerSupplier supplier = meterBaseCustomerSupplierMapper.selectByPrimaryKey(car.getCustomerSupplierNo());
                        if(supplier != null) {
                            car.setCustomerSupplierName(supplier.getCustomerSupplierName());
                        }
                    }
                }
                //客商名称
                if(params.get("customerSupplierName") != null ) {
                    if(!StringUtils.isEmpty(params.get("customerSupplierName").toString())) {
                        car.setCustomerSupplierName(params.get("customerSupplierName").toString());
                    }
                }
                car.setUpdateManNo(userId);
                car.setUpdateManName(userName);
                car.setUpdateTime(new Date());
                // 将sso id写入车辆表，便于以后删除和重置密码
                if (StringUtils.isNotEmpty(car.getSsoId())) {
                    // SSO重置密码
                    SSOUtil ssoUtil = new SSOUtil();
                    msg = ssoUtil.ssoResetPwd(car.getSsoId(), params.get("password").toString(), "");
                }
                meterBaseCarMapper.updateByPrimaryKeySelective(car);
            }
            HashMap<String, Object> obj = new HashMap<>();
            obj.put("carId", params.get("carNo").toString());
            meterBaseDriverMapper.updateValidFlagByCarId(obj);
            if (StringUtils.isNotEmpty(msg)) {
                rm.setMessage("重置密码失败，请确认！！>>>" + msg);
                return rm;
            }
        } catch (Exception e) {
            e.printStackTrace();
            rm.setMessage("操作异常,请确认！！>>>>>" + e.getMessage());
            return rm;
        }
        rm.setSucceed();
        rm.setMessage("添加车辆成功");
        return rm;
    }

    @ApiOperation(value = "PC新增车辆", notes = "PC新增车辆")
    @Transactional
    @PostMapping(value = "/addByCarNo")
    public RESTfulResult addByCarNo(@RequestBody HashMap params) {
        MeterBaseCar car = new MeterBaseCar();
        try {
            String userId = null;
            String userName = null;
            try{
                UserPayload payload = UserPayload.getCurrUser();
                userId = payload.getUserCode();
                userName = payload.getUserName();
            }catch(Exception e) {
                userId = "admin";
                userName = "admins";
            }

            car.setUsetDepartmentNo("");
            car.setUsetDepartment("");
            car.setIsSpecialunit("0");

            String msg = "";
            // 检查车辆是否存在
            HashMap query = new HashMap();
            query.put("carNo", params.get("carNo").toString());
            List<MeterBaseCar> list = meterBaseCarService.query(query);
            if (list.size() == 0) {
                // sso注册
                params.put("password", "a1234567");
                SSOUtil ssoUtil = new SSOUtil();
                HashMap ssoMap = ssoUtil.ssoRegister(params);

                // sso注册成功时，增加车辆表
                if (ssoMap.get("code").equals("1")&&false) {
                    return failed(null, ssoMap.get("msg").toString());
                } else {
                    // sso注册成功时，增加车辆表
                    // 将车辆类型，载重信息加入实体
                    car.setCarNo(params.get("carNo").toString());
                    car.setCarTypeNo(params.get("carTypeNo").toString());
                    car.setCarTypeName(params.get("carTypeName").toString());
                    car.setCreateManNo(userId);
                    car.setCreateManName(userName);
                    car.setCreateTime(new Date());
                    // 将sso id写入车辆表，便于以后删除和重置密码
                    if(ssoMap.get("code").equals("1")) {
                        HashMap<String, Object> carTemp = new HashMap<>();
                        carTemp.put("userCode", params.get("carNo").toString());
                        String ssoId = meterBaseCarMapper.ssoIdBySysUser(carTemp);
                        car.setSsoId(ssoId);
                    }else if(ssoMap.get("code").equals("0")) {
                        car.setSsoId(ssoMap.get("data").toString());
                    }
                    if (car.getSsoId() != null) {
                        // SSO重置密码
                        ssoUtil.ssoResetPwd(car.getSsoId(), "123456", "");
                    }
                    msg = meterBaseCarService.checkandadd(car);
                }
            } else {
                // 车辆已存在，更新车辆表
                car = list.get(0);
                // 将车辆类型，载重信息加入实体
                car.setCarTypeNo(params.get("carTypeNo").toString());
                car.setCarTypeName(params.get("carTypeName").toString());
                car.setValidFlag("0");
                car.setUpdateManNo(userId);
                car.setUpdateManName(userName);
                car.setUpdateTime(new Date());

                // 将sso id写入车辆表，便于以后删除和重置密码
                if (car.getSsoId() != null) {
                    // SSO重置密码
                    SSOUtil ssoUtil = new SSOUtil();
                    if (car.getSsoId() != null && car.getCarTypeNo().equals("001004003")) {
                        // SSO重置密码
                        ssoUtil.ssoResetPwd(car.getSsoId(), "123456", "");
                    }
//                    msg = ssoUtil.ssoResetPwd(car.getSsoId(), params.get("newpassword").toString(), "");
                }
                meterBaseCarMapper.updateByPrimaryKeySelective(car);
            }
            if (com.steerinfo.util.StringUtils.isNotEmpty(msg)) {
                return failed(null, msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "操作异常，请确认！");
        }
        return success(car);
    }

    @ApiOperation(value = "PC新增司机", notes = "PC新增司机")
    @Transactional
    @PostMapping(value = "/adddriver")
    public RESTfulResult addDriver(@RequestBody HashMap params) {
        try {
            String msg = "";
            // 拆分司机list，新增司机表
            List<MeterBaseDriver> driverList = JSONObject.parseArray(params.get("driverList").toString(), MeterBaseDriver.class);
            // 循环新增司机
            for (MeterBaseDriver driver : driverList) {
                HashMap query = new HashMap();
                query.put("driverNo",params.get("carNo") + driver.getTelNum());
                List<MeterBaseDriver> list = meterBaseDriverService.query(query);
                // 检查司机是否存在
                if (list.size() >= 1) {
                    // 主键改为车号+电话号码
                    driver.setDriverNo(params.get("carNo")+driver.getTelNum());
                    driver.setCarId(params.get("carNo").toString());
                    UserPayload payload = UserPayload.getCurrUser();
                    String userId = payload.getUserCode();
                    String userName = payload.getUserName();
                    driver.setUpdateManNo(userId);
                    driver.setUpdateManName(userName);
                    driver.setUpdateTime(new Date());
                    driver.setValidFlag("0");//服役
                    meterBaseDriverMapper.updateByPrimaryKeySelective(driver);
                }else {
                    driver.setCarId(params.get("carNo").toString());
                    meterBaseDriverService.checkandadd(driver);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "操作异常，请确认！");
        }
        return success();
    }


    @ApiOperation(value = "批量删除", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("meterbasecar:delete")
    @PutMapping(value = "/deleteBatch")//String
    public RESTfulResult deleteBatch(@RequestBody List<MeterBaseCar> model) {
        try {
            List<String> list = new ArrayList<>();
            for(MeterBaseCar models : model){
                list.add(models.getCarNo());
            }
            meterBaseDriverMapper.deleteCarById(list, UserUtil.getCurrentUserId(), UserUtil.getCurrentUserName());
            meterBaseCarMapper.deleteCarById(list, UserUtil.getCurrentUserId(), UserUtil.getCurrentUserName());
            return success();
        }
        catch (Exception e){
            e.printStackTrace();
            return failed(null,"操作异常，请确认");
        }
    }

    @ApiOperation(value = "根据车辆直接废除", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("meterbasecar:delete")
    @PostMapping(value = "/deleteByCarNo")//String
    public RESTfulResult deleteByCarNo(@RequestBody Map carNo) {
        try {
            List<String> list = new ArrayList<>();
            list.add(carNo.get("carNo").toString());

            UserUtil.getCurrentUserId();
            meterBaseDriverMapper.deleteCarById(list,UserUtil.getCurrentUserId(),UserUtil.getCurrentUserName());
            meterBaseCarMapper.deleteCarById(list,UserUtil.getCurrentUserId(),UserUtil.getCurrentUserName());
            return success();
        }
        catch (Exception e){
            e.printStackTrace();
            return failed(null,"操作异常，请确认");
        }
    }

    @ApiOperation(value = "根据车辆直接启用", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(paramType = "path", name = "id", value = "ID", required = true, dataType = "String")
    //@RequiresPermissions("meterbasecar:delete")
    @PostMapping(value = "/enableByCarNo")//String
    public RESTfulResult enableByCarNo(@RequestBody Map carNo) {
        try {
            List<String> list = new ArrayList<>();
            list.add(carNo.get("carNo").toString());
            meterBaseDriverMapper.enableCarById(list);
            meterBaseCarMapper.enableCarById(list);
            return success();
        }
        catch (Exception e){
            e.printStackTrace();
            return failed(null,"操作异常，请确认");
        }
    }


    @ApiOperation(value = "批量车辆导入", notes = "批量车辆导入")

    @PostMapping(value = "/batchAddCar")
    public RESTfulResult batchAddCar(@RequestBody List<HashMap> model) {
        RESTfulResult rm = failed();
        try {
            rm = meterBaseCarService.batchAddCar(model);
            return rm;
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return failed(null, "操作异常，请确认！");
        }
    }

    @ApiOperation(value = "获取计量结果", notes = "客商来获取详细计量信息")
    //@RequiresPermissions("meterbasecar:view")
    @PostMapping(value = "/getCarActual")
    public RESTfulResult getCarActual(@RequestBody CommonPage<MeterBaseCar> oci) {
        try {
            RESTfulResult rm = meterBaseCarService.getCarActual(oci);
            return rm;
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "根据车号是否是特殊单位信息", notes = "根据车号是否是特殊单位信息")
    //@RequiresPermissions("meterbasecar:view")
    @PostMapping(value = "/getUnitByCar")
    public RESTfulResult getUnitByCar(@RequestBody Map params) {
        try {
            String carNo = params.get("carNo") == null ? "" : params.get("carNo").toString();
            RESTfulResult rm = meterBaseCarService.getUnitByCar(carNo);
            return rm;
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }

    @ApiOperation(value = "主键ID重新生成", notes = "主键ID重新生成")
    //@RequiresPermissions("meterbasecar:view")
    @PostMapping(value = "/reNewID")
    public RESTfulResult newId() {
        try {
            HashMap<String, Object> map = new HashMap<>();
            map.put("validFlag", "0");
            List<MeterBaseCar> carList = meterBaseCarMapper.selectByParameters(map);
            int count = 0;
            if(carList.size() >= 1) {
                int i = 0;

                for(MeterBaseCar car: carList) {
                    i++;
                    DecimalFormat decimalFormat = new DecimalFormat("00000");
                    String numFormat= decimalFormat .format(i);
                    car.setPkNo("20220517"+numFormat);

                    int j = meterBaseCarMapper.reNewID(car);
                    if(j >= 1) {
                        count ++;
                    }
                }
            }
            RESTfulResult rm = success();
            rm.setData(count);
            return rm;
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new MarkerMetException(500, "操作异常！！");
        }
    }


}
