Newer
Older
huludao / src / main / java / com / newfiber / modules / inspection / controller / EquDectionItemController.java
package com.newfiber.modules.inspection.controller;

import com.github.pagehelper.PageHelper;
import com.newfiber.common.exception.RRException;
import com.newfiber.common.utils.Constant;
import com.newfiber.common.utils.R;
import com.newfiber.modules.inspection.entity.EquDetectionItem;
import com.newfiber.modules.inspection.entity.EquInfoEntity;
import com.newfiber.modules.inspection.request.EquDetectionItemSaveReq;
import com.newfiber.modules.inspection.request.EquDetectionItemUpdateReq;
import com.newfiber.modules.inspection.service.EquDetectionItemService;
import com.newfiber.modules.inspection.service.EquInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;
import javax.validation.Valid;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(tags = "设备检测项")
@RequestMapping("equ_detection_item")
public class EquDectionItemController {

    @Autowired
    private EquDetectionItemService equDetectionItemService;

    @Autowired
    private EquInfoService equInfoService;

    /**
     * 保存
     */
    @PostMapping("/save")
    @ApiOperation("新增设备检测项")
    public R save(@RequestBody @Valid EquDetectionItemSaveReq req) {
        EquInfoEntity equInfoEntity = equInfoService.getByEquNo(req.getEquNo());

        if(null == equInfoEntity){
            throw new RRException(String.format("该设备不存在:{%s}", req.getEquNo()));
        }

        EquDetectionItem equDetectionItem = new EquDetectionItem();
        BeanUtils.copyProperties(req, equDetectionItem);

        equDetectionItem.setEquName(equInfoEntity.getEquName());
        equDetectionItem.setCreateDatetime(new Date());
        equDetectionItemService.save(equDetectionItem);

        return R.ok();
    }

    /**
     * 删除
     */
    @PostMapping("/delete")
    @ApiOperation("删除设备检测项")
    public R delete(@RequestBody Integer[] ids) {
        equDetectionItemService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation("修改设备检测项")
    public R update(@RequestBody @Valid EquDetectionItemUpdateReq req) {
        EquDetectionItem equDetectionItem = new EquDetectionItem();
        BeanUtils.copyProperties(req, equDetectionItem);

        equDetectionItemService.updateById(equDetectionItem);

        return R.ok();
    }

    /**
     * 列表
     */
    @PostMapping("/list")
    @ApiOperation("列表查设备检测项")
    public R list(@RequestBody Map<String, Object> params) {
        return R.ok().put("list", equDetectionItemService.listByMap(params));
    }

    /**
     * 分页
     */
    @PostMapping("/page")
    @ApiOperation("分页查设备检测项")
    public R page(@RequestBody Map<String, Object> params) {
        int page = Integer.parseInt(params.get(Constant.PAGE).toString());
        int limit = Integer.parseInt(params.get(Constant.LIMIT).toString());
        PageHelper.startPage(page, limit);

        params.remove(Constant.PAGE);
        params.remove(Constant.LIMIT);

        return R.ok().put("page", equDetectionItemService.listByMap(params));
    }

    /**
     * 详细查
     */
    @PostMapping("/detail/{id}")
    @ApiOperation("详细查设备检测项")
    public R detail(@PathVariable Integer id) {
        return R.ok().put("data", equDetectionItemService.getById(id));
    }

}