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

import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import java.util.*;
import java.io.IOException;
import com.cf.imes.framework.common.pojo.PageParam;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.util.object.BeanUtils;
import static com.cf.imes.framework.common.pojo.CommonResult.success;
import com.cf.imes.framework.excel.core.util.ExcelUtils;
import com.cf.imes.framework.operatelog.core.annotations.OperateLog;
import static com.cf.imes.framework.operatelog.core.enums.OperateTypeEnum.*;
import com.cf.imes.module.system.controller.admin.labeltemplate.vo.*;
import com.cf.imes.module.system.dal.dataobject.labeltemplate.LabelTemplateDO;
import com.cf.imes.module.system.service.labeltemplate.LabelTemplateService;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

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

    @Resource
    private LabelTemplateService labelTemplateService;

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

    @PutMapping("/update")
    @Operation(summary = "更新标签模板")
    @PreAuthorize("@ss.hasPermission('system:label-template:update')")
    @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("/get")
    @Operation(summary = "获得标签模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:label-template:query')")
    @OperateLog(enable = false)
    public CommonResult<LabelTemplateRespVO> getLabelTemplate(@RequestParam("id") Long id) {
        return success(labelTemplateService.getLabelTemplate(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得标签模板分页")
    @PreAuthorize("@ss.hasPermission('system:label-template:query')")
    @OperateLog(enable = false)
    public CommonResult<PageResult<LabelTemplateRespVO>> getLabelTemplatePage(@Valid LabelTemplatePageReqVO pageReqVO) {
        PageResult<LabelTemplateDO> pageResult = labelTemplateService.getLabelTemplatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LabelTemplateRespVO.class));
    }

    @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.hasPermission('system:label-template:query')")
    @OperateLog(enable = false)
    public CommonResult<LabelTemplateRespVO> getDefaultLabelTemplate(@RequestParam("type") String type) {
        return success(labelTemplateService.getDefaultLabelTemplate(type));
    }

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


    @GetMapping("/export-excel")
    @Operation(summary = "导出标签模板 Excel")
    @PreAuthorize("@ss.hasPermission('system:label-template:export')")
    @OperateLog(type = EXPORT)
    public void exportLabelTemplateExcel(@Valid LabelTemplatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LabelTemplateDO> list = labelTemplateService.getLabelTemplatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "标签模板.xls", "数据", LabelTemplateRespVO.class,
                        BeanUtils.toBean(list, LabelTemplateRespVO.class));
    }


}