package com.rongwei.savecheck.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.rongwe.scentity.vo.PushMessageToAppRequestVo;
import com.rongwei.rwcommon.base.R;
import com.rongwei.safecommon.utils.CXCommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：sc
 * @since ：2024/2/5
 */
@RestController
@RequestMapping("app/pushMessage")
@Slf4j
public class AppPushMessageController {
    /**
     * 旧版批量替换沿用版本post，url传参
     * @param message
     */
    @PostMapping("/old/pushToApp")
    public R pushToWeb(@RequestParam String message){
        try {
            JSONObject jsonObject = JSONObject.parseObject(message);
            String title = jsonObject.getString("title");
            String content = jsonObject.getString("content");
            List<String> userIdList = JSON.parseArray(jsonObject.getJSONArray("userIds").toJSONString(), String.class);
            Lists.partition(userIdList, 10).forEach(userIds -> CXCommonUtils.pushApp(title, content, userIds));
            return R.ok("发送app通知成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("发送app通知失败");
        }

    }

    /**
     * 新版可用此方式，post实体类传参
     */
    @PostMapping("/new/pushToApp")
    public R pushToWeb(@RequestBody PushMessageToAppRequestVo model){
        try {
            String title = model.getTitle();
            String content = model.getContent();
            List<String> userIdList = model.getUserIds();
            Lists.partition(userIdList, 10).forEach(userIds -> CXCommonUtils.pushApp(title, content, userIds));
            return R.ok("发送app通知成功");
        } catch (Exception e) {
            e.printStackTrace();
            return R.error("发送app通知失败");
        }
    }




}
