KeyInfoController.java 7.85 KB
package com.ruoyi.controller.keyinfo;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.domain.OrderEntity;
import com.ruoyi.domain.keyInfo.KeyInfo;
import com.ruoyi.domain.keyInfo.dto.KeyInfoAddDTO;
import com.ruoyi.domain.keyInfo.dto.KeyInfoQueryDTO;
import com.ruoyi.domain.keyInfo.dto.KeyInfoUpdateDTO;
import com.ruoyi.domain.keyInfo.dto.KeyInfoUpdateStatusDTO;
import com.ruoyi.domain.keyInfo.vo.KeyInfoVO;
import com.ruoyi.equipment.domain.Equipment;
import com.ruoyi.equipment.service.IEquipmentService;
import com.ruoyi.service.carinfo.CarInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.runtime.options.Option;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.BeanUtils;

import org.springframework.security.access.prepost.PreAuthorize;

import java.util.*;
import java.util.stream.Collectors;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.alibaba.fastjson2.JSON;

import com.ruoyi.common.utils.poi.ExcelUtil;

import javax.servlet.http.HttpServletResponse;


@RestController
@RequestMapping("key/info")
@Api(tags = "钥匙管理")
public class KeyInfoController {
    @Autowired
    private com.ruoyi.service.keyinfo.KeyInfoService KeyInfoService;
    @Autowired
    private IEquipmentService equipmentService;

    @PreAuthorize("@ss.hasPermi('key:info:list:limit:page:limit')")
    @PostMapping(value = "/list/limit/{page}/{pageLimit}")
    @ApiOperation("分页查询")
    public String listLimit(@ModelAttribute KeyInfoQueryDTO request, @ModelAttribute OrderEntity orderEntity, @PathVariable Integer page, @PathVariable Integer pageLimit, org.springframework.ui.Model model) {
        request.clearStrEmpty();
        KeyInfo entity = convert(request);
        IPage<KeyInfo> response = KeyInfoService.pageList(new Page<KeyInfo>(page, pageLimit), entity,orderEntity);
        if (CollectionUtils.isNotEmpty(response.getRecords())) {
            Set<Integer> deviceIds = response.getRecords().stream().map(KeyInfo::getDeviceId).collect(Collectors.toSet());
            List<Equipment> equipmentList = equipmentService.listNameAndIDByIds(deviceIds);
            if (CollectionUtils.isNotEmpty(equipmentList)) {
                List<KeyInfo> keyInfos = response.getRecords().stream().map(obj -> {
                    Optional<Equipment> option = equipmentList.stream().filter(equ -> Objects.equals(equ.getId().intValue(), obj.getDeviceId().intValue())).findFirst();
                    if (option.isPresent()) {
                        obj.setDeviceLabel(option.get().getSiteName());
                    }
                    return obj;
                }).collect(Collectors.toList());
                response.setRecords(keyInfos);
            }
        }

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

    @PostMapping(value = "list/select")
    @ApiOperation("(页面选择)")
    public AjaxResult listSelect(@RequestBody KeyInfoQueryDTO dto) {
        dto.clearStrEmpty();
        KeyInfo entity = convert(dto);
        List<KeyInfo> selectList = KeyInfoService.listOfSelect(entity);
        return AjaxResult.success(selectList);
    }

    @GetMapping(value = "/view/{id}")
    @ApiOperation("根据ID查看详情")
    public com.ruoyi.common.core.domain.AjaxResult view(@PathVariable("id") Integer id, org.springframework.ui.Model model) {
        KeyInfo source = KeyInfoService.getById(id);

        return com.ruoyi.common.core.domain.AjaxResult.success(source);
    }

    @PreAuthorize("@ss.hasPermi('key:info:export')")
    @PostMapping("/export")
    @ApiOperation("导出")
    public void export(HttpServletResponse response, KeyInfoQueryDTO dto) {
        dto.clearStrEmpty();
        KeyInfo entity = convert(dto);
        List<KeyInfo> list = KeyInfoService.list(entity);
        ExcelUtil<KeyInfo> util = new ExcelUtil<KeyInfo>(KeyInfo.class);
        util.exportExcel(response, list, "KeyInfo");
    }

    @PreAuthorize("@ss.hasPermi('key:info:add')")
    @PostMapping(value = "/add")
    @ApiOperation("添加")
    public com.ruoyi.common.core.domain.AjaxResult add(@ModelAttribute KeyInfoAddDTO request) {
        request.clearStrEmpty();
        KeyInfo entity = convert(request);
        int count = KeyInfoService.insertSelective(entity);
        return count > 0 ? com.ruoyi.common.core.domain.AjaxResult.success(Boolean.TRUE) : com.ruoyi.common.core.domain.AjaxResult.error("添加数据失败,请稍后再试");
    }

    @PreAuthorize("@ss.hasPermi('key:info:update')")
    @PostMapping(value = "/update")
    @ApiOperation("修改")
    public com.ruoyi.common.core.domain.AjaxResult update(@ModelAttribute KeyInfoUpdateDTO request) {
        request.clearStrEmpty();
        KeyInfo entity = convert(request);
        boolean flag = KeyInfoService.updateByPrimaryKey(entity);
        return flag ? com.ruoyi.common.core.domain.AjaxResult.success(Boolean.TRUE) : com.ruoyi.common.core.domain.AjaxResult.error("修改数据失败,请稍后再试");
    }

    @PreAuthorize("@ss.hasPermi('key:info:update:status')")
    @PostMapping(value = "/update/status")
    @ApiOperation("修改状态")
    public com.ruoyi.common.core.domain.AjaxResult updateState(@ModelAttribute KeyInfoUpdateStatusDTO request) {
        request.clearStrEmpty();
        KeyInfo entity = convert(request);
        boolean flag = KeyInfoService.updateByPrimaryKey(entity);
        return flag ? com.ruoyi.common.core.domain.AjaxResult.success(Boolean.TRUE) : com.ruoyi.common.core.domain.AjaxResult.error("修改数据失败,请稍后再试");
    }

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

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

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

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

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

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

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