package com.fs.gtPush.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fs.common.utils.DateUtils;
import com.fs.common.utils.bean.BeanUtils;
import com.fs.gtPush.domain.PushReqBean;
import com.fs.gtPush.domain.UniPushLog;
import com.fs.gtPush.mapper.PushLogMapper;
import com.fs.gtPush.service.UniPushLogService;
import com.fs.gtPush.vo.UniPushLogAppVo;
import com.fs.gtPush.vo.UniPushLogLastAndReadStatusVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * uni推送消息记录Service业务层处理
 *
 * @author fs
 * @date 2025-03-12
 */
@Service
@Slf4j
public class UniPushLogServiceImpl extends ServiceImpl<PushLogMapper, UniPushLog> implements UniPushLogService {

    /**
     * 查询uni推送消息记录
     *
     * @param id uni推送消息记录主键
     * @return uni推送消息记录
     */
    @Override
    public UniPushLog selectUniPushLogById(String id) {
        return baseMapper.selectPushLogById(id);
    }

    /**
     * 查询uni推送消息记录列表
     *
     * @param UniPushLog uni推送消息记录
     * @return uni推送消息记录
     */
    @Override
    public List<UniPushLog> selectUniPushLogList(UniPushLog UniPushLog) {
        return baseMapper.selectPushLogList(UniPushLog);
    }

    /**
     * 新增uni推送消息记录
     *
     * @param UniPushLog uni推送消息记录
     * @return 结果
     */
    @Override
    public int insertUniPushLog(UniPushLog UniPushLog) {
        UniPushLog.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insertPushLog(UniPushLog);
    }

    /**
     * 修改uni推送消息记录
     *
     * @param UniPushLog uni推送消息记录
     * @return 结果
     */
    @Override
    public int updateUniPushLog(UniPushLog UniPushLog) {
        UniPushLog.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updatePushLog(UniPushLog);
    }

    /**
     * 批量删除uni推送消息记录
     *
     * @param ids 需要删除的uni推送消息记录主键
     * @return 结果
     */
    @Override
    public int deleteUniPushLogByIds(String[] ids) {
        return baseMapper.deletePushLogByIds(ids);
    }

    /**
     * 删除uni推送消息记录信息
     *
     * @param id uni推送消息记录主键
     * @return 结果
     */
    @Override
    public int deleteUniPushLogById(String id) {
        return baseMapper.deletePushLogById(id);
    }

    /**
     * 查询uni推送消息记录最新记录,且是否含有未读消息
     *
     * @param userId
     * @return vo
     */
    @Override
    public UniPushLogLastAndReadStatusVo getLastAndReadStatus(Long userId) {
        UniPushLogLastAndReadStatusVo vo = new UniPushLogLastAndReadStatusVo();
        if (userId == null) {
            vo.setUnReadNum(0);
        } else {
            UniPushLog log = baseMapper.getLast(userId);
            if (log != null) {
                BeanUtils.copyProperties(log, vo);
                analysisParam(log, vo);
                //查询未读条数
                Integer num = baseMapper.getNumByIsRead(userId);
                vo.setUnReadNum(num);
            }
        }
        return vo;
    }

    private static void analysisParam(UniPushLog pushLog, UniPushLogAppVo vo) {
        //解析内容
        String pushMsg = pushLog.getPushMsg();
        if (StringUtils.isNotBlank(pushMsg)) {
            try {
                PushReqBean pushParam = JSONUtil.toBean(pushMsg, PushReqBean.class);
                vo.setTitle(pushParam.getTitle());
                vo.setContent(pushParam.getContent());
                Map<String, Object> payload = pushParam.getPayload();
                if (payload != null && !payload.isEmpty()) {
                    if (payload.get("url") != null) {
                        vo.setUrl(payload.get("url").toString());
                    }
                    if (payload.get("imgUrl") != null) {
                        String[] imgUrls = payload.get("imgUrl").toString().split(",");
                        vo.setImgUrl(imgUrls[0]);
                    }
                }
            } catch (Exception e) {
                log.info("解析推送参数错误,id为{}", pushLog.getId());
            }
        }
    }

    /**
     * 设置已读
     *
     * @param logIds
     * @return num
     */
    @Override
    public Integer setIsRead(List<Long> logIds, Long userId) {
        return baseMapper.setIsRead(logIds, userId);
    }

    @Override
    public List<UniPushLogAppVo> selectUniPushLogListApp(UniPushLog uniPushLog) {
        List<UniPushLog> uniPushLogs = selectUniPushLogList(uniPushLog);
        ArrayList<UniPushLogAppVo> res = new ArrayList<>();
        for (UniPushLog log : uniPushLogs) {
            UniPushLogAppVo vo = new UniPushLogAppVo();
            BeanUtils.copyProperties(log, vo);
            analysisParam(log, vo);
            res.add(vo);
        }
        return res;
    }
}
