package com.mcwl.communityCenter.service.impl;

import cn.hutool.core.bean.BeanUtil;
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.core.domain.R;
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.communityCenter.domain.*;
import com.mcwl.communityCenter.domain.dto.*;
import com.mcwl.communityCenter.domain.vo.CommentVo;
import com.mcwl.communityCenter.domain.vo.PersonHomeVo;
import com.mcwl.communityCenter.domain.vo.PublishVo;
import com.mcwl.communityCenter.mapper.*;
import com.mcwl.communityCenter.service.PublishCollectService;
import com.mcwl.communityCenter.service.PublishCommentService;
import com.mcwl.communityCenter.service.PublishService;
import com.mcwl.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@RequiredArgsConstructor
public class PublishCollectServiceImpl extends ServiceImpl<PublishCollectMapper, PublishCollect> implements PublishCollectService {

    private final PublishCommentService publishCommentService;

    private final PublishLikeMapper publishLikeMapper;


    @Override
    public TableDataInfo myCollectList(MyPublishCollectPageRes myPublishCollectPageRes) {
        Page<PublishCollect> page = initPage(myPublishCollectPageRes);
        page.addOrder(new OrderItem("p.create_time", false));
        List<PublishVo> publishVoList = baseMapper.myCollectList(page, myPublishCollectPageRes, SecurityUtils.getUserId());
        List<PersonHomeVo> personHomeVoList = new ArrayList<>();

        for (PublishVo publishVo : publishVoList) {
            PersonHomeVo personHomeVo = BeanUtil.toBean(publishVo, PersonHomeVo.class);

            PublishLikeRes publishLikeRes = new PublishLikeRes();
            publishLikeRes.setTenantId(myPublishCollectPageRes.getTenantId());
            publishLikeRes.setCommunityId(myPublishCollectPageRes.getCommunityId());
            publishLikeRes.setPublishId(publishVo.getId());
            PublishLike publishLike = publishLikeMapper.selectPublishLike(publishLikeRes, SecurityUtils.getUserId());
            if (Objects.nonNull(publishLike)) {
                String delFlag = publishLike.getDelFlag();
                if ("0".equals(delFlag)) {
                    publishVo.setIsLike(1);
                } else {
                    publishVo.setIsLike(0);
                }
            } else {
                publishVo.setIsLike(0);
            }


            CommentDetailRes commentDetailRes = new CommentDetailRes();
            commentDetailRes.setTenantId(myPublishCollectPageRes.getTenantId());
            commentDetailRes.setCommunityId(myPublishCollectPageRes.getCommunityId());
            commentDetailRes.setPublishId(publishVo.getId());

            List<CommentVo> commentVoList = publishCommentService.getComment(commentDetailRes).getData();
            personHomeVo.setCommentList(commentVoList);
            personHomeVoList.add(personHomeVo);
        }


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

    @Override
    public void savePublishCollect(PublishCollect publishCollect) {
        baseMapper.savePublishCollect(publishCollect);
    }

    @Override
    public PublishCollect getPublishCollect(PublishCollectRes publishCollectRes, Long userId) {
        return baseMapper.getPublishCollect(publishCollectRes, userId);
    }

    @Override
    public void updatePublishCollect(PublishCollect publishCollect) {
        baseMapper.updatePublishCollect(publishCollect);
    }

    private Page<PublishCollect> initPage(PageDomain pageDomain) {
        return new Page<>(pageDomain.getPageNum(), pageDomain.getPageSize());
    }


}
