InBoxServiceImpl.java 4.73 KB
package com.yohoufo.user.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.yoho.core.common.utils.DateUtil;
import com.yoho.error.ServiceError;
import com.yoho.error.exception.ServiceException;
import com.yohoufo.common.enums.InboxBusinessTypeEnum;
import com.yohoufo.common.enums.InboxTypeEnum;
import com.yohoufo.dal.user.IInBoxDao;
import com.yohoufo.dal.user.model.InBox;
import com.yohoufo.dal.user.model.TypeCountInbox;
import com.yohoufo.user.requestVO.InboxReqVO;
import com.yohoufo.user.requestVO.ListInboxTypeInfoReqVO;
import com.yohoufo.user.requestVO.ListInboxReqVO;
import com.yohoufo.user.responseVO.PageResponseVO;
import com.yohoufo.user.service.IInBoxService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * Created by shengguo.cai on 2018/9/11.
 */
@Service
public class InBoxServiceImpl implements IInBoxService {
    private static Logger log = LoggerFactory.getLogger(InBoxServiceImpl.class);
    @Autowired
    private IInBoxDao inBoxDao;

    @Override
    public JSONArray listInboxTypeInfo(ListInboxTypeInfoReqVO reqBO) {
        log.info("listInboxTypeInfo begin.param is {}",reqBO);
        Integer uid = reqBO.getUid();
        Map<Integer,TypeCountInbox> typeCountMap = inBoxDao.selectTypeCount(getTableName(uid), uid, "N", "N");
        JSONArray result = new JSONArray();
        for(InboxTypeEnum typeEnum : InboxTypeEnum.values()) {
            JSONObject obj = new JSONObject();
            obj.put("type", typeEnum.getId());
            obj.put("imgUrl", typeEnum.getImgUrl());
            obj.put("description", typeEnum.getDescription());
            obj.put("unReadCount", typeCountMap.containsKey(typeEnum.getId()) ? typeCountMap.get(typeEnum.getId()).getCount() : 0);
            result.add(obj);
        }
        return result;
    }

    @Override
    public PageResponseVO<InBox> listInboxByTypes(ListInboxReqVO reqVO) {
        log.info("entre listInboxByTypes with param is {}", reqVO);
        //判断为空
        if (reqVO.getUid() <= 0) {
            log.warn("listInboxByTypes error because uid is null with param is {}", reqVO);
            throw new ServiceException(ServiceError.SMS_INBOX_UID_NULL);
        }
        PageResponseVO<InBox> response = new PageResponseVO<>();

        int total = inBoxDao.selectTotalInboxs(getTableName(reqVO.getUid()),reqVO.getType(),
                reqVO.getUid());
        if(total == 0){
            log.info("listInboxByTypes query inbox is empty with param is {}", reqVO);
            response.setPage(reqVO.getPage());
            response.setTotal(0);
            return response;
        }
        List<InBox> inBoxes = inBoxDao.selectInboxs(getTableName(reqVO.getUid()),reqVO.getType(),
                reqVO.getUid(),reqVO.getRowNo(),reqVO.getLimit());
        //如果根据类型查看,设置未读为已读
        if(null != reqVO.getType()){
            log.info("listInboxByTypes updateReadedByUidAndType param is {}", reqVO);
            inBoxDao.updateReadedByUidAndType(getTableName(reqVO.getUid()),reqVO.getUid(),reqVO.getType(),DateUtil.getCurrentTimeSecond());
        }
        response.setList(inBoxes);
        response.setPage(reqVO.getPage());
        response.setSize(reqVO.getLimit());
        response.setTotal(total);
        return response;
    }

    @Override
    public void addInbox(InboxReqVO reqVO) {
        log.info("entre addInbox with param is {}", reqVO);
        InboxBusinessTypeEnum businessTypeEnum = InboxBusinessTypeEnum.getByTypeAndBusinessType(reqVO.getType(),reqVO.getBusinessType());
        if(businessTypeEnum == null){
            log.warn("addInbox businessTypeEnum is null.param is {}",reqVO);
            throw new ServiceException(ServiceError.ERROR);//TODO 自定义
        }
        InBox inBox = new InBox();
        inBox.setUid(reqVO.getUid());
        inBox.setType(reqVO.getType());
        inBox.setIsRead(InBox.N);
        inBox.setIsDel(InBox.N);
        inBox.setTitle(businessTypeEnum.getTitle());
        inBox.setCreateTime(DateUtil.getCurrentTimeSecond());
        inBox.setContent(createContent(businessTypeEnum.getContent(),reqVO.getParams()));
        inBox.setBusinessType(reqVO.getBusinessType());
        inBoxDao.insertInbox(getTableName(inBox.getUid()),inBox);
    }

    private String createContent(String template, List<String> params){
        if(params==null){
            return template;
        }
        for(String param : params){
            template=template.replaceFirst("\\{\\}",param);
        }
        return template;
    }

    private String getTableName(Integer uid) {
        return "inbox_" + uid % 10;
    }

}