package com.seer.teach.mp.admin.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.seer.teach.common.PageListBean;
import com.seer.teach.common.ResultBean;
import com.seer.teach.common.enums.ResultCodeEnum;
import com.seer.teach.common.utils.AssertUtils;
import com.seer.teach.common.utils.PageConverterUtils;
import com.seer.teach.mp.admin.controller.req.MpTemplateMessageConfigPageQueryReq;
import com.seer.teach.mp.admin.controller.req.MpTemplateMessageConfigReq;
import com.seer.teach.mp.admin.controller.resp.MpTemplateMessageConfigResp;
import com.seer.teach.mp.admin.convert.AdminMpTemplateMessageConfigConvert;
import com.seer.teach.mp.entity.MpTemplateMessageConfigEntity;
import com.seer.teach.mp.service.IMpTemplateMessageConfigService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;

/**
 * <p>
 * 模板消息自动发送配置表 前端控制器
 * </p>
 *
 * @author Wang
 * @since 2025-08-12 09:21:28
 */
@Tag(name = "管理端 - 公众号模板消息配置")
@RequiredArgsConstructor
@RestController
@RequestMapping("/mp/message/config")
public class AdminMpTemplateMessageConfigController {

    private final IMpTemplateMessageConfigService mpTemplateMessageConfigService;

    /**
     * 分页查询模板消息配置列表
     * 
     */
    @Operation(summary = "分页查询模板消息配置列表")
    @GetMapping("/paged")
    @SaCheckPermission("mp:message:config:paged")
    public ResultBean<PageListBean<MpTemplateMessageConfigResp>> list(MpTemplateMessageConfigPageQueryReq params) {
        IPage<MpTemplateMessageConfigEntity> pageQuery = new Page<>(params.getPageNo(), params.getPageSize());
        MpTemplateMessageConfigEntity queryParam = AdminMpTemplateMessageConfigConvert.INSTANCE.convertQuery2Entity(params);
        IPage<MpTemplateMessageConfigEntity> resultPage = mpTemplateMessageConfigService.pageList(pageQuery,queryParam);
        PageListBean<MpTemplateMessageConfigResp> mpTemplateMessageConfigRespPageListBean = PageConverterUtils.convertPageListBean(resultPage, AdminMpTemplateMessageConfigConvert.INSTANCE::convertList);
        return ResultBean.success(mpTemplateMessageConfigRespPageListBean);
    }

    /**
     * 根据ID查询模板消息配置
     * 
     */
    @Operation(summary = "根据ID查询模板消息配置")
    @GetMapping("/{id}")
    @SaCheckPermission("mp:message:config:query")
    public ResultBean<MpTemplateMessageConfigResp> getById(@PathVariable Long id) {
        MpTemplateMessageConfigEntity entity = mpTemplateMessageConfigService.getById(id);
        MpTemplateMessageConfigResp resp = AdminMpTemplateMessageConfigConvert.INSTANCE.convertOne(entity);
        return ResultBean.success(resp);
    }

    /**
     * 创建模板消息配置
     */
    @Operation(summary = "创建模板消息配置")
    @PostMapping
    @SaCheckPermission("mp:message:config:create")
    public ResultBean<Boolean> create(@Valid @RequestBody MpTemplateMessageConfigReq req) {
        long counter = mpTemplateMessageConfigService.getTemplateIdByBusinessSceneCodeAndTemplateId(req.getBusinessSceneCode(), req.getTemplateId());
        AssertUtils.isTrue(counter == 0, ResultCodeEnum.TEMPLATE_ID_BUSINESS_SCENE_ALREADY_EXISTS);
        MpTemplateMessageConfigEntity entity = AdminMpTemplateMessageConfigConvert.INSTANCE.convertOne2Req(req);
        boolean result = mpTemplateMessageConfigService.save(entity);
        return ResultBean.success(result);
    }

    /**
     * 更新模板消息配置
     */
    @Operation(summary = "更新模板消息配置")
    @PutMapping
    @SaCheckPermission("mp:message:config:update")
    public ResultBean<Boolean> update(@Valid @RequestBody MpTemplateMessageConfigReq req) {
        MpTemplateMessageConfigEntity entity = AdminMpTemplateMessageConfigConvert.INSTANCE.convertOne2Req(req);
        boolean result = mpTemplateMessageConfigService.updateById(entity);
        return ResultBean.success(result);
    }

    /**
     * 删除模板消息配置
     */
    @Operation(summary = "删除模板消息配置")
    @DeleteMapping("/{id}")
    @SaCheckPermission("mp:message:config:delete")
    public ResultBean<Boolean> delete(@PathVariable Long id) {
        boolean result = mpTemplateMessageConfigService.removeById(id);
        return ResultBean.success(result);
    }
}