package cn.iocoder.yudao.module.system.api.subscribe;

import cn.binarywang.wx.miniapp.bean.WxMaSubscribeMessage;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.template.vo.MsgData;
import cn.iocoder.yudao.framework.common.template.vo.SubscribeMessageReqDTO;
import cn.iocoder.yudao.module.system.dal.dataobject.social.SocialClientDO;
import cn.iocoder.yudao.module.system.service.social.SocialClientService;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateData;
import me.chanjar.weixin.mp.bean.template.WxMpTemplateMessage;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

/**
 * 功能描述 微信小程序发送订阅消息
 *
 * @author: yj
 * @date: 2024年02月25日 11:35
 */
@RestController // 提供 RESTful API 接口，给 Feign 调用
@Validated
@Slf4j
public class SubscribeMessageSendApiImpl implements SubscribeMessageSendApi {

    @Resource
    private SocialClientService socialClientService;

    @Override
    public CommonResult<Long> sendMaMsg(SubscribeMessageReqDTO reqDTO) {
        try {
            //发送审批结果通知
            socialClientService.getWxMaService(reqDTO.getSocialType()).getMsgService().sendSubscribeMsg(initWxMaSubscribeMessage(reqDTO));
        } catch (Exception e) {
            log.error("发送小程序订阅消息失败:{}", e.getMessage());
        }
        return success(1L);
    }

    @Override
    public CommonResult<Long> sendMpMsg(SubscribeMessageReqDTO reqDTO) {
        // -- 公众号 点击 跳转 小程序 - 需要获取到小程序appId
        // 判断是否需要跳转小程序 - 通过
        try {
            socialClientService.getWxMpService().getTemplateMsgService().sendTemplateMsg(initWxMpSubscribeMessage(reqDTO));
        } catch (Exception e) {
            log.error("发送公众号消息失败:{}", e.getMessage());
        }
        return success(1L);
    }

    private WxMaSubscribeMessage initWxMaSubscribeMessage(SubscribeMessageReqDTO reqDTO) {
        WxMaSubscribeMessage message = WxMaSubscribeMessage.builder()
                .toUser(reqDTO.getToUser())
                .templateId(reqDTO.getTemplateId())
                .build();
        message.setMiniprogramState(reqDTO.getMiniProgramState());
        List<MsgData> dataList = reqDTO.getData();
        for (MsgData msgData : dataList) {
            WxMaSubscribeMessage.MsgData wxMsgData = new WxMaSubscribeMessage.MsgData();
            wxMsgData.setName(msgData.getName());
            wxMsgData.setValue(msgData.getValue());
            message.addData(wxMsgData);
        }
        message.setPage(reqDTO.getPage());
        return message;
    }


    private WxMpTemplateMessage initWxMpSubscribeMessage(SubscribeMessageReqDTO reqDTO) {
        List<MsgData> dataList = reqDTO.getData();
        List<WxMpTemplateData> data = new ArrayList<>();
        for (MsgData msgData : dataList) {
            WxMpTemplateData wxMpTemplateData = new WxMpTemplateData();
            wxMpTemplateData.setName(msgData.getName());
            wxMpTemplateData.setValue(msgData.getValue());
            data.add(wxMpTemplateData);
        }
        // -- 公众号跳转小程序
        WxMpTemplateMessage.MiniProgram miniProgram = new WxMpTemplateMessage.MiniProgram();
        if (reqDTO.getJumpWxMaFlag()) {
            // -- 插入appId
            SocialClientDO social = socialClientService.getBySocialType(reqDTO.getSocialType());
            miniProgram.setAppid(social.getClientId());
            miniProgram.setPagePath(reqDTO.getPage());
        }
        WxMpTemplateMessage templateMessage = WxMpTemplateMessage.builder()
                .toUser(reqDTO.getToUser())
                .miniProgram(miniProgram)
                .templateId(reqDTO.getTemplateId())
                .data(data)
                .build();
        return templateMessage;
    }
}
