package com.mcwl.resource.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcwl.common.constant.HttpStatus;
import com.mcwl.common.constant.QueueConstants;
import com.mcwl.common.core.domain.entity.SysUser;
import com.mcwl.common.core.page.PageDomain;
import com.mcwl.common.core.page.TableDataInfo;
import com.mcwl.common.utils.SecurityUtils;
import com.mcwl.common.utils.StringUtils;
import com.mcwl.resource.domain.WorkFlow;
import com.mcwl.resource.domain.WorkFlowLike;
import com.mcwl.resource.domain.vo.ModelLikeVo;
import com.mcwl.resource.domain.vo.WorkFlowLikeVo;
import com.mcwl.resource.mapper.WorkFlowLikeMapper;
import com.mcwl.resource.mapper.WorkFlowMapper;
import com.mcwl.resource.service.WorkFlowLikeService;
import com.mcwl.system.service.ISysUserService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * 工作流点赞
 * @Author：ChenYan
 * @Project：McWl
 * @Package：com.mcwl.resource.service.impl
 * @Filename：ModelLikeServiceImpl
 * @Description TODO
 * @Date：2025/1/12 12:05
 */
@Service
public class WorkFlowLikeServiceImpl extends ServiceImpl<WorkFlowLikeMapper, WorkFlowLike> implements WorkFlowLikeService {


    @Autowired
    private WorkFlowMapper workFlowMapper;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void like(Long modelId) {
        Map<String, Long> map = new HashMap<>();
        map.put("userId", SecurityUtils.getUserId());
        map.put("commentId", modelId);
        rabbitTemplate.convertAndSend(QueueConstants.WORK_FLOW_LIKE_QUEUE, map);
    }

    @Override
    public TableDataInfo listByPage(PageDomain pageDomain) {

        if (StringUtils.isEmpty(pageDomain.getOrderByColumn())) {
            pageDomain.setOrderByColumn("create_time");
        }

        // 2. 构建分页对象并设置排序规则
        Page<WorkFlowLikeVo> page = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());

        List<WorkFlowLikeVo> workFlowLikeVoList = workFlowMapper.selectLikedWorkFlowsByUser(
                page,
                SecurityUtils.getUserIdMax(),
                pageDomain.getOrderByColumn());

        // 4. 封装分页结果
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(workFlowLikeVoList);
        rspData.setTotal(page.getTotal());
        return rspData;
    }

    @Override
    public WorkFlowLike selectWorkFlowLikeById(Long userId, Long id) {

        LambdaQueryWrapper<WorkFlowLike> workFlowLikeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        workFlowLikeLambdaQueryWrapper.eq(WorkFlowLike::getWorkFlowId,id);
        workFlowLikeLambdaQueryWrapper.eq(WorkFlowLike::getUserId,userId);

        return baseMapper.selectOne(workFlowLikeLambdaQueryWrapper);
    }





    private Page<WorkFlowLike> initPage(PageDomain pageDomain) {
        Page<WorkFlowLike> page = new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
        if (StringUtils.isBlank(pageDomain.getOrderByColumn())) {
            pageDomain.setOrderByColumn("create_time");
        }
        OrderItem orderItem = OrderItem.desc(pageDomain.getOrderByColumn());
        page.addOrder(orderItem);

        return page;
    }

}
