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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.newfiber.common.exception.RRException;
import com.newfiber.common.utils.R;
import com.newfiber.modules.inspection.entity.InspectionTemplateEntity;
import com.newfiber.modules.inspection.service.InspectionTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.HashMap;
import java.util.List;
import java.util.UUID;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
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;


/**
 * 巡检模版信息
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2020-09-07 19:49:56
 */
@RestController
@Api(value = "巡检模版管理", tags = "巡检模版管理")
@RequestMapping("inspectiontemplate")
public class InspectionTemplateController {

    @Autowired
    private InspectionTemplateService inspectionTemplateService;

    /**
     * 列表
     */
    @PostMapping("/list")
    //@RequiresPermissions("business:inspectiontemplate:list")
    public R list() {
        List<HashMap<String, Object>> list = inspectionTemplateService.queryList();

        return R.ok().put("list", list);
    }

    /**
     * 批量保存,先删除在新增
     */
    @ApiOperation(value = "批量保存巡检模板")
    @PostMapping("/saveBatch")
    //@RequiresPermissions("business:inspectiontemplate:save")
    @Transactional
    public R saveBatch(@RequestBody List<InspectionTemplateEntity> entities) {

        QueryWrapper<InspectionTemplateEntity> wrapper = new QueryWrapper<InspectionTemplateEntity>()
                .eq("template_no", entities.get(0).getTemplateNo());
        inspectionTemplateService.remove(wrapper);

        String uuid = UUID.randomUUID().toString().replace("-", "");
        for (InspectionTemplateEntity entity : entities) {
            QueryWrapper<InspectionTemplateEntity> queryWrapper = new QueryWrapper<InspectionTemplateEntity>().eq("template_name", entity.getTemplateName());
            if(CollectionUtils.isNotEmpty(inspectionTemplateService.list(queryWrapper))){
                throw new RRException(String.format("模板名称:{%s}已存在,请重新输入", entity.getTemplateName()));
            }
            entity.setTemplateNo(uuid);
        }
        inspectionTemplateService.saveBatch(entities);

        return R.ok().put("templateNo", uuid);
    }

    /**
     * 根据模版编号删除
     */
    @PostMapping("/deleteByNo")
    //@RequiresPermissions("business:inspectiontemplate:delete")
    public R delete(@RequestBody String[] templateNos) {

        QueryWrapper<InspectionTemplateEntity> wrapper = new QueryWrapper<InspectionTemplateEntity>()
                .in("template_no", templateNos);
        inspectionTemplateService.remove(wrapper);

        return R.ok();
    }

}