package com.ruoyi.xq.controller.app;

import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.domain.PageModel;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.xq.domain.ActivityOrder;
import com.ruoyi.xq.dto.app.activity.*;
import com.ruoyi.xq.dto.app.common.IdReq;
import com.ruoyi.xq.dto.app.pay.OrderCreateVo;
import com.ruoyi.xq.service.ActivityOrderService;
import com.ruoyi.xq.service.ActivityShopService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Validated
@RestController
@Tag(name = "相亲会相关接口")
@RequestMapping("/api/activity/matchmaking")
public class ActivityMatchmakingController {

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

    @GetMapping("/page")
    @Operation(summary = "相亲会列表-分页")
    @Log(title = "相亲会列表-分页", businessType = BusinessType.OTHER, isPrintResponseData = false, isSaveDb = false)
    public R<PageModel<ActivityShopAppList>> page(PageQuery pageQuery){
        IPage<ActivityShopAppList> page = activityShopService.pageApp(pageQuery);
        return R.ok(PageModel.build(page));
    }

    @GetMapping("/info")
    @Operation(summary = "相亲详情")
    @Log(title = "相亲会详情", businessType = BusinessType.OTHER, isPrintResponseData = false, isSaveDb = false)
    @SaIgnore
    public R<ActivityShopAppInfo> info(Long activityId, String activityCode){
        if(activityId == null && StringUtils.isEmpty(activityCode)){
            return R.ok();
        }
        ActivityShopAppInfo info = activityShopService.info(activityId, activityCode);
        return R.ok(info);
    }

    @PostMapping("/checkJoin")
    @Operation(summary = "参加相亲会-检测")
    @Log(title = "参加相亲会-检测", businessType = BusinessType.OTHER)
    public R<JoinResp> join(@RequestBody IdReq idReq){
        Long userId = LoginHelper.getUserId();
        JoinResp join = activityShopService.checkJoin(userId, idReq.getId());
        return R.ok(join);
    }

    @PostMapping("/freeJoin")
    @Operation(summary = "参加相亲会（免费）")
    @Log(title = "参加相亲会（免费）", businessType = BusinessType.OTHER)
    public R<JoinResp> freeJoin(@RequestBody FreeJoinReq req){
        req.setUserId(LoginHelper.getUserId());
        JoinResp join = activityShopService.join(req);
        return R.ok(join);
    }

    @PostMapping("/create/order")
    @Operation(summary = "创建相亲会订单")
    @Log(title = "创建相亲会订单", businessType = BusinessType.OTHER)
    public R<OrderCreateVo> createOrder(@RequestBody CreateOrderReq req){
        Long userId = LoginHelper.getUserId();
        req.setUserId(userId);
        ActivityOrder order = activityOrderService.createOrder(req);
        OrderCreateVo result = new OrderCreateVo();
        result.setPrice(order.getPrice());
        result.setOrderNo(order.getOrderNo());
        result.setOrderName(order.getOrderName());
        return R.ok(result);
    }

}
