package com.usoftchina.uas.office.dingtalk.listener;

import com.usoftchina.dingtalk.sdk.ScheduleSdk;
import com.usoftchina.dingtalk.sdk.dto.AddScheduleReq;
import com.usoftchina.dingtalk.sdk.util.UrlUtils;
import com.usoftchina.uas.office.context.MasterHolder;
import com.usoftchina.uas.office.dingtalk.entity.Meeting;
import com.usoftchina.uas.office.dingtalk.manage.entity.DingTalkAgent;
import com.usoftchina.uas.office.dingtalk.manage.service.DingTalkAgentService;
import com.usoftchina.uas.office.dingtalk.service.UasMeetingService;
import com.usoftchina.uas.office.dto.UasEvent;
import com.usoftchina.uas.office.entity.DataCenter;
import com.usoftchina.uas.office.jdbc.DataSourceHolder;
import com.usoftchina.uas.office.listener.UasEventListener;
import com.usoftchina.uas.office.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.stream.Collectors;

/**
 * @author yingp
 * @date 2020/2/16
 */
@Component
public class UasMeetingListener {
    @Autowired
    private ScheduleSdk scheduleSdk;
    @Autowired
    private UasMeetingService meetingService;
    @Autowired
    private DingTalkAgentService agentService;

    /**
     * uas会议申请审核通过，写入企业微信日程
     *
     * @param event
     */
    @UasEventListener(caller = "Meetingroomapply", operation = "AUDIT")
    public void onMeetingAudit(UasEvent event) throws Exception{
        Meeting meeting = meetingService.getById((int) event.getKey());
        if (null != meeting) {
            DingTalkAgent uasAgent = getUasAgent();
            String sourceUrl = UrlUtils.generateAuthUrl(scheduleSdk.getCorpId(), event.getMaster(), uasAgent.getCode(), uasAgent.getOuterUrl(),
                    DataCenter.INSTANCE.getOuterUrl() + "authorize",
                    String.format("jsps/oa/meeting/meetingroomapply.jsp?formCondition=ma_idIS%s&gridCondition=md_maidIS%s", meeting.getMa_id(), meeting.getMa_id()));
            AddScheduleReq req = new AddScheduleReq()
                    .organizer(meeting.getEm_ding())
                    .during(meeting.getMa_starttime().getTime(), meeting.getMa_endtime().getTime())
                    .summary("会议-" + meeting.getMa_theme())
                    .bizId(String.valueOf(meeting.getMa_id()))
                    .remindBefore(900)
                    .source("UAS系统", sourceUrl);
            if (null != meeting.getRoom()) {
                req.location(meeting.getRoom().getMr_site() + " " + meeting.getRoom().getMr_name());
            }
            if (!StringUtils.isEmpty(meeting.getMa_remark())) {
                req.description(meeting.getMa_remark());
            }
            if (!CollectionUtils.isEmpty(meeting.getAttendees())) {
                req.attendees(meeting.getAttendees().stream().map(Meeting.MeetingAttendee::getEm_ding).collect(Collectors.toList()));
            }
            String scheduleId = scheduleSdk.addSchedule("Uas", req);
            meeting.setMa_scheduleid(scheduleId);
            meetingService.setScheduleId(meeting);
        }
    }

    private DingTalkAgent getUasAgent() {
        try {
            DataSourceHolder.set(DataCenter.INSTANCE);
            return agentService.findByCode("Uas");
        } finally {
            DataSourceHolder.set(MasterHolder.get());
        }
    }
}
