DriverHealthyController.java 6.68 KB
package com.ruoyi.controller.driver.healthy;

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.driver.healthy.DriverHealthy;
import com.ruoyi.domain.driver.healthy.dto.DriverHealthyAddDTO;
import com.ruoyi.domain.driver.healthy.dto.DriverHealthyQueryDTO;
import com.ruoyi.domain.driver.healthy.dto.DriverHealthyUpdateDTO;
import com.ruoyi.domain.driver.healthy.dto.DriverHealthyUpdateStatusDTO;
import com.ruoyi.domain.driver.healthy.vo.DriverHealthyVO;
import com.ruoyi.service.driver.healthy.DriverHealthyService;
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.Date;
import java.util.List;

@RestController
@RequestMapping("driver/healthy")
@Api(tags="驾驶员健康信息")
public class DriverHealthyController extends BaseController {
    @Autowired
    private DriverHealthyService driverHealthyService;

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

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


    @GetMapping(value = "/view/{id}")
    @ApiOperation("根据ID查看详情")
    public ResponseResult view(@PathVariable("id") Long id, org.springframework.ui.Model model) {
        DriverHealthy source = driverHealthyService.getById(id);

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


    @PreAuthorize("@ss.hasPermi('driver:healthy:export')")
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, @RequestBody DriverHealthyQueryDTO request) {
        request.clearStrEmpty();
        DriverHealthy entity = convert(request);
        List<DriverHealthy> list = driverHealthyService.list(entity);
        ExcelUtil<DriverHealthy> util = new ExcelUtil<DriverHealthy>(DriverHealthy.class);
        util.exportExcel(response, list, "DriverHealthy");
    }

    @PreAuthorize("@ss.hasPermi('driver:healthy:add')")
    @PostMapping(value = "/add")
    @ApiOperation("添加 ")
    public ResponseResult add(@ModelAttribute DriverHealthyAddDTO request) {
        request.clearStrEmpty();
        DriverHealthy entity = convert(request);
        entity.setCreateBy(getUserId());
        entity.setCreateTime(new Date());
        int count = driverHealthyService.insertSelective(entity);
        return count > 0 ? ResponseResult.success(Boolean.TRUE) : ResponseResult.error("添加数据失败,请稍后再试");
    }

    @PreAuthorize("@ss.hasPermi('driver:healthy:update')")
    @PostMapping(value = "/update")
    @ApiOperation("修改 ")
    public ResponseResult update(@ModelAttribute DriverHealthyUpdateDTO request) {
        request.clearStrEmpty();
        DriverHealthy entity = convert(request);
        entity.setUpdateBy(getUserId());
        entity.setUpdateTime(new Date());
        boolean flag = driverHealthyService.updateByPrimaryKey(entity);
        return flag ? ResponseResult.success(Boolean.TRUE) : ResponseResult.error("修改数据失败,请稍后再试");
    }

    @PreAuthorize("@ss.hasPermi('driver:healthy:update:status')")
    @PostMapping(value = "/update/status")
    @ApiOperation("修改状态 ")
    public ResponseResult updateState(@ModelAttribute DriverHealthyUpdateStatusDTO request) {
        request.clearStrEmpty();
        DriverHealthy entity = convert(request);
        boolean flag = driverHealthyService.updateByPrimaryKey(entity);
        return flag ? ResponseResult.success(Boolean.TRUE) : ResponseResult.error("修改数据失败,请稍后再试");
    }

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

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

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

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

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

    private List<DriverHealthyVO> convert(List<DriverHealthy> 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<DriverHealthyVO> convert(IPage<DriverHealthy> sources) {
        return java.util.Optional.ofNullable(sources).map(scs -> {
            IPage<DriverHealthyVO> target = new Page();
            BeanUtils.copyProperties(scs, target);
            List<DriverHealthyVO> voNames = convert(scs.getRecords());
            target.setRecords(voNames);

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