package com.liquidnet.service.sweet.service.impl;

import com.liquidnet.commons.lang.util.CollectionUtil;
import com.liquidnet.commons.lang.util.IDGenerator;
import com.liquidnet.commons.lang.util.StringUtil;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.base.SqlMapping;
import com.liquidnet.service.base.constant.MQConst;
import com.liquidnet.service.sweet.config.WechatMaConfigure;
import com.liquidnet.service.sweet.utils.QueueUtils;
import com.liquidnet.service.sweet.utils.WechatUsersRedisUtils;
import com.liquidnet.service.sweet.vo.SweetMaoDengVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;

@Service
public class SweetWechatMaoDengServiceImpl {

    @Autowired
    private WechatUsersRedisUtils redisUtils;
    @Autowired
    QueueUtils queueUtils;
    @Autowired
    WechatMaConfigure maConfigure;

    public ResponseDto addBuilderImg(String userId,String imgUrl){
        String maoId = IDGenerator.nextSnowId();
        SweetMaoDengVo sweetMaoDeng = redisUtils.getSweetMaoDengTransfer(userId);
        if (sweetMaoDeng != null){
            sweetMaoDeng.setImgUrl(imgUrl);
        }
        String textMsg = sweetMaoDeng.getTextMsg();
        String picUrl = sweetMaoDeng.getPicUrl();
        sweetMaoDeng.setImgUrl(imgUrl);
        sweetMaoDeng.setMaoId(maoId);
        redisUtils.setSweetMaoDeng(sweetMaoDeng);
        if (StringUtil.isNotEmpty(textMsg) && StringUtil.isNotEmpty(picUrl)){
            //redis队列入库
            LinkedList<String> sqls = CollectionUtil.linkedListString();
            LinkedList<Object[]> sqlsDataA = CollectionUtil.linkedListObjectArr();
            sqls.add(SqlMapping.get("sweet_wechat_maodeng.insert"));
            sqlsDataA.add(new Object[]{maoId, userId, textMsg, picUrl,imgUrl});
            queueUtils.sendMsgByRedis(MQConst.SweetQueue.SWEET_WECHAT_MAODENG_TEXT_INSERT.getKey(), SqlMapping.gets(sqls, sqlsDataA));
        }

        return ResponseDto.success();
    }
}
