package com.tzld.piaoquan.offline.job;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tzld.piaoquan.growth.common.common.enums.MessageAttachmentTypeEnum;
import com.tzld.piaoquan.growth.common.dao.mapper.MomentSendMessageMapper;
import com.tzld.piaoquan.growth.common.dao.mapper.StaffMapper;
import com.tzld.piaoquan.growth.common.dao.mapper.UserWithTagMapper;
import com.tzld.piaoquan.growth.common.model.po.MessageAttachment;
import com.tzld.piaoquan.growth.common.model.po.MomentSendMessage;
import com.tzld.piaoquan.growth.common.model.po.MomentSendMessageExample;
import com.tzld.piaoquan.growth.common.model.po.Staff;
import com.tzld.piaoquan.growth.common.service.MessageAttachmentService;
import com.tzld.piaoquan.growth.common.service.MessageService;
import com.tzld.piaoquan.growth.common.utils.DateUtil;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;


@Component
public class WeComMomentDataJob {

    @Autowired
    private StaffMapper staffMapper;

    @Autowired
    private MessageService messageService;

    @Autowired
    private MessageAttachmentService messageAttachmentService;

    @Autowired
    UserWithTagMapper userWithTagMapper;

    @Autowired
    private MomentSendMessageMapper momentSendMessageMapper;


    @XxlJob("sendMomentMessageJob")
    public ReturnT<String> sendMomentMessage(String param) {
        String thatDayDateString = DateUtil.getThatDayDateString();
        MomentSendMessageExample example = new MomentSendMessageExample();
        example.createCriteria().andPreSendDateEqualTo(thatDayDateString).andIsSendEqualTo(0);
        List<MomentSendMessage> momentSendMessages = momentSendMessageMapper.selectByExample(example);
        for (MomentSendMessage momentSendMessage : momentSendMessages) {
            Staff staff = staffMapper.selectByPrimaryKey(momentSendMessage.getStaffId());
            JSONObject jsonObject = new JSONObject();
            JSONObject text = new JSONObject();
            if (StringUtils.isNotEmpty(momentSendMessage.getContent())) {
                text.put("content", momentSendMessage.getContent());
                jsonObject.put("text", text);
            }
            String attachmentIds = momentSendMessage.getAttachmentIds();
            JSONArray attachments = new JSONArray();
            if (StringUtils.isNotEmpty(attachmentIds)) {
                List<Long> attachmentIdList = JSONArray.parseArray(attachmentIds, Long.class);
                if (!CollectionUtils.isEmpty(attachmentIdList)) {
                    List<MessageAttachment> messageAttachments = messageAttachmentService.getMessageAttachment(attachmentIdList);
                    if (!CollectionUtils.isEmpty(messageAttachments)) {
                        for (MessageAttachment messageAttachment : messageAttachments) {
                            Integer type = messageAttachment.getType();
                            if (Objects.equals(MessageAttachmentTypeEnum.LINK.getCode(), type)) {
                                JSONObject linkAttachment = new JSONObject();
                                JSONObject link = new JSONObject();
                                link.put("title", messageAttachment.getTitle());
                                link.put("media_id", messageAttachment.getMediaId());
                                link.put("url", messageAttachment.getUrl());
                                linkAttachment.put("msgtype", "link");
                                linkAttachment.put("link", link);
                                attachments.add(linkAttachment);
                            }

                            if (Objects.equals(MessageAttachmentTypeEnum.IMAGE.getCode(), type)) {
                                JSONObject imageAttachment = new JSONObject();
                                JSONObject image = new JSONObject();
                                image.put("media_id", messageAttachment.getMediaId());
                                imageAttachment.put("msgtype", "image");
                                imageAttachment.put("image", image);
                                attachments.add(imageAttachment);
                            }

                            if (Objects.equals(MessageAttachmentTypeEnum.VIDEO.getCode(), type)) {
                                JSONObject videoAttachment = new JSONObject();
                                JSONObject image = new JSONObject();
                                image.put("media_id", messageAttachment.getMediaId());
                                videoAttachment.put("msgtype", "video");
                                videoAttachment.put("video", image);
                                attachments.add(videoAttachment);
                            }
                        }

                    }
                }
            }
            if (!attachments.isEmpty()) {
                jsonObject.put("attachments", attachments);
            }
            JSONObject visibleRange = new JSONObject();
            JSONObject senderList = new JSONObject();
            JSONArray userList = new JSONArray();
            userList.add(staff.getCarrierId());
            senderList.put("user_list", userList);
            visibleRange.put("sender_list", senderList);
            jsonObject.put("visible_range", visibleRange);
            boolean flag = messageService.pushMomentMessage(jsonObject, staff.getCorpId());
            if (flag) {
                momentSendMessage.setIsSend(1);
                momentSendMessageMapper.updateByPrimaryKeySelective(momentSendMessage);
            }
        }
        return ReturnT.SUCCESS;
    }
}
