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.conditions.update.LambdaUpdateWrapper;
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.mybatis.entity.SysBaseField;
import com.storlead.message.pojo.entity.MessageTemplateEventDetailEntity;
import com.storlead.message.pojo.entity.MessageTemplateEventGroupEntity;
import com.storlead.message.pojo.vo.MessageArgTemplateVO;
import com.storlead.message.pojo.vo.MessageTemplateVO;
import com.storlead.message.service.MessageTemplateEventDetailService;
import com.storlead.message.service.MessageTemplateEventGroupService;
import com.storlead.message.pojo.dto.MessageTemplateEventDTO;
import com.storlead.message.util.MessageArgTemplateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
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.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @program: sp-sales-platform
 * @description:
 * @author: chenkq
 * @create: 2024-04-26 15:15
 */
@RestController
@RequestMapping("/message/template")
@Api(tags = "消息: 消息模板机制配置")
public class MessageTemplateApiController {

    @Resource
    private MessageTemplateEventGroupService templateEventGroupService;

    @Resource
    private MessageTemplateEventDetailService templateEventDetailService;

    @ApiOperation("保存模板")
    @PostMapping("save")
    public Result save(@RequestBody MessageTemplateEventGroupEntity groupEntity) {

//      groupEntity.dictFiled();
        templateEventGroupService.saveOrUpdate(groupEntity);
        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<MessageTemplateEventDetailEntity> wrapper = new LambdaQueryWrapper<>();
            wrapper.in(MessageTemplateEventDetailEntity::getTemplateEventId,dt);
            List<MessageTemplateEventDetailEntity>  detailEntities = templateEventDetailService.list(wrapper);
            Map<Long, List<MessageTemplateEventDetailEntity>> groupMap = detailEntities.stream().collect(Collectors.groupingBy(MessageTemplateEventDetailEntity::getTemplateEventId));
            pageList.getRecords().forEach(arg -> {
                arg.setDetailEntities(groupMap.get(arg.getId()));
            });
            SysBaseField.dictFileds(pageList.getRecords());
        }
        return Result.ok(pageList);
    }

    @ApiOperation("保存模板详情")
    @PostMapping("saveTempleteContent")
    public Result saveTempleteContent(@RequestBody MessageTemplateEventDetailEntity entity) {
        templateEventDetailService.saveOrUpdate(entity);
        return Result.ok();
    }

    @ApiOperation("获取消息模板详情")
    @PostMapping("getGroupDetail")
    public Result getGroupDetail(Long id,String templateDetailType) {
        MessageTemplateEventGroupEntity templateEventGroup =  templateEventGroupService.getById(id);
        if (Objects.isNull(templateEventGroup)) {
            return Result.ok();
        }
        MessageTemplateVO templateVO = new MessageTemplateVO();
        BeanUtils.copyProperties(templateEventGroup,templateVO);
        LambdaQueryWrapper<MessageTemplateEventDetailEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MessageTemplateEventDetailEntity::getTemplateEventId,templateEventGroup.getId());
        if (Objects.nonNull(templateDetailType)) {
            wrapper.eq(MessageTemplateEventDetailEntity::getTemplateDetailType,templateDetailType);
        }
        List<MessageTemplateEventDetailEntity>  detailEntities = templateEventDetailService.list(wrapper);
        templateVO.setDetailEntities(detailEntities);
        templateVO.dictFiled();

        List<MessageArgTemplateVO> argTemplatels = MessageArgTemplateUtil.parseArgTemplateJson(templateEventGroup.getArgTemplateJson());
        templateVO.setArgls(argTemplatels);
        return Result.result(templateVO);
    }
    @ApiOperation("获取消息模板详情")
    @PostMapping("getDetail")
    public Result getDetail(Long id) {
        MessageTemplateEventDetailEntity detailEntity =  templateEventDetailService.getById(id);
        return Result.result(detailEntity);
    }

    @ApiOperation("禁用或启用模型")
    @PostMapping("enabledDetail")
    public Result enabledTemplateDetail(Long id,Boolean enable) {
        LambdaUpdateWrapper<MessageTemplateEventDetailEntity> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(MessageTemplateEventDetailEntity::getEnabled,enable);
        updateWrapper.eq(MessageTemplateEventDetailEntity::getId,id);
        templateEventDetailService.update(updateWrapper);
        return Result.ok();
    }

    @ApiOperation("删除模板")
    @PostMapping("delete")
    public Result delete(Long id) {
        if(Objects.isNull(id)) {
            return Result.error("参数错误");
        }
        templateEventGroupService.lgDelete(id);
        return Result.ok();
    }
}
