CarInfoController.java 6.48 KB
package com.ruoyi.controller.carinfo;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.ResponseResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.OrderEntity;
import com.ruoyi.domain.caiinfo.CarInfo;
import com.ruoyi.domain.caiinfo.dto.CarInfoAddDTO;
import com.ruoyi.domain.caiinfo.dto.CarInfoQueryDTO;
import com.ruoyi.domain.caiinfo.dto.CarInfoUpdateDTO;
import com.ruoyi.domain.caiinfo.vo.CarInfoVO;
import com.ruoyi.service.carinfo.CarInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;
import java.util.stream.Collectors;

@RestController
@RequestMapping("car/info")
@Api(tags="车辆信息")
public class CarInfoController extends BaseController {
    @Autowired
    private CarInfoService CarInfoService;

    @PreAuthorize("@ss.hasPermi('car:info:list:limit:page:limit')")
    @ApiOperation("分页查询")
    @PostMapping(value = "/list/limit/{page}/{pageLimit}")
    public String listLimit(@ModelAttribute CarInfoQueryDTO request, OrderEntity orderEntity, @PathVariable Integer page, @PathVariable Integer pageLimit, org.springframework.ui.Model model) {
        CarInfo entity = convert(request);
        IPage<CarInfo> response = CarInfoService.pageList(new Page<CarInfo>(page, pageLimit), entity,orderEntity);

        return JSON.toJSONString(convert(response));
    }

    @ApiOperation("根据ID查询详情")
    @GetMapping(value = "/view/{id}")
    public ResponseResult<CarInfoVO> view(@PathVariable("id") Integer id, org.springframework.ui.Model model) {
        CarInfo source = CarInfoService.getById(id);

        return ResponseResult.success(convert(source));
    }



    @PreAuthorize("@ss.hasPermi('car:info:export')")
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, CarInfo entity) {
        List<CarInfo> list = CarInfoService.list(entity);
        ExcelUtil<CarInfo> util = new ExcelUtil<CarInfo>(CarInfo.class);
        util.exportExcel(response, list, "CarInfo");
    }


    @PostMapping(value = "list/select")
    @ApiOperation("车辆列表(页面选择)")
    public ResponseResult<List<CarInfoVO>> listSelect(CarInfoQueryDTO dto) {
        CarInfo entity = convert(dto);
        List<CarInfo> selectList = CarInfoService.listOfSelect(entity);
        return ResponseResult.success(convert(selectList));
    }

    @GetMapping(value = "list/select/status")
    @ApiOperation("车辆状态选择列表(页面选择)")
    public ResponseResult<List<Map<String,Object>>> listSelect() {
        List<Map<String,Object>> results = Arrays.stream(CarInfo.CarStatusEnum.values()).map(c->{
            Map<String,Object> map = new HashMap<>();
            map.put("label",c.getLabel());
            map.put("value",c.getValue());

            return map;
        }).collect(Collectors.toList());
        return ResponseResult.success(results);
    }

    @PreAuthorize("@ss.hasPermi('car:info:add')")
    @PostMapping(value = "/add")
    @ApiOperation("添加车辆信息")
    public ResponseResult add(@ModelAttribute CarInfoAddDTO request) {
        CarInfo entity = convert(request);
        entity.setCreateBy(getUserId());
        entity.setCreateTime(new Date());
        int count = CarInfoService.insertSelective(entity);
        return count > 0 ? ResponseResult.success(Boolean.TRUE) : ResponseResult.error("添加数据失败,请稍后再试");
    }

    @PreAuthorize("@ss.hasPermi('car:info:update')")
    @PostMapping(value = "/update")
    @ApiOperation("修改车辆信息")
    public ResponseResult update(@ModelAttribute CarInfoUpdateDTO request) {
        CarInfo entity = convert(request);
        entity.setUpdateBy(getUserId());
        entity.setUpdateTime(new Date());
        boolean flag = CarInfoService.updateByPrimaryKey(entity);
        return flag ? ResponseResult.success(Boolean.TRUE) : ResponseResult.error("修改数据失败,请稍后再试");
    }

    @PreAuthorize("@ss.hasPermi('car:info:del')")
    @GetMapping(value = "/del/{id}")
    @ApiOperation("删除车辆信息")
    public ResponseResult delById(@PathVariable("id") Integer id) {
        boolean flag = CarInfoService.deleteById(id);
        return flag ? ResponseResult.success(Boolean.TRUE) : ResponseResult.error("操作数据失败,请稍后再试");
    }

    private CarInfo convert(CarInfoQueryDTO source) {
        return java.util.Optional.ofNullable(source).map(sc -> {
            CarInfo target = new CarInfo();
            BeanUtils.copyProperties(sc, target);
            return target;
        }).orElse(null);
    }

    private CarInfo convert(CarInfoUpdateDTO source) {
        return java.util.Optional.ofNullable(source).map(sc -> {
            CarInfo target = new CarInfo();
            BeanUtils.copyProperties(sc, target);
            return target;
        }).orElse(null);
    }


    private CarInfo convert(CarInfoAddDTO source) {
        return java.util.Optional.ofNullable(source).map(sc -> {
            CarInfo target = new CarInfo();
            BeanUtils.copyProperties(sc, target);
            return target;
        }).orElseGet(null);
    }

    private CarInfoVO convert(CarInfo source) {
        return java.util.Optional.ofNullable(source).map(sc -> {
            CarInfoVO target = new CarInfoVO();
            BeanUtils.copyProperties(source, target);
            return target;
        }).orElseGet(null);
    }

    private List<CarInfoVO> convert(List<CarInfo> sources) {
        return java.util.Optional.ofNullable(sources).map(scs -> {
            return scs.stream().map(source -> {
                return convert(source);
            }).collect(java.util.stream.Collectors.toList());
        }).orElseGet(null);
    }

    private IPage<CarInfoVO> convert(IPage<CarInfo> sources) {
        return java.util.Optional.ofNullable(sources).map(scs -> {
            IPage<CarInfoVO> target = new Page();
            BeanUtils.copyProperties(scs, target);
            List<CarInfoVO> voNames = convert(scs.getRecords());
            target.setRecords(voNames);

            return target;
        }).orElseGet(null);
    }
}