package com.qs.mp.web.controller.api.user;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.qs.mp.common.core.domain.AjaxResult;
import com.qs.mp.common.core.page.TableDataInfo;
import com.qs.mp.framework.security.handle.HostHolder;
import com.qs.mp.user.domain.MarketingHitPrize;
import com.qs.mp.user.domain.MarketingUserCode;
import com.qs.mp.user.domain.param.UserMarketingHitPrizeQueryParam;
import com.qs.mp.user.domain.vo.UserMarketingHitPrizeDetailVO;
import com.qs.mp.user.domain.vo.UserMarketingHitPrizeListVO;
import com.qs.mp.user.service.IMarketingHitPrizeService;
import com.qs.mp.user.service.IMarketingUserCodeService;
import com.qs.mp.utils.SecurityUtils;
import com.qs.mp.web.controller.common.BaseApiController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 营销活动中奖信息相关接口
 * @author Cup
 * @date 2022/5/19
 */
@RestController
@RequestMapping("/api/v1/mp/user/marketing/hit/prize")
@Api(tags = "营销活动中奖信息相关接口")
public class MarketingHitPrizeController extends BaseApiController {

    @Autowired
    private IMarketingHitPrizeService marketingHitPrizeService;

    @Autowired
    private IMarketingUserCodeService marketingUserCodeService;

    @Autowired
    private HostHolder hostHolder;

    @PostMapping("/list")
    @ApiOperation("获取中奖信息列表")
    @ApiResponses(
            @ApiResponse(code = 200, message = "成功", response = UserMarketingHitPrizeListVO.class)
    )
    public TableDataInfo list(@Validated @RequestBody UserMarketingHitPrizeQueryParam userMarketingHitPrizeQueryParam) {
        startPage();
        QueryWrapper<MarketingHitPrize> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("t1.marketing_id",userMarketingHitPrizeQueryParam.getMarketingId());
        queryWrapper.orderByAsc("t2.sort");
        List<UserMarketingHitPrizeListVO> marketingHitPrizeListVOList = marketingHitPrizeService.listHitPrizeByQueryWrapper(queryWrapper);
        if (CollectionUtils.isNotEmpty(marketingHitPrizeListVOList)) {
            for (UserMarketingHitPrizeListVO userMarketingHitPrizeListVO : marketingHitPrizeListVOList) {
                String nickName = userMarketingHitPrizeListVO.getNickName();
                if (StringUtils.isNotBlank(nickName)) {
                    userMarketingHitPrizeListVO.setNickName(nickName.charAt(0) + "**");
                }
            }
        }
        return getDataTable(marketingHitPrizeListVOList);
    }

    @PostMapping("/isHit/{marketingId}")
    @ApiOperation("判断当前用户是否中奖")
    @ApiResponses(
            @ApiResponse(code = 200, message = "成功", response = UserMarketingHitPrizeDetailVO.class)
    )
    public AjaxResult isHit(@PathVariable("marketingId") Long marketingId) {

        Long userId = SecurityUtils.getLoginUser().getUserId();

        UserMarketingHitPrizeDetailVO userMarketingHitPrizeDetailVO = new UserMarketingHitPrizeDetailVO();
        userMarketingHitPrizeDetailVO.setIsHit(0);

        // 判断是否参与了
        int count = marketingUserCodeService.count(new LambdaQueryWrapper<MarketingUserCode>()
                .eq(MarketingUserCode::getMarketingId, marketingId)
                .eq(MarketingUserCode::getUserId, userId));
        if (count == 0) {
            userMarketingHitPrizeDetailVO.setIsHit(-1);
            return AjaxResult.success(userMarketingHitPrizeDetailVO);
        }

        if (Objects.nonNull(userId) && userId != 0) {
            QueryWrapper<MarketingHitPrize> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("t1.marketing_id",marketingId);
            queryWrapper.eq("t1.user_id", userId);
            List<UserMarketingHitPrizeListVO> list = marketingHitPrizeService.listHitPrizeByQueryWrapper(queryWrapper);
            if (CollectionUtils.isNotEmpty(list)) {
                UserMarketingHitPrizeListVO userMarketingHitPrizeListVO = list.get(0);
                userMarketingHitPrizeDetailVO.setPrizeTitle(userMarketingHitPrizeListVO.getPrizeTitle());
                userMarketingHitPrizeDetailVO.setIsHit(1);
                userMarketingHitPrizeDetailVO.setAwardName(userMarketingHitPrizeListVO.getAwardName());
                userMarketingHitPrizeDetailVO.setPrizePicUrl(userMarketingHitPrizeListVO.getPrizePicUrl());
                userMarketingHitPrizeDetailVO.setPrizeType(userMarketingHitPrizeListVO.getPrizeType());
                userMarketingHitPrizeDetailVO.setValue(userMarketingHitPrizeListVO.getValue());
            }
        }
        return AjaxResult.success(userMarketingHitPrizeDetailVO);
    }

}
