package com.chushang.inspection.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.chushang.common.core.validator.Create;
import com.chushang.common.core.web.AjaxResult;
import com.chushang.common.log.annotation.SysLog;
import com.chushang.common.log.enums.BusinessType;
import com.chushang.inspection.project.po.Template;
import com.chushang.inspection.project.service.TbTemplateService;
import com.chushang.oss.entity.dto.UploadFileDTO;
import com.chushang.oss.feign.RemoteOssService;
import com.chushang.security.annotation.RequiresPermissions;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * 模板相关管理
 * @auther: zhao
 * @date: 2024/7/2 11:09
 */
@RestController
@RequestMapping(value = "/template")
public class TemplateController {

    /**
     * 新增 模板, 然后 巡检单模板, 去进行筛选选择
     */

    @Resource
    TbTemplateService tbTemplateService;

    /**
     * 获取全部类型的模板信息
     * 用于筛选
     * @param templateType 模板类型
     */
    @GetMapping(value = "/list")
    public AjaxResult allList(@RequestParam(name = "templateType") String templateType){
        return AjaxResult.success(tbTemplateService.list(
                new LambdaQueryWrapper<Template>()
                        .eq(Template::getTemplateType, templateType)
        ));
    }

    /**
     * 模板列表页面
     */
    @GetMapping(value = "/page")
//    @RequiresPermissions(value = "ins:template:page")
    public AjaxResult pageList(Template template){
        return AjaxResult.success(tbTemplateService.pageList(template));
    }
    /**
     * 新增模板
     */
    @SysLog(value = "模板", businessType = BusinessType.INSERT)
    @PostMapping(value = "/save")
//    @RequiresPermissions(value = "ins:template:save")
    public AjaxResult save(@RequestBody@Validated(Create.class) Template template){
        return AjaxResult.success(tbTemplateService.saveTemplate(template));
    }
    /**
     * 修改模板
     */
    @SysLog(value = "模板", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/update")
//    @RequiresPermissions(value = "ins:template:update")
    public AjaxResult update(@RequestBody@Validated(Create.class) Template template){
        return AjaxResult.success(tbTemplateService.updateTemplate(template));
    }


    /**
     * 删除模板
     */
    @SysLog(value = "模板", businessType = BusinessType.DELETE)
    @DeleteMapping(value = "/del/{templateId}")
    @RequiresPermissions(value = "ins:template:del")
    public AjaxResult del(@PathVariable Long templateId){
        return AjaxResult.success(tbTemplateService.removeById(templateId));
    }

    /**
     * 上传模板
     * @param file 文件
     */
    @SysLog(value = "模板", businessType = BusinessType.UPLOAD)
    @PostMapping(value = "/upload")
    @RequiresPermissions(value = "ins:template:upload")
    public AjaxResult uploadTemplate(@RequestParam MultipartFile file){
        return AjaxResult.success(tbTemplateService.uploadFile(file));
    }

}
