package cn.iocoder.yudao.module.member.service.activityawardsuser;

import cn.hutool.core.bean.BeanUtil;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.activityawardsuser.vo.ActivityAwardsUserCountVO;
import cn.iocoder.yudao.module.member.controller.admin.activityawardsuser.vo.ActivityAwardsUserPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.activityawardsuser.vo.ActivityAwardsUserRespVO;
import cn.iocoder.yudao.module.member.controller.admin.activityawardsuser.vo.ActivityAwardsUserSaveReqVO;
import cn.iocoder.yudao.module.member.controller.app.activity.vo.AppCancelVO;
import cn.iocoder.yudao.module.member.controller.app.activity.vo.AppUserAwardsVO;
import cn.iocoder.yudao.module.member.dal.dataobject.activityawardsuser.ActivityAwardsUserDO;
import cn.iocoder.yudao.module.member.dal.mysql.activityawardsuser.ActivityAwardsUserMapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.ACTIVITY_AWARDS_USER_NOT_EFFECTIVE;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.ACTIVITY_AWARDS_USER_NOT_EXISTS;

/**
 * 活动中奖人员 Service 实现类
 *
 * @author 我是秦俊旗
 */
@Service
@Validated
public class ActivityAwardsUserServiceImpl implements ActivityAwardsUserService {

    @Resource
    private ActivityAwardsUserMapper activityAwardsUserMapper;

    @Override
    public Long createActivityAwardsUser(ActivityAwardsUserSaveReqVO createReqVO) {
        // 插入
        ActivityAwardsUserDO activityAwardsUser = BeanUtils.toBean(createReqVO, ActivityAwardsUserDO.class);
        activityAwardsUserMapper.insert(activityAwardsUser);
        // 返回
        return activityAwardsUser.getId();
    }

    @Override
    public void updateActivityAwardsUser(ActivityAwardsUserSaveReqVO updateReqVO) {
        // 校验存在
        validateActivityAwardsUserExists(updateReqVO.getId());
        // 更新
        ActivityAwardsUserDO updateObj = BeanUtils.toBean(updateReqVO, ActivityAwardsUserDO.class);
        activityAwardsUserMapper.updateById(updateObj);
    }

    @Override
    public void deleteActivityAwardsUser(Long id) {
        // 校验存在
        validateActivityAwardsUserExists(id);
        // 删除
        activityAwardsUserMapper.deleteById(id);
    }

    private void validateActivityAwardsUserExists(Long id) {
        if (activityAwardsUserMapper.selectById(id) == null) {
            throw exception(ACTIVITY_AWARDS_USER_NOT_EXISTS);
        }
    }

    @Override
    public ActivityAwardsUserDO getActivityAwardsUser(Long id) {
        return activityAwardsUserMapper.selectById(id);
    }

    @Override
    public PageResult<ActivityAwardsUserDO> getActivityAwardsUserPage(ActivityAwardsUserPageReqVO pageReqVO) {
        return activityAwardsUserMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<ActivityAwardsUserRespVO> getActivityAwardsUserJoinPage(ActivityAwardsUserPageReqVO pageReqVO) {
        return activityAwardsUserMapper.selectJoinPage(pageReqVO);
    }

    @Override
    public Map<Long, ActivityAwardsUserCountVO> countByActivityIds(List<Long> activityIds) {
        List<ActivityAwardsUserCountVO> activityAwardsUserCountVOS = activityAwardsUserMapper.countByActivityIds(activityIds);
        if (activityAwardsUserCountVOS.isEmpty()) {
            return Collections.emptyMap();
        }
        return activityAwardsUserCountVOS.stream().collect(Collectors.toMap(ActivityAwardsUserCountVO::getActivityId, v -> v));
    }

    @Override
    public List<ActivityAwardsUserDO> getAwardsUserListByActivityId(Long activityId) {

//        LocalDateTime date = LocalDateTime.now();
//
//        LocalDateTime startOfDay = date.with(LocalTime.MIN);
//
//        LocalDateTime endOfDay = date.with(LocalTime.MAX);

        return activityAwardsUserMapper.selectList(Wrappers.<ActivityAwardsUserDO>lambdaQuery()
                .eq(ActivityAwardsUserDO::getActivityId, activityId)
//                .between(ActivityAwardsUserDO::getCreateTime, startOfDay, endOfDay)
        );
    }

    @Override
    public void insertBatch(List<ActivityAwardsUserDO> list) {
        activityAwardsUserMapper.insertBatch(list);
    }

    @Override
    public List<AppUserAwardsVO> getListByActivityIdAndUserId(Long activityId, Long userId) {


        List<ActivityAwardsUserDO> activityAwardsUserDOS = activityAwardsUserMapper.selectList(Wrappers.<ActivityAwardsUserDO>lambdaQuery()
                .eq(ActivityAwardsUserDO::getActivityId, activityId)
                .eq(ActivityAwardsUserDO::getUserId, userId)
        );
        return BeanUtil.copyToList(activityAwardsUserDOS, AppUserAwardsVO.class);
    }


    @Override
    public Boolean cancel(AppCancelVO vo) {
        ActivityAwardsUserDO activityAwardsUserDO = activityAwardsUserMapper.selectById(vo.getActivityAwardsUserId());
        if(!"0".equals(activityAwardsUserDO.getAwardsStatus())){
            throw exception(ACTIVITY_AWARDS_USER_NOT_EFFECTIVE);
        }
        activityAwardsUserDO.setAwardsStatus("1");
        int i = activityAwardsUserMapper.updateById(activityAwardsUserDO);
        return i>0;
    }

    @Override
    public List<ActivityAwardsUserDO> getListByTime() {
        LocalDateTime date = LocalDateTime.now().minusDays(1);

        LocalDateTime endOfDay = date.with(LocalTime.MAX);

        return activityAwardsUserMapper.selectList(Wrappers.<ActivityAwardsUserDO>lambdaQuery()
                .le(ActivityAwardsUserDO::getCreateTime, endOfDay)
                .eq(ActivityAwardsUserDO::getAwardsStatus, "0")
        );
    }

    @Override
    public void updateBatch(List<ActivityAwardsUserDO> list) {
        activityAwardsUserMapper.updateBatch(list);
    }
}