package com.ruoyi.xq.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.xq.domain.ActivityOrder;
import com.ruoyi.xq.domain.ActivityShop;
import com.ruoyi.xq.domain.ActivityUser;
import com.ruoyi.xq.dto.common.user.MinUser;
import com.ruoyi.xq.mapper.ActivityUserMapper;
import com.ruoyi.xq.service.ActivityOrderService;
import com.ruoyi.xq.service.ActivityShopService;
import com.ruoyi.xq.service.ActivityUserService;
import com.ruoyi.xq.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 相亲会参与人Service业务层处理
 *
 * @author 77
 * @date 2024-05-09
 */
@Service
public class ActivityUserServiceImpl extends ServiceImpl<ActivityUserMapper, ActivityUser> implements ActivityUserService {

    @Autowired
    private ActivityOrderService activityOrderService;
    @Autowired
    private UserService userService;
    @Autowired
    private ActivityShopService activityShopService;

    @Override
    public void joinActivity(String orderNo) {
        ActivityOrder activityOrder = activityOrderService.getByOrderNo(orderNo);
        ActivityUser activityUser = new ActivityUser();
        activityUser.setActivityId(activityOrder.getActivityId());
        activityUser.setUserId(activityOrder.getUserId());
        activityUser.setUsercode(activityOrder.getUsercode());
        activityUser.setGender(activityOrder.getGender());
        activityUser.setMobile(activityOrder.getActivityMobile());
        activityUser.setPay(1);
        activityUser.setPrice(activityOrder.getPrice());
        activityUser.setTraceId(activityOrder.getTraceId());
        this.save(activityUser);
        activityShopService.resetActivityShopUser(activityOrder.getActivityId(), activityOrder.getGender());
    }

    @Override
    public void joinActivity(Long activityId,String mobile,Long userId) {
        ActivityShop activityShop = activityShopService.getById(activityId);
        MinUser minUser = userService.getMinUserById(userId);
        ActivityUser activityUser = new ActivityUser();
        activityUser.setActivityId(activityShop.getId());
        activityUser.setUserId(minUser.getId());
        activityUser.setUsercode(minUser.getUsercode());
        activityUser.setGender(minUser.getGender());
        activityUser.setMobile(mobile);
        activityUser.setPay(0);
        this.save(activityUser);
        activityShopService.resetActivityShopUser(activityId, minUser.getGender());
    }

    @Override
    public Long countByActivityIdAndGender(Long activityId, Integer gender) {
        return this.count(Wrappers.lambdaQuery(ActivityUser.class).eq(ActivityUser::getActivityId, activityId).eq(ActivityUser::getGender, gender));
    }

    @Override
    public boolean checkJoin(Long userId, Long activityId) {
        return this.exists(Wrappers.lambdaQuery(ActivityUser.class).eq(ActivityUser::getActivityId, activityId).eq(ActivityUser::getUserId, userId));
    }
}
