package cn.iocoder.yudao.module.configure.controller.admin.inspectiontemplate;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.module.configure.controller.admin.inspectiontemplate.vo.InspectionTemplateCreateReqVO;
import cn.iocoder.yudao.module.configure.controller.admin.inspectiontemplate.vo.InspectionTemplatePageReqVO;
import cn.iocoder.yudao.module.configure.controller.admin.inspectiontemplate.vo.InspectionTemplateRespVO;
import cn.iocoder.yudao.module.configure.controller.admin.inspectiontemplate.vo.InspectionTemplateUpdateReqVO;
import cn.iocoder.yudao.module.configure.convert.inspectiontemplate.InspectionTemplateConvert;
import cn.iocoder.yudao.module.configure.dal.dataobject.inspectiontemplate.InspectionTemplateDO;
import cn.iocoder.yudao.module.configure.service.inspectiontemplate.InspectionTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Api(tags = "管理后台 - 检测报告模板")
@RestController
@RequestMapping("/configure/inspection-template")
@Validated
public class InspectionTemplateController {

    @Resource
    private InspectionTemplateService inspectionTemplateService;

    @PostMapping("/create")
    @ApiOperation("创建检测报告模板")
    @PreAuthorize("@ss.hasPermission('configure:inspection-template:create')")
    public CommonResult<Long> createInspectionTemplate(@Valid @RequestBody InspectionTemplateCreateReqVO createReqVO) {
        return success(inspectionTemplateService.createInspectionTemplate(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新检测报告模板")
    @PreAuthorize("@ss.hasPermission('configure:inspection-template:update')")
    public CommonResult<Boolean> updateInspectionTemplate(@Valid @RequestBody InspectionTemplateUpdateReqVO updateReqVO) {
        inspectionTemplateService.updateInspectionTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除检测报告模板")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('configure:inspection-template:delete')")
    public CommonResult<Boolean> deleteInspectionTemplate(@RequestParam("id") Long id) {
        inspectionTemplateService.deleteInspectionTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得检测报告模板")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('configure:inspection-template:query')")
    public CommonResult<InspectionTemplateRespVO> getInspectionTemplate(@RequestParam("id") Long id) {
        return success(inspectionTemplateService.getInspectionTemplate(id));
    }

    @GetMapping("/page")
    @ApiOperation("获得检测报告模板分页")
    @PreAuthorize("@ss.hasPermission('configure:inspection-template:query')")
    public CommonResult<PageResult<InspectionTemplateRespVO>> getInspectionTemplatePage(@Valid InspectionTemplatePageReqVO pageVO) {
        PageResult<InspectionTemplateDO> pageResult = inspectionTemplateService.getInspectionTemplatePage(pageVO);
        return success(InspectionTemplateConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/getByBusinessId")
    @ApiOperation("通过商户ID获得检测报告模板列表")
    @ApiImplicitParam(name = "businessId", value = "商户ID", required = true, example = "1024", dataTypeClass = Long.class)
    @PreAuthorize("@ss.hasPermission('configure:inspection-template:query')")
    public CommonResult<List<InspectionTemplateRespVO>> getInspectionTemplateByBusinessId(@RequestParam("businessId") Long businessId) {
        return success(inspectionTemplateService.getInspectionTemplateByBusinessId(businessId));
    }

}
