/**
 * Copyright (C) 2018-2022
 * All rights reserved, Designed By www.yixiang.co
 * 注意：
 * 本软件为www.yixiang.co开发研制，未经购买不得使用
 * 购买后可获得全部源代码（禁止转卖、分享、上传到码云、github等开源平台）
 * 一经发现盗用、分享等行为，将追究法律责任，后果自负
 */
package co.yixiang.yshop.module.product.controller.app.relation;


import cn.hutool.core.util.NumberUtil;
import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.framework.common.util.collection.CollectionUtils;
import co.yixiang.yshop.framework.security.core.annotations.PreAuthenticated;
import co.yixiang.yshop.module.product.controller.app.relation.param.AppStoreProductRelationQueryParam;
import co.yixiang.yshop.module.product.controller.app.relation.param.BatchDelAppStoreProductRelationQueryParam;
import co.yixiang.yshop.module.product.controller.app.relation.vo.AppStoreProductRelationQueryVo;
import co.yixiang.yshop.module.product.service.storeproductrelation.AppStoreProductRelationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static co.yixiang.yshop.framework.common.exception.util.ServiceExceptionUtil.exception;
import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;
import static co.yixiang.yshop.framework.web.core.util.WebFrameworkUtils.getLoginUserId;
import static co.yixiang.yshop.module.product.enums.ErrorCodeConstants.PARAM_ERROR;

/**
 * <p>
 * 收藏/足迹控制器
 * </p>
 *
 * @author hupeng
 * @since 2023-6-14
 */
@Slf4j
@RestController
@Tag(name = "用户 APP - 收藏(足迹)")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@RequestMapping("/relation")
public class AppStoreProductRelationController {

    private final AppStoreProductRelationService appStoreProductRelationService;


    /**
     * 添加收藏
     */
    @PostMapping("/collect/add")
    @Operation(summary = "添加收藏")
    @PreAuthenticated
    public CommonResult<Boolean> collectAdd(@Validated @RequestBody AppStoreProductRelationQueryParam param) {
        if (!NumberUtil.isNumber(param.getProductId())) {
            throw exception(PARAM_ERROR);
        }
        appStoreProductRelationService.addProductRelation(Long.parseLong(param.getProductId()), getLoginUserId(), param.getCategory());
        return success(true);
    }

    /**
     * 取消收藏/删除足迹
     */
    @PreAuthenticated
    @PostMapping("/collect/del")
    @Operation(summary = "取消收藏")
    public CommonResult<Boolean> collectDel(@Validated @RequestBody AppStoreProductRelationQueryParam param) {
        if (!NumberUtil.isNumber(param.getProductId())) {
            throw exception(PARAM_ERROR);
        }
        appStoreProductRelationService.delProductRelation(Long.parseLong(param.getProductId()),
                getLoginUserId(), param.getCategory(), param.getType());
        return success(true);
    }

    /**
     * 批量取消收藏
     */
    @PreAuthenticated
    @PostMapping("/collect/batchDel")
    @Operation(summary = "批量取消收藏/足迹")
    public CommonResult<Boolean> batchDelCollect(@Validated @RequestBody BatchDelAppStoreProductRelationQueryParam param) {
        if (CollectionUtils.isAnyEmpty(param.getProductIdList())) {
            throw exception(PARAM_ERROR);
        }
        appStoreProductRelationService.batchDelProductRelation(param.getProductIdList(), getLoginUserId(), param.getCategory(), param.getType());
        return success(true);
    }

    /**
     * 获取收藏产品，足迹
     */
    @PreAuthenticated
    @GetMapping("/collect/user")
    @Operation(summary = "获取收藏产品,或足迹")
    @Parameter(name = "type", description = "类型 collect 收藏 foot足迹", required = true)
    public CommonResult<List<AppStoreProductRelationQueryVo>> collectRootUser(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                                              @RequestParam(value = "limit", defaultValue = "10") Integer limit,
                                                                              @RequestParam(value = "type") String type) {
        return success(appStoreProductRelationService.userCollectRootProductList(page, limit, getLoginUserId(), type));
    }


}

