package com.yiqi.activity.service.impl;

import java.math.BigDecimal;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.activity.domain.ActivityApplyOrg;
import com.yiqi.activity.domain.ActivityRechargeItem;
import com.yiqi.activity.domain.dto.ActivityInfoDTO;
import com.yiqi.activity.domain.dto.ActivityRechargeDTO;
import com.yiqi.activity.domain.dto.weapp.WeAppActivityCouponQueryDTO;
import com.yiqi.activity.domain.dto.weapp.WeAppActivityDiscountQueryDTO;
import com.yiqi.activity.domain.dto.weapp.WeAppActivityRechargeQueryDTO;
import com.yiqi.activity.domain.vo.weapp.WeAppActivityCouponQueryVO;
import com.yiqi.activity.domain.vo.weapp.WeAppActivityDiscountQueryVO;
import com.yiqi.activity.domain.vo.weapp.WeAppActivityRechargeQueryVO;
import com.yiqi.activity.service.IActivityApplyOrgService;
import com.yiqi.activity.service.IActivityRechargeItemService;
import com.yiqi.common.core.domain.AuthHolder;
import com.yiqi.common.enums.ActivityType;
import com.yiqi.common.enums.PlatformType;
import com.yiqi.common.enums.SourceType;
import com.yiqi.common.enums.StatusType;
import com.yiqi.common.utils.DateUtils;
import com.yiqi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yiqi.activity.mapper.ActivityInfoMapper;
import com.yiqi.activity.domain.ActivityInfo;
import com.yiqi.activity.service.IActivityInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

/**
 * 活动明细Service业务层处理
 *
 * @author xiaohei
 * @date 2023-09-08
 */
@Service
public class ActivityInfoServiceImpl extends ServiceImpl<ActivityInfoMapper,ActivityInfo> implements IActivityInfoService {

    @Autowired
    private IActivityRechargeItemService activityRechargeItemService;

    @Autowired
    private IActivityApplyOrgService activityApplyOrgService;

    /**
     * 查询活动明细
     *
     * @param id 活动明细主键
     * @return 活动明细
     */
    @Override
    public ActivityInfo selectActivityInfoById(Long id) {
        return baseMapper.selectActivityInfoById(id);
    }

    /**
     * 查询活动明细列表
     *
     * @param activityInfo 活动明细
     * @return 活动明细
     */
    @Override
    public List<ActivityInfo> selectActivityInfoList(ActivityInfo activityInfo) {
        List<ActivityInfo>  activityInfoList = baseMapper.selectActivityInfoList(activityInfo);
        for(ActivityInfo activityInfo1 : activityInfoList){
            activityInfo1.setStoreNum(activityApplyOrgService.getActivityApplyOrgNumByActivityId(activityInfo1.getId()));
        }
        return activityInfoList;
    }

    /**
     * 新增活动明细
     *
     * @param activityInfo 活动明细
     * @return 结果
     */
    @Override
    public int insertActivityInfo(ActivityInfo activityInfo) {
        activityInfo.setCreateTime(DateUtils.getNowDate());
        return baseMapper.insert(activityInfo);
    }

    /**
     * 修改活动明细
     *
     * @param activityInfo 活动明细
     * @return 结果
     */
    @Override
    public int updateActivityInfo(ActivityInfo activityInfo) {
        activityInfo.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateById(activityInfo);
    }

    /**
     * 批量删除活动明细
     *
     * @param ids 需要删除的活动明细主键
     * @return 结果
     */
    @Override
    public int deleteActivityInfoByIds(Long[] ids,String activityType) {
        return baseMapper.deleteActivityInfoByIds(ids,activityType);
    }

    /**
     * 删除活动明细信息
     *
     * @param id 活动明细主键
     * @return 结果
     */
    @Override
    public int deleteActivityInfoById(Long id,String activityType) {
        return baseMapper.deleteActivityInfoById(id,activityType);
    }

    /**
     * 修改活动明细状态
     *
     * @param activityInfo 活动明细
     * @return 结果
     */
    @Override
    public int updateActivityInfoStatus(ActivityInfo activityInfo) {
        activityInfo.setUpdateById(SecurityUtils.getUserId());
        activityInfo.setUpdateBy(SecurityUtils.getUsername());
        activityInfo.setUpdateTime(DateUtils.getNowDate());
        return baseMapper.updateActivityInfo(activityInfo);
    }

    @Override
    public List<WeAppActivityCouponQueryVO> selectActivityCouponList(WeAppActivityCouponQueryDTO weAppActivityCouponQueryDTO) {
        return baseMapper.selectActivityCouponList(weAppActivityCouponQueryDTO);
    }

    @Override
    public List<WeAppActivityDiscountQueryVO> selectActivityDiscountList(WeAppActivityDiscountQueryDTO weAppActivityDiscountQueryDTO) {
        return baseMapper.selectActivityDiscountList(weAppActivityDiscountQueryDTO);
    }

    @Override
    public List<WeAppActivityRechargeQueryVO> selectActivityRechargeList(WeAppActivityRechargeQueryDTO weAppActivityRechargeQueryDTO) {
        return baseMapper.selectActivityRechargeList(weAppActivityRechargeQueryDTO);
    }

}
