package org.jeecg.modules.adweb.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import io.swagger.v3.oas.annotations.tags.Tag;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;

import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.adweb.common.jedis.ObjectUtil;
import org.jeecg.modules.adweb.common.util.JedisUtil;
import org.jeecg.modules.adweb.enquiry.constant.EnquiryConstants;
import org.jeecg.modules.adweb.enquiry.service.EnquiryRedisService;
import org.jeecg.modules.adweb.enquiry.service.IEnquiryEmailMessageService;
import org.jeecg.modules.adweb.system.service.SysAdwebApi;
import org.jeecg.modules.system.entity.SysRole;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.entity.SysUserRole;
import org.jeecg.modules.system.service.ISysRoleService;
import org.jeecg.modules.system.service.ISysUserRoleService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: adweb 系统api
 * @Author: jeecg-boot
 * @Date: 2024-09-27
 * @Version: V1.0
 */
@Tag(name = "adweb 系统api")
@RestController
@RequestMapping("/adweb/system")
@Slf4j
public class SystemAdwebController {

    @Resource private SysAdwebApi sysAdwebApi;

    @Resource private ISysUserService iSysUserService;

    @Resource private ISysRoleService iSysRoleService;

    @Resource private ISysUserRoleService iSysUserRoleService;

    @Resource private EnquiryRedisService enquiryRedisService;

    @Resource private IEnquiryEmailMessageService enquiryEmailMessageService;

    private static final byte[] redisKey = EnquiryConstants.ENQUIRY_EMAIL.getBytes();

    /**
     * 获取租户列表
     *
     * @return
     */
    @GetMapping("/getChannelProvider")
    public Result<?> getChannelProvider() {
        List<SysUser> userList = null;
        // 只针对admin 获取 渠道商列表
        if (sysAdwebApi.isAdmin()) {
            // 获取role表里角色编码为adweb_oem的角色的id
            List<SysRole> sysRoleList =
                    iSysRoleService.list(
                            new LambdaQueryWrapper<SysRole>()
                                    .eq(SysRole::getRoleCode, "adweb_oem"));
            if (sysRoleList.isEmpty()) {
                return Result.error("获取渠道商列表失败");
            }
            SysRole role = sysRoleList.get(0);
            // 根据adweb_oem的角色的id去查user_role中间表中所有为此角色id的user_id
            List<SysUserRole> sysUserRoleList =
                    iSysUserRoleService.list(
                            new LambdaQueryWrapper<SysUserRole>()
                                    .eq(SysUserRole::getRoleId, role.getId()));
            if (sysUserRoleList.isEmpty()) {
                return Result.error("获取渠道商列表失败");
            }
            List<String> userIdList =
                    sysUserRoleList.stream()
                            .map(SysUserRole::getUserId)
                            .collect(Collectors.toList());
            // 查询user表中所有上面查询出的user_id的数据
            userList =
                    iSysUserService.list(
                            new LambdaQueryWrapper<SysUser>().in(SysUser::getId, userIdList));
        }
        return Result.OK(userList);
    }

    @PostConstruct
    public void startEnquiryRedisListener() {
        enquiryRedisService.init();
        enquiryRedisService.startEnquiryEmailMessageListener(
                EnquiryConstants.ENQUIRY_EMAIL_CHANNEL, enquiryEmailMessageService);
    }

    /**
     * 获取渠道商列表
     *
     * @return
     */
    @GetMapping("/testSendEmail")
    public Result<?> testSendEmail() {
        try {
            JedisUtil.lpush(redisKey, ObjectUtil.object2Bytes(1311742895508602880L));
            JedisUtil.publishMsg(
                    EnquiryConstants.ENQUIRY_EMAIL_CHANNEL, EnquiryConstants.ENQUIRY_EMAIL);
        } catch (IOException e) {
            log.info("测试发送监听发送邮件的消息失败");
        }

        //        enquiryEmailMessageService.sendEnquiryEmail(1311742895508602880L);

        return Result.OK("");
    }
}
