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

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.BaseEntity;
import com.ruoyi.common.page.PageResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.controller.req.ActivityPageReq;
import com.ruoyi.controller.resp.ActivityDetailResp;
import com.ruoyi.controller.resp.ActivityPageResp;
import com.ruoyi.frequency.activity.model.result.ActivityCount;
import com.ruoyi.frequency.news.entity.News;
import com.ruoyi.post.frequency.post.mapper.PostMapper;
import com.ruoyi.response.ResponseData;
import com.ruoyi.utils.SnowIdUtils;
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.activity.mapper.ActivityMapper;
import com.ruoyi.frequency.activity.entity.Activity;
import com.ruoyi.frequency.activity.service.ActivityService;

/**
 * 活动Service业务层处理
 *
 * @author liwenlong
 * @date 2024-03-19
 */
@Service
public class ActivityServiceImpl extends ServiceImpl<ActivityMapper, Activity>  implements ActivityService
{
    @Autowired
    private ActivityMapper activityMapper;

    /**
     * 查询活动
     *
     * @param id 活动主键
     * @return 活动
     */
    @Override
    public Activity selectActivityById(Long id)
    {
        return activityMapper.selectActivityById(id);
    }

    /**
     * 查询活动列表
     *
     * @param activity 活动
     * @return 活动
     */
    @Override
    public List<Activity> selectActivityList(Activity activity)
    {
        return activityList(activity);
    }

    private List<Activity> activityList(Activity activity){
        return activityMapper.selectActivityList(activity);
    }

    @Override
    public ActivityCount statistics(Activity activity) {
        List<Activity> activities = activityList(activity);
        List<Long> activityIdList = activities.stream().map(x -> x.getId()).collect(Collectors.toList());
        activityIdList.add(-1L);

        ActivityCount count =activityMapper.statistics(activityIdList);

        return count;
    }


    /**
     * 新增活动
     *
     * @param activity 活动
     * @return 结果
     */
    @Override
    public int insertActivity(Activity activity)
    {
        activity.setCreateData();
        return activityMapper.insert(activity);
    }

    /**
     * 修改活动
     *
     * @param activity 活动
     * @return 结果
     */
    @Override
    public int updateActivity(Activity activity)
    {
        activity.setUpdateData();
        return activityMapper.updateActivity(activity);
    }

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

    @Override
    public ResponseData<PageResult<Page<Activity>>> activityPage(ActivityPageReq req, UserVo userVo) {

        Page<Activity> page = this.baseMapper.activityPage(new Page<>(req.getPageNo(), req.getPageSize()), req,userVo);

        return ResponseData.success(new PageResult<>(page));
    }

    @Autowired
    private PostMapper postMapper;

    @Override
    public ResponseData<Activity> activityDetail(Long id, UserVo userVo) {
        Activity activity = this.baseMapper.activityDetail(id,userVo);

        if (activity != null){
            //增加浏览数
            this.baseMapper.updateActivity(new Activity().setId(id).setBrowseNum(1));

            //type: 类型 1作品 2 帖子 3投票 4新闻 5活动
            postMapper.updateBrowseRecord(SnowIdUtils.uniqueLong(),5,new Date(),id,userVo);
        }

        return ResponseData.success(activity);
    }


}
