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.ModelLike;
import com.mcwl.resource.domain.ModelProduct;
import com.mcwl.resource.domain.vo.ModelImageLikeVo;
import com.mcwl.resource.domain.vo.ModelLikeVo;
import com.mcwl.resource.mapper.ModelLikeMapper;
import com.mcwl.resource.mapper.ModelMapper;
import com.mcwl.resource.service.ModelLikeService;
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 java.util.*;

/**
 * 模型点赞
 */
@Service
public class ModelLikeServiceImpl extends ServiceImpl<ModelLikeMapper, ModelLike> implements ModelLikeService {


    @Autowired
    private ModelMapper modelMapper;

    @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.MODEL_LIKE_QUEUE, map);
    }

    @Override
    public TableDataInfo listByPage(PageDomain pageDomain) {
        if (StringUtils.isEmpty(pageDomain.getOrderByColumn())) {
            pageDomain.setOrderByColumn("create_time");
        }

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

        List<ModelLikeVo> modelLikeVoList = modelMapper.selectLikedModelsByUser(
                page,
                SecurityUtils.getUserIdMax(),
                pageDomain.getOrderByColumn());

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


    }


}
