/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ifish.helper;

import com.ifish.bean.PageResult;
import com.ifish.bean.Tbl_Push_List;
import com.ifish.enums.ResultEnum;
import com.ifish.mapper.Tbl_Push_List_Mapper;
import com.ifish.util.IfishUtil;
import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 *
 * @author Administrator
 */
@Component
public class PushMessageHelper implements PushMessageHelperI {

    @Autowired
    private Tbl_Push_List_Mapper tbl_Push_List_Mapper;

    @Autowired
    private RedisHelperI redisHelperI;

    @Autowired
    private RedisKeyHelperI redisKeyHelperI;

    /**
     * 根据条件获取推送列表
     *
     * @param pushId
     * @param userId
     * @param firstResult
     * @param pageSize
     * @return
     */
    @Override
    public Object getPushList(String pushType, Integer userId, Integer firstResult, Integer pageSize) {
        try {
            if (firstResult == null) {
                firstResult = 0;
            }
            if (pageSize == null) {
                pageSize = 10;
            }
            if (userId == null) {
                return IfishUtil.returnJson(ResultEnum.error401.getKey(), "");
            }
            List<Tbl_Push_List> list = tbl_Push_List_Mapper.getPushList(userId, pushType, pageSize, firstResult);
            int count = tbl_Push_List_Mapper.getPushListCount(userId, pushType, pageSize, firstResult);
            PageResult page = new PageResult();
            page.setList(list);
            page.setPageNo(firstResult);
            page.setPageSize(pageSize);
            page.setTotalCount(count);
            return IfishUtil.returnPageData(page, ResultEnum.success.getKey());
        } catch (Exception e) {
            return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
        }
    }

    /**
     * 删除推送消息
     *
     * @param userId
     * @param pushId
     * @return
     */
    @Override
    public Object deletePushList(Integer userId, Integer pushId) {
        try {
            Tbl_Push_List tpl = getPush_ListById(pushId);
            if (tpl.getUserId().equals(userId)) {
                int i = tbl_Push_List_Mapper.deletePushListById(pushId);
                if (i > 0) {
                    return IfishUtil.returnJson(ResultEnum.success.getKey(), "");
                }
            }
            return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
        } catch (Exception e) {
            return IfishUtil.returnJson(ResultEnum.fail101.getKey(), "");
        }
    }

    /**
     * 根据ID从缓存中获取推送消息对象
     *
     * @param pushId
     * @return
     * @throws Exception
     */
    public Tbl_Push_List getPush_ListById(Integer pushId) throws Exception {
        Tbl_Push_List list = null;
        String key = redisKeyHelperI.getTbl_Push_List_RedisByPushId(pushId);
        String redisString = redisHelperI.getRedis(key);
        if (StringUtils.isNotBlank(redisString)) {
            list = (Tbl_Push_List) IfishUtil.JsonToBean(redisString, Tbl_Push_List.class);
        } else {
            list = tbl_Push_List_Mapper.getPush_ListById(pushId);
            if (list != null && list.getPushId() > 0) {
                redisHelperI.setRedis(key, IfishUtil.ObjectToJson(list));
            }
        }
        return list;
    }
}
