package com.ruoyi.push.service.impl;


import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.code.PushConstant;
import com.ruoyi.enums.message.AppMessageEnums;
import com.ruoyi.enums.message.InteractionMessageEnums;
import com.ruoyi.enums.message.OrderMessageEnums;
import com.ruoyi.enums.message.PushEnums;
import com.ruoyi.push.PushGoeasyUtil;
import com.ruoyi.push.PushToSingle;
import com.ruoyi.push.service.PushService;
import com.ruoyi.utils.BusinessUtil;
import com.ruoyi.vo.UserVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author liwenlong
 * 推送
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class PushServiceImpl implements PushService {

    /**
     * 推送类型
     */
    @Value("${push.push_type}")
    private String pushType;

    @Autowired
    private PushGoeasyUtil pushGoeasyUtil;

    @Autowired
    private PushToSingle pushToSingle;


    @Autowired
    private BusinessUtil businessUtil;

    @Override
    public void push(UserVo userVo, Long objectId, Long messageId, AppMessageEnums appMessageEnums) {
        pushMessage(userVo,appMessageEnums.getCode(),appMessageEnums.getTitle(),appMessageEnums.getContent(),objectId,messageId, PushConstant.系统消息);
    }

    @Override
    public void push(UserVo userVo,String title,String content, Long objectId, Long messageId, AppMessageEnums appMessageEnums) {
        pushMessage(userVo,appMessageEnums.getCode(),title,content,objectId,messageId,PushConstant.系统消息);
    }

    @Override
    public void push(UserVo userVo, Long objectId, String orderNo, Long messageId, OrderMessageEnums orderMessageEnums) {
        pushMessage(userVo,orderMessageEnums.getCode(),orderMessageEnums.getTitle(),orderMessageEnums.getContent().replace("orderNo",orderNo),objectId,messageId,PushConstant.商城订单消息);
    }

    @Override
    public void push(UserVo userVo, Long objectId, Long messageId, InteractionMessageEnums interactionMessageEnums) {
        pushMessage(userVo,interactionMessageEnums.getCode(),interactionMessageEnums.getTitle(),interactionMessageEnums.getContent(),objectId,messageId,PushConstant.互动消息);
    }

    private void pushMessage(UserVo userVo, Integer messageType, String title, String content, Long objectId, Long messageId, Integer type){

        if (ObjectUtil.isEmpty(pushType)){
            return;
        }

        switch (PushEnums.getPushEnums(Integer.valueOf(pushType))){

            case goeasy:
                pushGoeasyUtil.pushMessage(userVo,messageType,title,content,objectId,messageId,type);
                break;

            case uniapp:

                //获取clientId
                String client_id =  (String) businessUtil.getUserFieldValue(userVo,"clientId");

                pushToSingle.sendToAlias(userVo,messageType,title,content,client_id,objectId,messageId,type);

                break;

            default:
                throw new IllegalStateException("Unexpected value: " + pushType);
        }
    }


}
