package com.huijy.web.controller.weixin;

import com.huijy.common.constant.Constants;
import com.huijy.common.core.domain.AjaxResult;
import com.huijy.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.WxMpTemplateMsgService;
import me.chanjar.weixin.mp.bean.template.WxMpTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/wxmptemp")
@Api(value = "wxmptemp", tags = "微信公众号模板管理")
public class WxMpTemplateController {

    private final WxMpService wxService;

    private ISysConfigService sysConfigService;

    /**
     * 获取模板列表
     * @return R
     */
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('wxmp:wxtemp:list')")
    public AjaxResult getTempList() throws WxErrorException {

       String tempId =  sysConfigService.selectConfigByKey(Constants.WX_MP_TEMPLATE_ID_KEY);

        WxMpTemplateMsgService templateMsgService = wxService.getTemplateMsgService();
        List<WxMpTemplate> list = templateMsgService.getAllPrivateTemplate();
        Map<String, Object> resultObj = new HashMap<String, Object>();
        resultObj.put("list", list);
        resultObj.put("tempId", tempId);

        return AjaxResult.success(resultObj);
    }

    //
    /**
     * 通过id删除公众号模板
     * @param id id
     * @return R
     */
    @DeleteMapping("/{id}")
    @PreAuthorize("@ss.hasPermi('wxmp:wxtemp:del')")
    public AjaxResult removeById(@PathVariable String id) throws WxErrorException {
        WxMpTemplateMsgService templateMsgService = wxService.getTemplateMsgService();
        return AjaxResult.success(templateMsgService.delPrivateTemplate(id));
    }

}
