package com.ruoyi.frequency.worksreport.service.impl;

import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollectionUtil;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.controller.req.WorksReportReq;
import com.ruoyi.frequency.customer.service.UserService;
import com.ruoyi.frequency.works.entity.Works;
import com.ruoyi.frequency.works.service.WorksService;
import com.ruoyi.post.frequency.posreport.entity.PostReport;
import com.ruoyi.post.frequency.post.entity.Post;
import com.ruoyi.response.ResponseData;
import com.ruoyi.vo.UserInfo;
import com.ruoyi.vo.UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.code.PublicCommon;
import com.ruoyi.frequency.worksreport.mapper.WorksReportMapper;
import com.ruoyi.frequency.worksreport.entity.WorksReport;
import com.ruoyi.frequency.worksreport.service.WorksReportService;

/**
 * 作品举报Service业务层处理
 *
 * @author liwenlong
 * @date 2023-11-30
 */
@Service
public class WorksReportServiceImpl extends ServiceImpl<WorksReportMapper, WorksReport>  implements WorksReportService
{
    @Autowired
    private WorksReportMapper worksReportMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private WorksService worksService;

    /**
     * 查询作品举报
     *
     * @param id 作品举报主键
     * @return 作品举报
     */
    @Override
    public WorksReport selectWorksReportById(Long id)
    {
        WorksReport report = worksReportMapper.selectWorksReportById(id);
        if (report != null){
            Set<UserVo> userVoSet = CollectionUtil.newHashSet(new UserVo(report.getUserType(), report.getUserId()));

            //查询用户信息
            Map<UserVo, UserInfo> userMap = userService.selectUserInfoMap(userVoSet,null);

            report.setUserInfo(userMap.get(new UserVo(report.getUserType(), report.getUserId())));

            //作品信息
            Works works = worksService.selectWorksById(report.getWorksId());
            report.setWorks(works);
        }
        return report;
    }

    /**
     * 查询作品举报列表
     *
     * @param worksReport 作品举报
     * @return 作品举报
     */
    @Override
    public List<WorksReport> selectWorksReportList(WorksReport worksReport)
    {
        List<WorksReport> reports = worksReportMapper.selectWorksReportList(worksReport);

        if (reports.size() > 0){
            Set<UserVo> userVoSet = reports.stream().map(s -> new UserVo(s.getUserType(), s.getUserId())).collect(Collectors.toSet());

            //查询用户信息
            Map<UserVo, UserInfo> userMap = userService.selectUserInfoMap(userVoSet,null);

            Set<Long> worksIds = reports.stream().map(WorksReport::getWorksId).collect(Collectors.toSet());

            //查询作品信息
            Map<Long, Works> worksMap =  worksService.selectWorksMap(worksIds);

            reports.stream().forEach(s ->{
                s.setUserInfo(userMap.get(new UserVo(s.getUserType(), s.getUserId())));
                s.setWorks(worksMap.get(s.getWorksId()));
            });
        }

        return reports;
    }

    /**
     * 新增作品举报
     *
     * @param worksReport 作品举报
     * @return 结果
     */
    @Override
    public int insertWorksReport(WorksReport worksReport)
    {
        return worksReportMapper.insert(worksReport);
    }

    /**
     * 修改作品举报
     *
     * @param worksReport 作品举报
     * @return 结果
     */
    @Override
    public int updateWorksReport(WorksReport worksReport)
    {
        return worksReportMapper.updateWorksReport(worksReport);
    }

    /**
     * 批量删除作品举报
     *
     * @param ids 需要删除的作品举报主键
     * @return 结果
     */
    @Override
    public int deleteWorksReportByIds(Long[] ids,Integer delFlag)
    {
        this.lambdaUpdate().set(WorksReport::getDelFlag,delFlag).in(WorksReport::getId,ids).update();
        return ids.length;
    }

    @Override
    public ResponseData worksReport(WorksReportReq req, UserVo userVo) {
        WorksReport worksReport = new WorksReport().setWorksId(req.getWorksId())
                .setUserType(userVo.getUserType())
                .setUserId(userVo.getUserId())
                .setReason(req.getReason())
                .setContent(req.getContent())
                .setImages(req.getImages());
        this.save(worksReport);

        return ResponseData.success();
    }
}
