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

import com.cf.imes.framework.common.pojo.CommonResult;
import com.cf.imes.framework.common.pojo.PageParam;
import com.cf.imes.framework.common.pojo.PageResult;
import com.cf.imes.framework.common.util.object.BeanUtils;
import com.cf.imes.framework.excel.core.util.ExcelUtils;
import com.cf.imes.framework.operatelog.core.annotations.OperateLog;
import com.cf.imes.module.system.controller.admin.label.vo.LabelPageReqVO;
import com.cf.imes.module.system.controller.admin.label.vo.LabelRespVO;
import com.cf.imes.module.system.controller.admin.label.vo.LabelSaveReqVO;
import com.cf.imes.module.system.dal.dataobject.lable.LabelDO;
import com.cf.imes.module.system.service.lable.LabelService;
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 javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import static com.cf.imes.framework.common.pojo.CommonResult.success;
import static com.cf.imes.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "标签管理")
@RestController
@RequestMapping("/system/label")
@Validated
public class LabelController {

    @Resource
    private LabelService labelService;

    @PostMapping("/create")
    @Operation(summary = "创建标签")
    //@PreAuthorize("@ss.hasPermission('system:label:create')")
    @OperateLog(enable = false)
    public CommonResult<Long> createLabel(@Valid @RequestBody LabelSaveReqVO createReqVO) {
        return success(labelService.createLabel(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新标签")
    //@PreAuthorize("@ss.hasPermission('system:label:update')")
    @OperateLog(enable = false)
    public CommonResult<Boolean> updateLabel(@Valid @RequestBody LabelSaveReqVO updateReqVO) {
        labelService.updateLabel(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得标签")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    //@PreAuthorize("@ss.hasPermission('system:label:query')")
    @OperateLog(enable = false)
    public CommonResult<LabelRespVO> getLabel(@RequestParam("id") Long id) {
        return success(labelService.getLabel(id));
    }

    @GetMapping("/page")
    @Operation(summary = "获得标签分页")
    //@PreAuthorize("@ss.hasPermission('system:label:query')")
    @OperateLog(enable = false)
    public CommonResult<PageResult<LabelRespVO>> getLabelPage(@Valid LabelPageReqVO pageReqVO) {
        PageResult<LabelDO> pageResult = labelService.getLabelPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, LabelRespVO.class));
    }

    @GetMapping("list")
    @Operation(summary = "获得标签列表")
    @Parameter(name = "type", description = "类型", required = false, example = "1024")
    //@PreAuthorize("@ss.hasPermission('system:label:query')")
    @OperateLog(enable = false)
    public CommonResult<List<LabelRespVO>> getList(@RequestParam("type") String type) {
        return success(BeanUtils.toBean(labelService.list(type), LabelRespVO.class));
    }

    @GetMapping("/group-list")
    @Operation(summary = "获得分组标签列表")
    //@PreAuthorize("@ss.hasPermission('system:label:query')")
    @OperateLog(enable = false)
    public CommonResult<Map<String, List<LabelRespVO>>> getGroupList(@RequestParam(value = "organId", required = false) Long organId) {
        return success(labelService.getGroupList(organId));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出标签 Excel")
    //@PreAuthorize("@ss.hasPermission('system:label:export')")
    @OperateLog(type = EXPORT)
    public void exportLabelExcel(@Valid LabelPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<LabelDO> list = labelService.getLabelPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "标签.xls", "数据", LabelRespVO.class,
                        BeanUtils.toBean(list, LabelRespVO.class));
    }

}