package com.simuwang.manage.api.template;

import com.simuwang.base.common.support.MybatisPage;
import com.simuwang.base.pojo.dto.query.DeletionPageQuery;
import com.simuwang.base.pojo.dto.query.EmailTemplateInfoPageQuery;
import com.simuwang.base.pojo.vo.*;
import com.simuwang.logging.SystemLog;
import com.simuwang.manage.service.EmailTemplateInfoService;
import com.smppw.common.pojo.ResultVo;
import com.smppw.common.pojo.enums.status.ResultCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 模版管理
 * Author:   chenjianhua
 * Date:     2024/9/25 18:46
 * Description: ${DESCRIPTION}
 */
@SystemLog(value = "模版管理")
@RestController
@RequestMapping("/v1/template")
public class TemplateController {

    @Autowired
    private EmailTemplateInfoService emailTemplateInfoService;

    /**
     * 模版详情页面展示数据
     * @param emailTemplateInfoPageQuery
     * @return
     */
    @SystemLog(value = "模版详情页面展示数据")
    @RequestMapping("search-template-list")
    public MybatisPage<EmailTemplateInfoVO> searchTemplateList(EmailTemplateInfoPageQuery emailTemplateInfoPageQuery){
        MybatisPage<EmailTemplateInfoVO> result = emailTemplateInfoService.searchTemplateList(emailTemplateInfoPageQuery);
        return result;
    }


    /**
     * 批量删除模版
     * @param idListVO
     * @return
     */
    @SystemLog(value = "批量删除模版")
    @RequestMapping("delete-template-list")
    public boolean deleteTemplateList(@RequestBody IdListVO idListVO){
        emailTemplateInfoService.deleteTemplateList(idListVO);
        return true;
    }

    /**
     * 开启关闭状态
     * @param statusVO
     * @return
     */
    @SystemLog(value = "开启关闭状态", type = SystemLog.Type.UPDATE)
    @PostMapping("control-template-status")
    public ResultVo controlTemplateConfig(@RequestBody StatusVO statusVO){
        ResultVo vo = new ResultVo(ResultCode.SAVE_SUCCESS.getCode());
        try{
            emailTemplateInfoService.controlTemplateConfig(statusVO);
            vo.setData(true);
        }catch (Exception e){
            vo.setCode(ResultCode.SAVE_FAILED.getCode());
        }
        return vo;
    }

    /**
     * 保存模版信息
     * @param saveTemplateInfoVO
     * @return
     */
    @SystemLog(value = "保存模版信息")
    @RequestMapping("save-template-list")
    public boolean saveTemplateList(@RequestBody SaveTemplateInfoVO saveTemplateInfoVO){
        emailTemplateInfoService.saveTemplateList(saveTemplateInfoVO);
        return true;
    }

    /**
     * 批量删除适用规则
     * @param idListVO
     * @return
     */
    @SystemLog(value = "批量删除适用规则")
    @RequestMapping("delete-application-rule")
    public boolean deleteApplicationRuleList(@RequestBody IdListVO idListVO){
        emailTemplateInfoService.deleteApplicationRuleList(idListVO);
        return true;
    }

    /**
     * 模版编辑查询
     * @param idVO
     * @return
     */
    @SystemLog(value = "模版编辑查询")
    @GetMapping("search-template-info")
    public ResultVo searchTemplateInfo(IdVO idVO){
        ResultVo vo = new ResultVo(ResultCode.SAVE_SUCCESS.getCode());
        try{
            SaveTemplateInfoVO saveTemplateInfoVO = emailTemplateInfoService.searchTemplateInfo(idVO.getId());
            vo.setData(saveTemplateInfoVO);
        }catch (Exception e){
            vo.setCode(ResultCode.SAVE_FAILED.getCode());
            vo.setData(false);
        }
        return vo;
    }

}
