
package com.tmerclub.cloud.multishop.controller.app;

import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.tmerclub.cloud.api.auth.bo.UidInfoBO;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.multishop.service.ShopCollectionService;
import com.tmerclub.cloud.multishop.vo.ShopCollectionVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 用户店铺收藏记录
 *
 * @author lhd
 * @date 2021-02-23 14:41:42
 */
@RestController("appUserCollectionShopController")
@RequestMapping("/shopCollection")
@Tag(name = "app-用户店铺收藏记录")
public class ShopCollectionController {

    @Autowired
    private ShopCollectionService shopCollectionService;

    @GetMapping("/page")
    @Operation(summary = "分页返回收藏数据", description = "根据用户id获取")
    public ServerResponseEntity<PageVO<ShopCollectionVO>> getUserCollectionDtoPageByUserId(@Valid PageDTO pageDTO, String shopName) {
        Long userId = AuthUserContext.get().getUserId();
        PageVO<ShopCollectionVO> shopCollectionPage = shopCollectionService.getUserCollectionShopPageByUserIdAndShopName(pageDTO, userId, shopName);
        return ServerResponseEntity.success(shopCollectionPage);
    }

    @GetMapping("/ma/isCollection")
    @Operation(summary = "根据店铺id获取该店铺是否在收藏夹中", description = "传入收藏店铺id")
    public ServerResponseEntity<Boolean> isCollection(Long shopId) {
        UidInfoBO uidInfoBO = AuthUserContext.get();
        // 没有登陆就返回false (未收藏店铺)
        if (uidInfoBO == null) {
            return ServerResponseEntity.success(Boolean.FALSE);
        }
        return ServerResponseEntity.success(shopCollectionService.countByShopIdAndUserId(shopId, uidInfoBO.getUserId()) > 0);
    }

    @PostMapping("/createOrCancel")
    @Operation(summary = "添加/取消收藏", description = "传入收藏店铺id,如果店铺未收藏则收藏店铺，已收藏则取消收藏")
    @Parameter(name = "shopId", description = "店铺id", required = true)
    public ServerResponseEntity<Boolean> createOrCancel(@RequestBody Long shopId) {
        Long userId = AuthUserContext.get().getUserId();
        Boolean isAdd = shopCollectionService.addOrCancel(shopId, userId);
        return ServerResponseEntity.success(isAdd);
    }

    @PostMapping("/batchCancel")
    @Operation(summary = "批量取消收藏", description = "传入收藏店铺id")
    @Parameter(name = "shopIds", description = "店铺Id", required = true)
    public ServerResponseEntity<Boolean> batchCancel(@RequestBody List<Long> shopIds) {
        Long userId = AuthUserContext.get().getUserId();
        if (CollectionUtils.isEmpty(shopIds)) {
            return ServerResponseEntity.success(false);
        }
        return ServerResponseEntity.success(shopCollectionService.batchCancel(shopIds, userId));
    }

    /**
     * 查询用户收藏店铺数量
     */
    @GetMapping("count")
    @Operation(summary = "查询用户收藏店铺数量", description = "查询用户收藏店铺数量")
    public int findUserCollectionCount() {
        Long userId = AuthUserContext.get().getUserId();
        return shopCollectionService.countByShopIdAndUserId(null, userId);
    }
}
