package com.cf.imes.module.system.controller.admin.labeltemplate;

import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.operatelog.core.annotations.OperateLog;
import com.cf.imes.module.system.controller.admin.labeltemplate.vo.LabelTemplateRespVO;
import com.cf.imes.module.system.controller.admin.labeltemplate.vo.LabelTemplateSaveReqVO;
import com.cf.imes.module.system.service.labeltemplate.LabelTemplateService;
import com.cf.imes.module.system.validation.label.LabelTemplateTypeValid;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;
import java.util.Map;

import static com.cf.imes.framework.common.pojo.CommonResult.success;

@Tag(name = "标签模板管理")
@RestController
@RequestMapping("/system/label-template")
@Validated
public class LabelTemplateController {

    @Resource
    private LabelTemplateService labelTemplateService;

    @PostMapping("/create")
    @Operation(summary = "创建标签模板")
    @PreAuthorize("@ss.hasAnyPermissions('system:label-template:create','system:label-template:copy')")
    @OperateLog(enable = false)
    public CommonResult<Long> createLabelTemplate(@Valid @RequestBody LabelTemplateSaveReqVO createReqVO) {
        return success(labelTemplateService.createLabelTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新标签模板")
    @PreAuthorize("@ss.hasAnyPermissions('system:label-template:update','system:label-template:save','system:label-template:create')")
    @OperateLog(enable = false)
    public CommonResult<Boolean> updateLabelTemplate(@Valid @RequestBody LabelTemplateSaveReqVO updateReqVO) {
        labelTemplateService.updateLabelTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除标签模板")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:label-template:delete')")
    public CommonResult<Boolean> deleteLabelTemplate(@RequestParam("id") Long id) {
        labelTemplateService.deleteLabelTemplate(id);
        return success(true);
    }

    @GetMapping("/group-list")
    @Operation(summary = "获得分组标签模板列表")
    @PreAuthorize("@ss.hasPermission('system:label-template:query')")
    @OperateLog(enable = false)
    public CommonResult<Map<String, List<LabelTemplateRespVO>>> getGroupList() {
        return success(labelTemplateService.getGroupList());
    }

    @GetMapping("/getDefault")
    @Operation(summary = "获得默认标签模板")
    @Parameter(name = "type", description = "标签类型", required = true, example = "1024")
    @PreAuthorize("@ss.hasAnyPermissions('system:label-template:query','sysTagDesignManager:query')")
    @OperateLog(enable = false)
    public CommonResult<LabelTemplateRespVO> getDefaultLabelTemplate(@RequestParam("type") @LabelTemplateTypeValid String type) {
        return success(labelTemplateService.getDefaultLabelTemplate(type));
    }

    @GetMapping("/setDefaultTemplate")
    @Operation(summary = "设置默认标签模板")
    @PreAuthorize("@ss.hasPermission('system:label-template:update')")
    public CommonResult<Boolean> setDefaultTemplate(@RequestParam Long id) {
        return success(labelTemplateService.setDefaultTemplate(id));
    }
}