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

import com.tmerclub.cloud.common.constant.StatusEnum;
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.CategoryVO;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.product.bo.mongo.MongoSpuBrowseLogBO;
import com.tmerclub.cloud.product.dto.SpuBrowseLogDTO;
import com.tmerclub.cloud.product.model.SpuBrowseLog;
import com.tmerclub.cloud.product.service.CategoryService;
import com.tmerclub.cloud.product.service.SpuBrowseLogService;
import com.tmerclub.cloud.product.vo.SpuBrowseLogVO;
import io.swagger.v3.oas.annotations.Operation;
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.Objects;

/**
 * 商品浏览记录表
 *
 * @author YXF
 * @date 2021-03-19 14:28:14
 */
@RestController("appSpuBrowseLogController")
@RequestMapping("/spu_browse_log")
@Tag(name = "商品浏览记录表")
public class SpuBrowseLogController {

    @Autowired
    private SpuBrowseLogService spuBrowseLogService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/page")
    @Operation(summary = "获取商品浏览记录表列表", description = "分页获取商品浏览记录表列表")
    public ServerResponseEntity<PageVO<SpuBrowseLogVO>> page(@Valid PageDTO pageDTO) {
        PageVO<SpuBrowseLogVO> spuBrowseLogPage = spuBrowseLogService.page(pageDTO);
        return ServerResponseEntity.success(spuBrowseLogPage);
    }

    @PostMapping
    @Operation(summary = "保存商品浏览记录表", description = "保存商品浏览记录表")
    public ServerResponseEntity<Void> save(@RequestBody SpuBrowseLogDTO spuBrowseLogDTO) {
        if (Objects.isNull(spuBrowseLogDTO.getSpuId())) {
            throw new LuckException("商品id不能为空");
        }
        if (Objects.isNull(spuBrowseLogDTO.getCategoryId())) {
            throw new LuckException("分类id不能为空");
        }
        if (Objects.isNull(spuBrowseLogDTO.getSpuType())) {
            spuBrowseLogDTO.setSpuType(SpuType.NORMAL.value());
        }
        Long userId = AuthUserContext.get().getUserId();
        spuBrowseLogDTO.setUserId(userId);
        SpuBrowseLog spuBrowseLog = spuBrowseLogService.getCurrentLogBySpuIdAndUserId(spuBrowseLogDTO);
        // 商品记录已存在，则更新商品记录
        if (Objects.nonNull(spuBrowseLog) && Objects.equals(spuBrowseLog.getSpuType(), spuBrowseLogDTO.getSpuType())) {
            MongoSpuBrowseLogBO mongoSpuBrowseLogBO = BeanUtil.map(spuBrowseLog, MongoSpuBrowseLogBO.class);
            mongoSpuBrowseLogBO.setStatus(StatusEnum.ENABLE.value());
            spuBrowseLogService.updateStatus(mongoSpuBrowseLogBO);
            return ServerResponseEntity.success();
        }
        MongoSpuBrowseLogBO spuBrowse = BeanUtil.map(spuBrowseLogDTO, MongoSpuBrowseLogBO.class);
        if (!Objects.equals(spuBrowseLogDTO.getSpuType(), SpuType.SCORE.value())) {
            CategoryVO category = categoryService.getById(spuBrowse.getCategoryId());
            spuBrowse.setCategoryId(category.getPrimaryCategoryId());
        } else {
            spuBrowse.setCategoryId(0L);
        }
        spuBrowse.setUserId(userId);
        spuBrowseLogService.save(spuBrowse);
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "删除商品浏览记录表(单个删除传id，批量删除传数组)", description = "根据商品浏览记录表id删除商品浏览记录表")
    public ServerResponseEntity<Void> delete(@RequestBody SpuBrowseLogDTO spuBrowseLogDTO) {
        spuBrowseLogService.delete(spuBrowseLogDTO);
        return ServerResponseEntity.success();
    }
}
