package com.storlead.message.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.storlead.framework.common.result.Result;
import com.storlead.framework.util.LoginUserUtil;
import com.storlead.message.pojo.dto.MessageTemplateEventDTO;
import com.storlead.message.pojo.entity.MessageTemplateEventGroupEntity;
import com.storlead.message.pojo.entity.UserMessageNoticeConfigEntity;
import com.storlead.message.pojo.vo.MessageTemplateVO;
import com.storlead.message.service.MessageTemplateEventGroupService;
import com.storlead.message.service.UserMessageNoticeConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @program: sp-sales-platform
 * @description:
 * @author: chenkq
 * @create: 2025-04-25 11:26
 */
@RestController
@RequestMapping("/message/config")
@Api(tags = "消息: 用户消息设置")
public class UserMessageConfigApiController {

    @Resource
    private UserMessageNoticeConfigService userMessageNoticeConfigService;

    @Resource
    private MessageTemplateEventGroupService templateEventGroupService;

    @ApiOperation("保存用户配置详情")
    @PostMapping("setMessageConfig")
    public Result setMessageConfig(@RequestBody UserMessageNoticeConfigEntity entity) {

        LambdaQueryWrapper<UserMessageNoticeConfigEntity> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(UserMessageNoticeConfigEntity::getOwnerBy, LoginUserUtil.getCurrentUserId());
        queryWrapper.eq(UserMessageNoticeConfigEntity::getTemplateEventId,entity.getTemplateEventId());
        queryWrapper.eq(UserMessageNoticeConfigEntity::getTemplateDetailType,entity.getTemplateDetailType());
        queryWrapper.last(" limit 1");
        UserMessageNoticeConfigEntity o1 = userMessageNoticeConfigService.getOne(queryWrapper);
        if (Objects.nonNull(o1)) {
            entity.setId(o1.getId());
        }
        userMessageNoticeConfigService.saveOrUpdate(entity);
        return Result.ok();
    }

    @ApiOperation("分页查询")
    @PostMapping("listPage")
    public Result listPage(@RequestBody MessageTemplateEventDTO dto) {
        Page<MessageTemplateEventGroupEntity> page = new Page<>(dto.getPageIndex(),dto.getPageSize());
        LambdaQueryWrapper<MessageTemplateEventGroupEntity> queryWrapper = new LambdaQueryWrapper();
        if (Objects.nonNull(dto.getTemplateServiceType())) {
            queryWrapper.eq(MessageTemplateEventGroupEntity::getTemplateServiceType,dto.getTemplateServiceType());
        }
        if (Objects.nonNull(dto.getAppId())) {
            queryWrapper.eq(MessageTemplateEventGroupEntity::getAppId, dto.getAppId());
        }
        if (StrUtil.isNotBlank(dto.getBlurry())) {
            queryWrapper.like(MessageTemplateEventGroupEntity::getEventName,dto.getBlurry());
        }
        IPage<MessageTemplateVO> pageList = templateEventGroupService.listPage(page, queryWrapper);
        if (CollectionUtil.isNotEmpty(pageList.getRecords())) {
            List<Long> dt = pageList.getRecords().stream().map(MessageTemplateVO::getId).collect(Collectors.toList());
            LambdaQueryWrapper<UserMessageNoticeConfigEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.in(UserMessageNoticeConfigEntity::getTemplateEventId,dt);
            wrapper.eq(UserMessageNoticeConfigEntity::getOwnerBy, LoginUserUtil.getCurrentUserId());
            List<UserMessageNoticeConfigEntity>  detailEntities = userMessageNoticeConfigService.list(wrapper);
            Map<Long, List<UserMessageNoticeConfigEntity>> groupMap = detailEntities.stream().collect(Collectors.groupingBy(UserMessageNoticeConfigEntity::getTemplateEventId));
            pageList.getRecords().forEach(arg -> {
                List<UserMessageNoticeConfigEntity> rs =  groupMap.get(arg.getId());
                arg.setUserDetails(Objects.isNull(rs) ? Collections.emptyList() : rs);
            });
        }
        return Result.ok(pageList);
    }

    @ApiOperation("消息开启关闭")
    @PostMapping("on_off_message_config")
    public Result onOffMessageConfig(@RequestBody UserMessageNoticeConfigEntity entity) {

        //    @ApiModelProperty(value = "site:站内,sms:短信,wecom:企业微信,mail:邮件")
        List<String> templateDetailTypels = CollectionUtils.isEmpty(entity.getTemplateDetailTypels()) ? Arrays.asList("site","wecom","mail") : entity.getTemplateDetailTypels();
        for (String templateDetailType : templateDetailTypels) {
            LambdaQueryWrapper<UserMessageNoticeConfigEntity> queryWrapper = new LambdaQueryWrapper();
            queryWrapper.eq(UserMessageNoticeConfigEntity::getOwnerBy,LoginUserUtil.getCurrentUserId());
            queryWrapper.eq(UserMessageNoticeConfigEntity::getTemplateEventId,entity.getTemplateEventId());
            queryWrapper.eq(UserMessageNoticeConfigEntity::getTemplateDetailType,templateDetailType);
            queryWrapper.last(" limit 1");
            UserMessageNoticeConfigEntity o1 = userMessageNoticeConfigService.getOne(queryWrapper);
            if (Objects.nonNull(o1)) {
                entity.setId(o1.getId());
            }
            userMessageNoticeConfigService.saveOrUpdate(entity);
        }
        return Result.ok();
    }
}
