VehicleController.java 18.5 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
package cn.com.glsx.admin.modules.controller;

import cn.com.glsx.admin.common.constants.Constants;
import cn.com.glsx.admin.common.enums.ResultEnum;
import cn.com.glsx.admin.common.exception.AdminException;
import cn.com.glsx.admin.modules.model.*;
import cn.com.glsx.admin.modules.model.export.TrialDriveVehicleExport;
import cn.com.glsx.admin.modules.model.export.VehicleInfoReportExport;
import cn.com.glsx.admin.modules.model.imports.TrialDriveVehicleImport;
import cn.com.glsx.admin.modules.model.imports.TrialDriveVehicleImportListener;
import cn.com.glsx.admin.modules.model.imports.VehicleInfoImport;
import cn.com.glsx.admin.modules.model.imports.VehicleInfoImportListener;
import cn.com.glsx.admin.modules.model.param.*;
import cn.com.glsx.admin.modules.service.DeviceService;
import cn.com.glsx.admin.modules.service.VehicleService;
import cn.com.glsx.admin.modules.utils.FileIOUtil;
import cn.com.glsx.auth.utils.ShieldContextHolder;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import com.glsx.biz.common.base.entity.*;
import com.glsx.plat.common.annotation.SysLog;
import com.glsx.plat.common.enums.OperateType;
import com.glsx.plat.common.utils.DateUtils;
import com.glsx.plat.common.utils.StringUtils;
import com.glsx.plat.core.web.R;
import com.glsx.plat.office.excel.EasyExcelUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Desc:车辆管理
 * @Author huangxc
 * @Date 2021/5/19
 */
@Slf4j
@RestController
@RequestMapping(value = "/vehicle")
@Api(value = "车辆管理", tags = {"车辆管理"})
public class VehicleController {

    private final static String MODULE = "试驾车辆管理";

    @Autowired
    private VehicleService vehicleService;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private HttpServletResponse response;

    @ApiOperation(value = "试驾车辆列表查询")
    @GetMapping("/drivevehicle/search")
    public R search(VehicleManageSearch search) {
        PageInfo<TrialDriveVehiclePageDTO> pageInfo = vehicleService.searchTrialDrive(search);
        return R.ok().putPageData(pageInfo);
    }

    @ApiOperation(value = "查询该组织下没有绑定为试驾车辆的列表")
    @SysLog(module = MODULE, value = "查询该组织下没有绑定为试驾车辆的列表", action = OperateType.QUERY)
    @PostMapping("/drivevehicle/listtestdrive")
    public R listTestDriveVehicle() {
        List<TestVehicleVo> testDriveVehicleList = vehicleService.listTestDriveVehicle();
        return R.ok().data(testDriveVehicleList);
    }

    @ApiOperation(value = "新增绑定试驾车辆")
    @SysLog(module = MODULE, value = "新增绑定试驾车辆", action = OperateType.ADD)
    @PostMapping("/drivevehicle/addtestdrive")
    public R addTestDriveVehicle(@RequestParam List<Long> vehicleIds) {
        vehicleService.addTestDriveVehicle(vehicleIds);
        return R.ok();
    }

    @ApiOperation(value = "新增试驾车辆")
    @SysLog(module = MODULE, value = "新增试驾车辆", action = OperateType.ADD)
    @PostMapping("/drivevehicle/add")
    public R addDriveVehicle(@RequestBody @Validated(ValidateGroups.Insert.class) TrialDriveVehicleBo driveVehicleBo) {
        vehicleService.addDriveVehicle(driveVehicleBo);
        return R.ok();
    }

    @ApiOperation(value = "根据id查询某条试驾车辆数据(数据回显)")
    @GetMapping("/drivevehicle/findbyid")
    public R findDriveVehicleById(@RequestParam(required = true) String tempId) {
        TrialDriveVehicleEditDTO vehicleEditDTO = vehicleService.findDriveVehicleById(tempId);
        return R.ok().data(vehicleEditDTO);
    }

    @ApiOperation(value = "编辑试驾车辆")
    @SysLog(module = MODULE, value = "编辑试驾车辆", action = OperateType.EDIT)
    @PostMapping("/drivevehicle/update")
    public R updateDriveVehicle(@RequestBody @Validated(ValidateGroups.Update.class) TrialDriveVehicleEditBo driveVehicleoBo) {
        vehicleService.updateDriveVehicleById(driveVehicleoBo);
        return R.ok();
    }

    @ApiOperation(value = "设备和车辆解绑")
    @GetMapping("/unbindSn")
    public R unbindDriveVehicle(@RequestParam(required = true) String tempId) {
        vehicleService.unbindDriveVehicle(tempId);
        return R.ok();
    }

    @ApiOperation(value = "删除试驾车辆数据")
    @GetMapping("/drivevehicle/delete")
    @SysLog(module = MODULE, value = "删除试驾车辆数据", action = OperateType.DELETE)
    public R deleteDriveVehicle(@RequestParam(required = true) String tempId) {
        if (StringUtils.isBlank(tempId)) {
            return R.error("未选择数据");
        }
        vehicleService.deleteDriveVehicleAndUnBind(tempId);
        return R.ok();
    }

    @ApiOperation(value = "导出试驾车辆数据")
    @GetMapping("/drivevehicle/export")
    public void exportDriveVehicle(VehicleManageSearch search) throws Exception {
        List<TrialDriveVehicleExport> list = vehicleService.exportDriveVehicle(search);
        String fileName = "试驾车辆_" + DateUtils.formatSerial(new Date());
        String sheetName = "Sheet1";
        EasyExcelUtils.writeExcel(response, list, fileName, sheetName, TrialDriveVehicleExport.class);
    }

    @ApiOperation(value = "导入试驾车辆数据")
    @PostMapping(value = "/drivevehicle/import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R importDriveVehicle(@RequestParam("file") MultipartFile file) throws IOException {
        TrialDriveVehicleImportListener listener = new TrialDriveVehicleImportListener(vehicleService, deviceService);
        EasyExcel.read(file.getInputStream(), TrialDriveVehicleImport.class, listener)
                .head(TrialDriveVehicleImport.class)
                .ignoreEmptyRow(false)
                .sheet().doRead();
        BaseImportInfo importInfo = listener.getBaseImportInfo();
        return R.ok().data(importInfo);
    }

    /**
     * 试驾车辆管理-下载车辆导入模板
     *
     * @throws Exception
     */
    @ApiOperation(value = "下载车辆管理导入模板")
    @GetMapping(value = "/drivevehicle/downloadtemplate")
    public void downloadTemplate() throws Exception {
        InputStream inputStream = VehicleController.class.getResourceAsStream("/templates/importVehicle.xlsx");
        String fileName = "车辆管理导入模板";
        FileIOUtil.download(inputStream, FileIOUtil.MimeType.EXCEL2007, fileName, response);
    }

    @ApiOperation(value = "下载导入失败的文件")
    @GetMapping(value = "/drivevehicle/downloadFailFile")
    public void downloadFailFile(String failSeqNoFile) throws Exception {
        InputStream inputStream = vehicleService.downloadFailExcelFile(failSeqNoFile);
        String fileName = "试驾车辆_" + DateUtils.formatSerial(new Date());
        FileIOUtil.download(inputStream, FileIOUtil.MimeType.EXCEL2007, fileName, response);
    }

    @PostMapping(value = "/drivevehicle/syncVehicleInfo", consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @SysLog(module = MODULE, value = "同步车辆信息", action = OperateType.API, saveLog = false)
    @ApiOperation(value = "同步车辆信息")
    public R syncVehicleDeviceInfo(@RequestBody @Validated(ValidateGroups.Insert.class) SyncVehicleBo vehicleBo) {
        try {
            String chanle = vehicleBo.getChanle();
            if (StringUtils.isNotBlank(chanle) && Constants.SYNC_CHANNEL_TIMED_TASK.equals(chanle)) {
                vehicleService.syncVehicleDeviceLinkFence(vehicleBo);
            } else {
                return R.error("同步渠道不正确。chanle:" + chanle);
            }
        } catch (Exception e) {
            log.info("同步车辆信息 异常:message:" + e.getMessage());
            return R.error(e.getMessage());
        }
        return R.ok();
    }

    @ApiOperation(value = "车辆设备关联列表查询")
    @GetMapping("/linksns/search")
    public R search(VehicleSearch search) {
        PageInfo<VehicleLinkSnsDTO> pageInfo = vehicleService.linkSnsSearch(search);
        return R.ok().putPageData(pageInfo);
    }

    @ApiOperation(value = "车型管理列表查询")
    @GetMapping("/tenanttypes/search")
    public R tenantTypesSearch(TenantTypesSearch search) {
        PageInfo<TenantTypesDTO> pageInfo = vehicleService.tenantTypesSearch(search);
        return R.ok().putPageData(pageInfo);
    }

    @SysLog(module = "车型库管理", value = "删除车型库", action = OperateType.DELETE)
    @ApiOperation(value = "删除车型库")
    @GetMapping("/tenanttypes/delete")
    public R deleteTenantTypes(@RequestParam Long tenantId) {
        vehicleService.deleteTenantTypes(tenantId);
        return R.ok();
    }

    @ApiOperation(value = "车辆品牌系别查询")
    @GetMapping("/brandseries")
    public R<List<CarBrandSelected>> brandsSeries(@RequestParam("full") Boolean full) {
        List<CarBrandSelected> carBrandSeriesList = vehicleService.getCarBrandSeriesList(full);
        return R.ok().data(carBrandSeriesList);
    }

    @ApiOperation(value = "车辆品牌查询")
    @GetMapping("/brands")
    public R<List<Carbrand>> brands() {
        List<Carbrand> carBrandList = vehicleService.getAllBrandList();
        return R.ok().data(carBrandList);
    }

    @ApiOperation(value = "车辆子品牌查询")
    @GetMapping("/subbrands")
    public R<List<Carbrand>> subBrands(@RequestParam Integer brandId) {
        List<Carbrand> carBrandList = vehicleService.getSubBrandsByBrandId(brandId);
        return R.ok().data(carBrandList);
    }

    @ApiOperation(value = "车辆系别查询")
    @GetMapping("/series")
    public R<List<Carseries>> series(@RequestParam Integer brandId) {
        List<Carseries> carSeriesList = vehicleService.getSeriesByBrandId(brandId);
        return R.ok().data(carSeriesList);
    }

    @ApiOperation(value = "车辆类型查询")
    @GetMapping("/types")
    public R<List<Cartype>> types(@RequestParam Integer seriesId) {
        List<Cartype> carTypeList = vehicleService.getTypesBySeriesId(seriesId);
        return R.ok().data(carTypeList);
    }

    @SysLog(module = "车型库管理", value = "保存车型库", action = OperateType.ADD)
    @ApiOperation(value = "保存车型库")
    @PostMapping("/savetypes")
    public R<List<Cartype>> saveVehicleTypes(@RequestBody VehicleTypesBO typesBO) {

        TenantTypesDTO typesDTO = vehicleService.getTenantTypes(typesBO.getTenantId());
        //新增且车型库已经存在
        if (typesBO.getFrom() == 1 && typesDTO != null) {
            throw new AdminException(ResultEnum.TENANT_VEHICLE_TYPES_EXISTED);
        }

        vehicleService.saveVehicleTypes(typesBO, ShieldContextHolder.getUserId());

        return R.ok();
    }

    @ApiOperation(value = "获取组织配置的车型库")
    @GetMapping("/gettenanttypes")
    public R<TenantTypesDTO> getTenantTypes(@RequestParam Long tenantId) {
        TenantTypesDTO typesDTO = vehicleService.getTenantTypes(tenantId);
        return R.ok().data(typesDTO);
    }

    @ApiOperation(value = "获取车型库配置树")
    @GetMapping("/getSelectedTypeTree")
    public R<List<CarInfoSelected>> getTypeSelectedTree() {
        List<CarInfoSelected> selectedTree = vehicleService.getTypeSelectedTree(ShieldContextHolder.getVisibleTenantIds());
        return R.ok().data(selectedTree);
    }

    /**
     * 车辆信息表
     *
     * @param search 搜索条件
     * @return 统一返回结果
     */
    @ApiOperation(value = "车辆信息列表查询")
    @GetMapping("/vehicleinfo/search")
    public R vehicleInfoSearch(VehicleInfoSearch search) {
        PageInfo<VehicleInfoPageDTO> pageInfo = vehicleService.searchVehicleInfo(search);
        return R.ok().putPageData(pageInfo);
    }

    @ApiOperation(value = "导出车辆信息列表")
    @GetMapping("/vehicleinfo/export")
    public void exportVehicleInfo(VehicleInfoSearch search) throws Exception {
        List<VehicleInfoReportExport> vehicleInfoList = vehicleService.exportVehicleInfo(search);
        String fileName = "车辆信息列表_" + DateUtils.formatSerial(new Date());
        String sheetName = "Sheet1";
        EasyExcelUtils.writeExcel(response, vehicleInfoList, fileName, sheetName, VehicleInfoReportExport.class);
    }

    @ApiOperation(value = "导入车辆信息列表")
    @PostMapping(value = "/vehicleinfo/import", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R importVehicleInfo(@RequestParam("file") MultipartFile file) throws IOException {
        VehicleInfoImportListener listener = new VehicleInfoImportListener(vehicleService, deviceService);
        EasyExcel.read(file.getInputStream(), VehicleInfoImport.class, listener)
                .head(VehicleInfoImport.class)
                .ignoreEmptyRow(false)
                .sheet().doRead();
        BaseImportInfo importInfo = listener.getBaseImportInfo();
        return R.ok().data(importInfo);
    }

    @ApiOperation(value = "下载车辆信息导入模板")
    @GetMapping(value = "/vehicleinfo/downloadtemplate")
    public void downloadVehicleInfoTemplate() throws Exception {
        InputStream inputStream = VehicleController.class.getResourceAsStream("/templates/importVehicleInfo.xlsx");
        String fileName = "车辆信息导入模板";
        FileIOUtil.download(inputStream, FileIOUtil.MimeType.EXCEL2007, fileName, response);
    }

    @ApiOperation(value = "新增车辆信息")
    @SysLog(value = "新增试驾车辆", action = OperateType.ADD)
    @PostMapping("/vehicleinfo/add")
    public R addVehicleInfo(@RequestBody @Validated(ValidateGroups.Insert.class) VehicleInfoVo vehicleInfoVo) {
        List<String> noExistSnList = new ArrayList<>();
        boolean snExist = deviceService.checkSnExist(vehicleInfoVo.getSn());
        if (!snExist) {
            noExistSnList.add(vehicleInfoVo.getSn());
        }
        boolean snExist1 = deviceService.checkSnExist(vehicleInfoVo.getSn1());
        if (!snExist1) {
            noExistSnList.add(vehicleInfoVo.getSn1());
        }
        boolean snExist2 = deviceService.checkSnExist(vehicleInfoVo.getSn2());
        if (!snExist2) {
            noExistSnList.add(vehicleInfoVo.getSn2());
        }
        boolean snExist3 = deviceService.checkSnExist(vehicleInfoVo.getSn3());
        if (!snExist3) {
            noExistSnList.add(vehicleInfoVo.getSn3());
        }
        boolean snExist4 = deviceService.checkSnExist(vehicleInfoVo.getSn4());
        if (!snExist4) {
            noExistSnList.add(vehicleInfoVo.getSn4());
        }
        boolean snExist5 = deviceService.checkSnExist(vehicleInfoVo.getSn5());
        if (!snExist5) {
            noExistSnList.add(vehicleInfoVo.getSn5());
        }
        boolean snExist6 = deviceService.checkSnExist(vehicleInfoVo.getSn6());
        if (!snExist6) {
            noExistSnList.add(vehicleInfoVo.getSn6());
        }
        boolean snExist7 = deviceService.checkSnExist(vehicleInfoVo.getSn7());
        if (!snExist7) {
            noExistSnList.add(vehicleInfoVo.getSn7());
        }
        boolean snExist8 = deviceService.checkSnExist(vehicleInfoVo.getSn8());
        if (!snExist8) {
            noExistSnList.add(vehicleInfoVo.getSn8());
        }
        boolean snExist9 = deviceService.checkSnExist(vehicleInfoVo.getSn9());
        if (!snExist9) {
            noExistSnList.add(vehicleInfoVo.getSn9());
        }

        if (CollectionUtil.isEmpty(noExistSnList)) {
            vehicleService.addVehicleInfo(vehicleInfoVo);
            return R.ok();
        } else {
            return R.error(StringUtils.join(noExistSnList, ",") + "设备不存在;");
        }
    }

    @ApiOperation(value = "编辑车辆信息")
    @SysLog(value = "编辑车辆信息", action = OperateType.EDIT)
    @PostMapping("/vehicleinfo/update")
    public R updateVehicleInfo(@RequestBody @Validated(ValidateGroups.Update.class) VehicleInfoVo vehicleInfoVo) {
        List<String> noExistSnList = new ArrayList<>();
        boolean snExist = deviceService.checkSnExist(vehicleInfoVo.getSn());
        if (!snExist) {
            noExistSnList.add(vehicleInfoVo.getSn());
        }
        boolean snExist1 = deviceService.checkSnExist(vehicleInfoVo.getSn1());
        if (!snExist1) {
            noExistSnList.add(vehicleInfoVo.getSn1());
        }
        boolean snExist2 = deviceService.checkSnExist(vehicleInfoVo.getSn2());
        if (!snExist2) {
            noExistSnList.add(vehicleInfoVo.getSn2());
        }
        boolean snExist3 = deviceService.checkSnExist(vehicleInfoVo.getSn3());
        if (!snExist3) {
            noExistSnList.add(vehicleInfoVo.getSn3());
        }
        boolean snExist4 = deviceService.checkSnExist(vehicleInfoVo.getSn4());
        if (!snExist4) {
            noExistSnList.add(vehicleInfoVo.getSn4());
        }
        boolean snExist5 = deviceService.checkSnExist(vehicleInfoVo.getSn5());
        if (!snExist5) {
            noExistSnList.add(vehicleInfoVo.getSn5());
        }
        boolean snExist6 = deviceService.checkSnExist(vehicleInfoVo.getSn6());
        if (!snExist6) {
            noExistSnList.add(vehicleInfoVo.getSn6());
        }
        boolean snExist7 = deviceService.checkSnExist(vehicleInfoVo.getSn7());
        if (!snExist7) {
            noExistSnList.add(vehicleInfoVo.getSn7());
        }
        boolean snExist8 = deviceService.checkSnExist(vehicleInfoVo.getSn8());
        if (!snExist8) {
            noExistSnList.add(vehicleInfoVo.getSn8());
        }
        boolean snExist9 = deviceService.checkSnExist(vehicleInfoVo.getSn9());
        if (!snExist9) {
            noExistSnList.add(vehicleInfoVo.getSn9());
        }

        if (CollectionUtil.isEmpty(noExistSnList)) {
            vehicleService.updateVehicleInfo(vehicleInfoVo);
            return R.ok();
        } else {
            return R.error(StringUtils.join(noExistSnList, ",") + "设备不存在");
        }
    }

    @ApiOperation(value = "根据id查询某条车辆信息(数据回显)")
    @SysLog(value = "查询车辆信息", action = OperateType.QUERY)
    @GetMapping("/vehicleinfo/findbyvehicleid")
    public R findVehicleInfoById(@RequestParam Long vehicleId) {
        VehicleInfoVo vehicleInfoVo = vehicleService.findVehicleInfoById(vehicleId);
        return R.ok().data(vehicleInfoVo);
    }
}