package com.yiqi.api.controller.activity;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yiqi.common.exception.ServiceException;
import com.yiqi.activity.domain.ActivityCouponInfo;
import com.yiqi.activity.domain.ActivityInfo;
import com.yiqi.activity.domain.dto.weapp.WeAppActivityCouponQueryDTO;
import com.yiqi.activity.domain.vo.ActivityCouponItemVO;
import com.yiqi.activity.domain.vo.weapp.WeAppActivityCouponInfoVO;
import com.yiqi.activity.domain.vo.weapp.WeAppActivityCouponQueryVO;
import com.yiqi.activity.service.IActivityCouponInfoService;
import com.yiqi.activity.service.IActivityCouponItemService;
import com.yiqi.activity.service.IActivityInfoService;
import com.yiqi.app.domain.AppUser;
import com.yiqi.app.service.IAppUserService;
import com.yiqi.common.constant.UrlConstants;
import com.yiqi.common.core.controller.BaseController;
import com.yiqi.common.core.domain.AuthHolder;
import com.yiqi.common.core.domain.R;
import com.yiqi.common.enums.OrderActivityStatusType;
import com.yiqi.common.enums.StatusType;
import com.yiqi.common.enums.SysBoolType;
import com.yiqi.order.domain.OrderActivity;
import com.yiqi.order.service.IOrderActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

@Api(tags = "app送券活动")
@RestController
@RequestMapping(value = UrlConstants.wxappApi + "/activity/coupon")
public class WeAppActivityCouponController extends BaseController {

    @Autowired
    private IActivityInfoService activityInfoService;
    @Autowired
    private IActivityCouponInfoService activityCouponInfoService;
    @Autowired
    private IActivityCouponItemService activityCouponItemService;
    @Autowired
    private IOrderActivityService orderActivityService;
    @Autowired
    private IAppUserService appUserService;



    /**
     * 查询注册送券活动列表
     */
    @PostMapping("/findPage")
    @ApiOperation(value = "查询注册送券活动列表")
    public R<List<WeAppActivityCouponQueryVO>> findPage(@RequestBody WeAppActivityCouponQueryDTO weAppActivityCouponQueryDTO) {
        List<WeAppActivityCouponQueryVO> list = activityInfoService.selectActivityCouponList(weAppActivityCouponQueryDTO);
        if (CollUtil.isEmpty(list)){
            return R.ok(list);
        }
        if (SysBoolType.YES.getCode().equals(weAppActivityCouponQueryDTO.getIsNewUser())){
            AppUser appUser = appUserService.getOne(new QueryWrapper<AppUser>().lambda()
                    .eq(AppUser::getId, AuthHolder.userId())
                    .eq(AppUser::getStatus, StatusType.OK.getCode()));
            if (appUser == null){
                throw new ServiceException("当前用户不存在");
            }
            list = list.stream().filter(vo -> vo.getNewUserRegistTime().compareTo(appUser.getCreateTime()) <= 0).collect(Collectors.toList());
        }
        return R.ok(list);
    }

    /**
     * 查询注册送券活动明细
     */
    @GetMapping("/getInfoActivityId")
    @ApiOperation(value = "查询注册送券活动明细")
    public R<WeAppActivityCouponInfoVO> getInfoActivityId(@RequestParam Long activityId) {
        WeAppActivityCouponInfoVO weAppActivityCouponInfoVO = new WeAppActivityCouponInfoVO();
        ActivityInfo activityInfo = activityInfoService.getOne(new QueryWrapper<ActivityInfo>()
                .lambda().eq(ActivityInfo::getId,activityId));
        BeanUtils.copyProperties(activityInfo,weAppActivityCouponInfoVO);
        ActivityCouponInfo activityCouponInfo = activityCouponInfoService
                .getOne(new QueryWrapper<ActivityCouponInfo>().lambda()
                .eq(ActivityCouponInfo::getActivityId,activityId));
        BeanUtils.copyProperties(activityCouponInfo,weAppActivityCouponInfoVO);
        List<ActivityCouponItemVO> list = activityCouponItemService.selectActivityCouponItemListByActivityId(activityId);
        weAppActivityCouponInfoVO.setActivityCouponItemVOS(list);
        weAppActivityCouponInfoVO.setHasBuyNum(orderActivityService.count(new QueryWrapper<OrderActivity>()
                .lambda().eq(OrderActivity::getActivityId,activityId)
                .eq(OrderActivity::getAppUserId, AuthHolder.userId())
                .ne(OrderActivity::getOrderStatus, OrderActivityStatusType.REFUND.getCode())
                .ne(OrderActivity::getOrderStatus,OrderActivityStatusType.CANCEL_ORDER.getCode())));
        return R.ok(weAppActivityCouponInfoVO);
    }
}









