package com.cyl.manager.act.service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.time.LocalDateTime;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyl.manager.act.domain.entity.ActivityApplyInfo;
import com.cyl.manager.act.domain.entity.ActivityInfo;
import com.cyl.manager.act.domain.query.ActivityInfoQuery;
import com.cyl.manager.act.mapper.ActivityApplyInfoMapper;
import com.cyl.manager.act.mapper.ActivityInfoMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * 活动信息Service业务层处理
 *
 * @author zcc
 */
@Service
public class ActivityInfoService {
    @Autowired
    private ActivityInfoMapper activityInfoMapper;

    @Autowired
    private ActivityApplyInfoMapper activityApplyInfoMapper;


    /**
     * 查询活动信息
     *
     * @param id 活动信息主键
     * @return 活动信息
     */
    public ActivityInfo selectById(Long id) {
        ActivityInfo activityInfo = activityInfoMapper.selectById(id);
        //获取报名人数
        List<ActivityApplyInfo> activityApplyInfos = activityApplyInfoMapper.selectList(Wrappers.<ActivityApplyInfo>lambdaQuery()
                .eq(ActivityApplyInfo::getActivityId, activityInfo.getId())
                .ne(ActivityApplyInfo::getDelFlag, "2")
                .notIn(ActivityApplyInfo::getStatus, 2, 3));
        if (CollectionUtils.isNotEmpty(activityApplyInfos)) {
            activityInfo.setNum(activityApplyInfos.size());
        }
        return activityInfo;
    }

    /**
     * 查询活动信息列表
     *
     * @param query 查询条件
     * @param page  分页条件
     * @return 活动信息
     */
    public List<ActivityInfo> selectList(ActivityInfoQuery query, Pageable page) {
        if (page != null) {
            PageHelper.startPage(page.getPageNumber() + 1, page.getPageSize());
        }
        QueryWrapper<ActivityInfo> qw = new QueryWrapper<>();
        qw.eq("del_flag", 0);
        String activityTitle = query.getActivityTitle();
        if (!StringUtils.isEmpty(activityTitle)) {
            qw.eq("activity_title", activityTitle);
        }
        String activityDate = query.getActivityDate();
        if (!StringUtils.isEmpty(activityDate)) {
            qw.eq("activity_date", activityDate);
        }
        String applyStartTime = query.getApplyStartTime();
        if (!StringUtils.isEmpty(applyStartTime)) {
            qw.eq("apply_start_time", applyStartTime);
        }
        String applyEndTime = query.getApplyEndTime();
        if (!StringUtils.isEmpty(applyEndTime)) {
            qw.eq("apply_end_time", applyEndTime);
        }
        if (!StringUtils.isEmpty(query.getStatus())) {
            qw.eq("status", query.getStatus());
        }
        String activityAddress = query.getActivityAddress();
        if (!StringUtils.isEmpty(activityAddress)) {
            qw.eq("activity_address", activityAddress);
        }
        Long personCondition = query.getPersonCondition();
        if (personCondition != null) {
            qw.eq("person_condition", personCondition);
        }
        Integer orderNum = query.getOrderNum();
        if (orderNum != null) {
            qw.eq("order_num", orderNum);
        }
        String coverUrl = query.getCoverUrl();
        if (!StringUtils.isEmpty(coverUrl)) {
            qw.eq("cover_url", coverUrl);
        }
        String activityIntroduce = query.getActivityIntroduce();
        if (!StringUtils.isEmpty(activityIntroduce)) {
            qw.eq("activity_introduce", activityIntroduce);
        }
        List<ActivityInfo> list = activityInfoMapper.selectList(qw);
        if (CollectionUtils.isNotEmpty(list)) {
            list.forEach(activityInfo -> {
                //获取报名人数
                List<ActivityApplyInfo> activityApplyInfos = activityApplyInfoMapper.selectList(Wrappers.<ActivityApplyInfo>lambdaQuery()
                        .eq(ActivityApplyInfo::getActivityId, activityInfo.getId())
                        .notIn(ActivityApplyInfo::getStatus, 2, 3));
                if (CollectionUtils.isNotEmpty(activityApplyInfos)) {
                    activityInfo.setNum(activityApplyInfos.size());
                }
            });
        }
        return list;
    }

    /**
     * 新增活动信息
     *
     * @param activityInfo 活动信息
     * @return 结果
     */
    public int insert(ActivityInfo activityInfo) {
        activityInfo.setCreateTime(LocalDateTime.now());
        return activityInfoMapper.insert(activityInfo);
    }

    /**
     * 修改活动信息
     *
     * @param activityInfo 活动信息
     * @return 结果
     */
    public int update(ActivityInfo activityInfo) {
        return activityInfoMapper.updateById(activityInfo);
    }

    /**
     * 批量删除活动信息
     *
     * @param ids 需要删除的活动信息主键
     * @return 结果
     */
    public int deleteByIds(Long[] ids) {
        return activityInfoMapper.updateDelFlagByIds(ids);
    }

    /**
     * 删除活动信息信息
     *
     * @param id 活动信息主键
     * @return 结果
     */
    public int deleteById(Long id) {
        Long[] ids = {id};
        return activityInfoMapper.updateDelFlagByIds(ids);
    }
}
