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

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
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.exception.LuckException;
import com.tmerclub.cloud.common.product.constant.SpuType;
import com.tmerclub.cloud.common.product.vo.SpuVO;
import com.tmerclub.cloud.common.product.vo.app.SpuAppVO;
import com.tmerclub.cloud.common.response.ResponseEnum;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.product.model.SpuCollection;
import com.tmerclub.cloud.product.service.SpuCollectionService;
import com.tmerclub.cloud.product.service.SpuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;

/**
 * 商品收藏信息
 *
 * @author tmerclub
 * @date 2020-11-21 14:43:16
 */
@RestController("appSpuCollectionController")
@RequestMapping("/spu_collection")
@Tag(name = "app-商品收藏信息")
public class SpuCollectionController {

    @Autowired
    private SpuCollectionService spuCollectionService;

    @Autowired
    private SpuService spuService;

    @GetMapping("/page")
    @Operation(summary = "分页返回收藏数据", description = "根据用户id获取")
    public ServerResponseEntity<PageVO<SpuAppVO>> getUserCollectionDtoPageByUserId(PageDTO page, String prodName, Integer prodType) {
        return ServerResponseEntity.success(spuCollectionService.getUserCollectionDtoPageByUserId(page, AuthUserContext.get().getUserId(), prodName, prodType));
    }

    @GetMapping("/ma/isCollection")
    @Operation(summary = "根据商品id获取该商品是否在收藏夹中", description = "传入收藏商品id")
    @Parameter(name = "spuId", description = "商品id")
    public ServerResponseEntity<Boolean> isCollection(@RequestParam Long spuId) {
        UidInfoBO uidInfoBO = AuthUserContext.get();
        if (uidInfoBO == null) {
            return ServerResponseEntity.fail(ResponseEnum.CLEAN_TOKEN);
        }
        int count = spuCollectionService.userCollectionCount(spuId, uidInfoBO.getUserId());
        return ServerResponseEntity.success(count > 0);

    }

    @PostMapping("/createOrCancel")
    @Operation(summary = "添加/取消收藏(返回false：取消收藏  true：添加收藏)", description = "传入收藏商品id,如果商品未收藏则收藏商品，已收藏则取消收藏")
    public ServerResponseEntity<Boolean> createOrCancel(@RequestBody Long spuId) {
        if (Objects.isNull(spuService.getBySpuId(spuId))) {
            throw new LuckException("该商品不存在");
        }
        boolean isAdd = false;
        Long userId = AuthUserContext.get().getUserId();
        if (spuCollectionService.userCollectionCount(spuId, userId) > 0) {
            spuCollectionService.deleteBySpuIdAndUserId(spuId, userId);
        } else {
            SpuVO spuVo = spuService.getBySpuId(spuId);
            if (Objects.equals(spuVo.getSpuType(), SpuType.ACTIVE.value())) {
                // 活动商品不能添加收藏
                throw new LuckException("活动商品不能添加收藏");
            }
            SpuCollection userCollection = new SpuCollection();
            userCollection.setUserId(userId);
            userCollection.setSpuId(spuId);
            spuCollectionService.save(userCollection);
            isAdd = true;
        }
        return ServerResponseEntity.success(isAdd);
    }

    @PostMapping("/batch_cancel")
    @Operation(summary = "批量取消收藏", description = "传入收藏商品id")
    public ServerResponseEntity<Void> batachCancel(@RequestBody List<Long> spuIds) {
        Long userId = AuthUserContext.get().getUserId();
        if (CollUtil.isEmpty(spuIds)) {
            return ServerResponseEntity.fail(ResponseEnum.DATA_ERROR);
        }
        spuCollectionService.deleteBatchBySpuIdsAndUserId(spuIds, userId);
        return ServerResponseEntity.success();
    }


    /**
     * 查询用户收藏商品数量
     */
    @GetMapping("count")
    @Operation(summary = "查询用户收藏商品数量", description = "查询用户收藏商品数量")
    public int findUserCollectionCount() {
        return spuCollectionService.userCollectionCount(null, AuthUserContext.get().getUserId());
    }

    @PostMapping("/order_prod_collection_all")
    @Operation(summary = "订单商品收藏", description = "传入商品id拼接字符串")
    @Parameter(name = "spuIdList", description = "spuId集合")
    public ServerResponseEntity<Boolean> orderProdCollectionAll(@RequestBody List<Long> spuIdList) {
        if (CollectionUtil.isEmpty(spuIdList)) {
            throw new LuckException("spuId列表不能为空");
        }
        // 去重
        HashSet<Long> spuIdSet = new HashSet<>(spuIdList);
        spuCollectionService.spuBatchCollection(new ArrayList<>(spuIdSet));
        return ServerResponseEntity.success();
    }
}
