package com.xjrsoft.module.oa.service.impl;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.xjrsoft.common.utils.VoToColumnUtil;
import com.xjrsoft.module.oa.mapper.WfMeetingApplyMapper;
import com.xjrsoft.module.oa.entity.WfMeetingApply;
import com.xjrsoft.module.oa.service.IWfMeetingApplyService;
import com.xjrsoft.module.organization.dto.WeChatSendMessageDto;
import com.xjrsoft.module.organization.entity.User;
import com.xjrsoft.module.organization.service.IUserService;
import com.xjrsoft.module.organization.service.IWeChatService;
import com.xjrsoft.module.workflow.entity.WorkflowExtra;
import com.xjrsoft.module.workflow.entity.WorkflowFormRelation;
import com.xjrsoft.module.workflow.service.IWorkflowExtraService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

/**
* @title: 物品申购
* @Author dzx
* @Date: 2024-02-19
* @Version 1.0
*/
@Service
@AllArgsConstructor
public class WfMeetingApplyServiceImpl extends MPJBaseServiceImpl<WfMeetingApplyMapper, WfMeetingApply> implements IWfMeetingApplyService {

    private final IUserService userService;

    private final IWeChatService weChatService;

    private final IWorkflowExtraService extraService;
    @Override
    public void noticeParticipants(Long id) {
        WfMeetingApply meetingApply = this.getById(id);
        if(meetingApply.getMeetingApplyParticipants() == null || meetingApply.getMeetingApplyParticipants().isEmpty()){
            return;
        }
        List<String> userIds = Arrays.asList(meetingApply.getMeetingApplyParticipants().split(","));

        List<User> userList = userService.list(new QueryWrapper<User>().lambda().in(User::getId, userIds).isNotNull(User::getOpenId));
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String startTime = meetingApply.getMeetingApplyS().format(formatter);
        String endTime = meetingApply.getMeetingApplyE().format(formatter);

        List<WorkflowExtra> list = extraService.list(
                new MPJLambdaWrapper<WorkflowExtra>()
                        .select(WorkflowExtra.class, x -> VoToColumnUtil.fieldsToColumns(WorkflowExtra.class).contains(x.getProperty()))
                        .innerJoin(WorkflowFormRelation.class, WorkflowFormRelation::getProcessId, WorkflowExtra::getProcessId)
                        .eq(WorkflowFormRelation::getFormId, id)
                        .orderByAsc(WorkflowExtra::getStartTime)
        );
        String startUserName = list.get(0).getStartUserName();

        for (User user : userList) {
            WeChatSendMessageDto weChatSendMessageDto = new WeChatSendMessageDto();
            weChatSendMessageDto.setUserId(user.getOpenId());
            weChatSendMessageDto.setTemplateId("Hign0b3e4WSXe__YmBKyDsBgAjkfNYC7c6yIUHjk3Hg");
            weChatSendMessageDto.setMsgId(IdUtil.getSnowflakeNextId() + "");
            JSONObject paramJson = new JSONObject();

            JSONObject thing2 = new JSONObject();
            thing2.put("value", meetingApply.getMeetingApplyTheme());
            paramJson.put("thing2", thing2);

            JSONObject time5 = new JSONObject();
            time5.put("value", startTime);
            paramJson.put("time5", time5);

            JSONObject time6 = new JSONObject();
            time6.put("value", endTime);
            paramJson.put("time6", time6);

            JSONObject thing7 = new JSONObject();
            thing7.put("value", meetingApply.getMeetingApplyAddress());
            paramJson.put("thing7", thing7);

            JSONObject thing8 = new JSONObject();
            thing8.put("value", startUserName);
            paramJson.put("thing8", thing8);

            weChatSendMessageDto.setContent(paramJson);
            weChatService.sendTemplateMessage(weChatSendMessageDto);
        }
    }
}
