package cn.newfeifan.mall.module.system.controller.admin.wechatmsgtemplate;

import cn.newfeifan.mall.framework.common.pojo.CommonResult;
import cn.newfeifan.mall.framework.common.pojo.PageParam;
import cn.newfeifan.mall.framework.common.pojo.PageResult;
import cn.newfeifan.mall.framework.common.util.object.BeanUtils;
import cn.newfeifan.mall.framework.excel.core.util.ExcelUtils;
import cn.newfeifan.mall.framework.operatelog.core.annotations.OperateLog;
import cn.newfeifan.mall.module.system.controller.admin.wechatmsgtemplate.vo.WechatMsgTemplatePageReqVO;
import cn.newfeifan.mall.module.system.controller.admin.wechatmsgtemplate.vo.WechatMsgTemplateRespVO;
import cn.newfeifan.mall.module.system.controller.admin.wechatmsgtemplate.vo.WechatMsgTemplateSaveReqVO;
import cn.newfeifan.mall.module.system.dal.dataobject.wechatmsgtemplate.WechatMsgTemplateDO;
import cn.newfeifan.mall.module.system.service.wechatmsgtemplate.WechatMsgTemplateService;
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 static cn.newfeifan.mall.framework.common.pojo.CommonResult.success;
import static cn.newfeifan.mall.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 微信消息模板")
@RestController
@RequestMapping("/system/wechat-msg-template")
@Validated
public class WechatMsgTemplateController {

    @Resource
    private WechatMsgTemplateService wechatMsgTemplateService;

    @PostMapping("/create")
    @Operation(summary = "创建微信消息模板")
    @PreAuthorize("@ss.hasPermission('system:wechat-msg-template:create')")
    public CommonResult<Long> createWechatMsgTemplate(@Valid @RequestBody WechatMsgTemplateSaveReqVO createReqVO) {
        return success(wechatMsgTemplateService.createWechatMsgTemplate(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新微信消息模板")
    @PreAuthorize("@ss.hasPermission('system:wechat-msg-template:update')")
    public CommonResult<Boolean> updateWechatMsgTemplate(@Valid @RequestBody WechatMsgTemplateSaveReqVO updateReqVO) {
        wechatMsgTemplateService.updateWechatMsgTemplate(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除微信消息模板")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('system:wechat-msg-template:delete')")
    public CommonResult<Boolean> deleteWechatMsgTemplate(@RequestParam("id") Long id) {
        wechatMsgTemplateService.deleteWechatMsgTemplate(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得微信消息模板")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('system:wechat-msg-template:query')")
    public CommonResult<WechatMsgTemplateRespVO> getWechatMsgTemplate(@RequestParam("id") Long id) {
        WechatMsgTemplateDO wechatMsgTemplate = wechatMsgTemplateService.getWechatMsgTemplate(id);
        return success(BeanUtils.toBean(wechatMsgTemplate, WechatMsgTemplateRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得微信消息模板分页")
    @PreAuthorize("@ss.hasPermission('system:wechat-msg-template:query')")
    public CommonResult<PageResult<WechatMsgTemplateRespVO>> getWechatMsgTemplatePage(@Valid WechatMsgTemplatePageReqVO pageReqVO) {
        PageResult<WechatMsgTemplateDO> pageResult = wechatMsgTemplateService.getWechatMsgTemplatePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, WechatMsgTemplateRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出微信消息模板 Excel")
    @PreAuthorize("@ss.hasPermission('system:wechat-msg-template:export')")
    @OperateLog(type = EXPORT)
    public void exportWechatMsgTemplateExcel(@Valid WechatMsgTemplatePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WechatMsgTemplateDO> list = wechatMsgTemplateService.getWechatMsgTemplatePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "微信消息模板.xls", "数据", WechatMsgTemplateRespVO.class,
                        BeanUtils.toBean(list, WechatMsgTemplateRespVO.class));
    }

}
