package cn.iocoder.yudao.module.appBase.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;

import cn.iocoder.yudao.module.appBase.domain.SysAnnouncement;
import cn.iocoder.yudao.module.appBase.mapper.SysAnnouncementMapper;
import cn.iocoder.yudao.module.appBase.service.ISysAnnouncementService;
import cn.iocoder.yudao.module.rescue.app.controller.admin.AnnouncementSocket;
import cn.iocoder.yudao.module.rescue.app.controller.admin.UserAnnouncementSocket;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 系统通知Service业务层处理
 *
 * @author zcy
 * @date 2023-09-21
 */
@Service
public class SysAnnouncementServiceImpl extends ServiceImpl<SysAnnouncementMapper, SysAnnouncement> implements ISysAnnouncementService
{

    @Autowired
    private AnnouncementSocket announcementSocket;
    @Autowired
    private UserAnnouncementSocket userAnnouncementSocket;

    /**
     * 查询系统通知
     *
     * @param id 系统通知主键
     * @return 系统通知
     */
    @Override
    public SysAnnouncement selectSysAnnouncementById(Long id)
    {
        return baseMapper.selectSysAnnouncementById(id);
    }

    /**
     * 查询系统通知列表
     *
     * @param sysAnnouncement 系统通知
     * @return 系统通知
     */
    @Override
    public IPage<SysAnnouncement> selectSysAnnouncementList(SysAnnouncement sysAnnouncement, Page<SysAnnouncement> page)
    {
        return baseMapper.selectSysAnnouncementList(sysAnnouncement, page);
    }

    /**
     * 新增系统通知
     *
     * @param sysAnnouncement 系统通知
     * @return 结果
     */
    @Override
    public int insertSysAnnouncement(SysAnnouncement sysAnnouncement)
    {
        return baseMapper.insert(sysAnnouncement);
    }

    @Override
    public void insertSysAnnouncements(SysAnnouncement sysAnnouncement) {
        if (CollectionUtil.isNotEmpty(sysAnnouncement.getToUserIds())){
            for (Long toUserId : sysAnnouncement.getToUserIds()) {
                sysAnnouncement.setId(null);
                sysAnnouncement.setToUserId(toUserId);
                sysAnnouncement.setIsRead("0");
                sysAnnouncement.setTenantId(1L);
                baseMapper.insert(sysAnnouncement);
                //socket通知
                try {
                    announcementSocket.sendMessage(JSONObject.toJSONString(sysAnnouncement),toUserId.toString());
                }catch (Exception e){
                    log.error(e.getMessage());
                }
                try{
                    JSONObject json =new JSONObject();
                    json.put("platform",new String[]{"android"});
                    JSONObject audience = new JSONObject();
                    audience.put("alias", new String[]{toUserId.toString()});
                    json.put("audience",audience);
                    JSONObject notification =new JSONObject();
                    notification.put("alert","道路救援新通知");
                    JSONObject notificationAndroid =new JSONObject();
                    notificationAndroid.put("alert","道路救援新通知");
                    notificationAndroid.put("title","道路救援新通知");
                    notificationAndroid.put("builder_id",1);
                    notificationAndroid.put("badge_set_num",1);
                    JSONObject dumpUrl =  new JSONObject();
                    dumpUrl.put("url","intent:#Intent;action=android.intent.action.MAIN;end");
                    notificationAndroid.put("intent",dumpUrl);
                    notification.put("android",notificationAndroid);


                    json.put("notification",notification);
                    //进行极光推送
                    //链式构建请求
                    HttpRequest.post("https://api.jpush.cn/v3/push")
                            .header(Header.AUTHORIZATION, "Basic NWIxYmRjMzNlODI5NmVlNWFkMzM2YzMwOmIwYTg4YWE2YmM5YzYxNTc4Yzk1MjIzYg==")//头信息，多个头信息多次调用此方法即可
                            .body(JSONObject.toJSONString(json))
                            .timeout(20000)//超时，毫秒
                            .execute().body();

                }catch (Exception ignored){

                }

            }
        }
    }




    @Override
    public void insertSysAnnouncements2(SysAnnouncement sysAnnouncement) {
        if (CollectionUtil.isNotEmpty(sysAnnouncement.getToUserIds())){
            for (Long toUserId : sysAnnouncement.getToUserIds()) {
                sysAnnouncement.setId(null);
                sysAnnouncement.setToUserId(toUserId);
                sysAnnouncement.setIsRead("0");
                baseMapper.insert(sysAnnouncement);
                //socket通知
                try {
                    userAnnouncementSocket.sendMessage(JSONObject.toJSONString(sysAnnouncement),toUserId.toString());
                }catch (Exception e){
                    log.error(e.getMessage());
                }
                try{
                    JSONObject json =new JSONObject();
                    json.put("platform",new String[]{"android"});
                    JSONObject audience = new JSONObject();
                    audience.put("alias", new String[]{toUserId.toString()});
                    json.put("audience",audience);
                    JSONObject notification =new JSONObject();
                    notification.put("alert","车辆信息通知");
                    JSONObject notificationAndroid =new JSONObject();
                    notificationAndroid.put("alert","车辆信息通知");
                    notificationAndroid.put("title","车辆信息通知");
                    notificationAndroid.put("builder_id",1);
                    notificationAndroid.put("badge_set_num",1);
                    JSONObject dumpUrl =  new JSONObject();
                    dumpUrl.put("url","intent:#Intent;action=android.intent.action.MAIN;end");
                    notificationAndroid.put("intent",dumpUrl);
                    notification.put("android",notificationAndroid);


                    json.put("notification",notification);
                    //进行极光推送
                    //链式构建请求
                    HttpRequest.post("https://api.jpush.cn/v3/push")
                            .header(Header.AUTHORIZATION, "Basic Zjg4NzlkNGM2ZjMwOTZhN2IwMjFhYThmOjNhZjM1OWVhYTE5YWY2Yzk1MTIyYTUxNw==")//头信息，多个头信息多次调用此方法即可
                            .body(JSONObject.toJSONString(json))
                            .timeout(20000)//超时，毫秒
                            .execute().body();

                }catch (Exception ignored){

                }

            }
        }
    }


    /**
     * 修改系统通知
     *
     * @param sysAnnouncement 系统通知
     * @return 结果
     */
    @Override
    public int updateSysAnnouncement(SysAnnouncement sysAnnouncement)
    {
        return baseMapper.updateById(sysAnnouncement);
    }

    /**
     * 批量删除系统通知
     *
     * @param ids 需要删除的系统通知主键
     * @return 结果
     */
    @Override
    public int deleteSysAnnouncementByIds(Long[] ids)
    {
        return baseMapper.deleteSysAnnouncementByIds(ids);
    }

    /**
     * 删除系统通知信息
     *
     * @param id 系统通知主键
     * @return 结果
     */
    @Override
    public int deleteSysAnnouncementById(Long id)
    {
        return baseMapper.deleteSysAnnouncementById(id);
    }
}
