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

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.response.ResponseEnum;
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.multishop.constant.HotSearchType;
import com.tmerclub.cloud.multishop.dto.HotSearchDTO;
import com.tmerclub.cloud.multishop.model.HotSearch;
import com.tmerclub.cloud.multishop.service.HotSearchService;
import com.tmerclub.cloud.multishop.vo.HotSearchVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * 热搜
 *
 * @author YXF
 * @date 2021-01-27 09:10:00
 */
@RestController("adminHotSearchController")
@RequestMapping("/mp/hotQuery")
@Tag(name = "admin-热搜")
public class HotSearchController {
    private static final Logger log = LoggerFactory.getLogger(HotSearchController.class);


    @Autowired
    private HotSearchService hotSearchService;


    @GetMapping("/page")
    @Operation(summary = "分页获取热搜列表", description = "分页获取热搜列表")
    public ServerResponseEntity<PageVO<HotSearchVO>> page(@Valid PageDTO pageDTO, HotSearchDTO hotSearchDTO) {
        hotSearchDTO.setShopId(AuthUserContext.get().getTenantId());
        PageVO<HotSearchVO> hotSearchPage = hotSearchService.page(pageDTO, hotSearchDTO);
        return ServerResponseEntity.success(hotSearchPage);
    }

    @GetMapping
    @Operation(summary = "获取热搜", description = "根据hotSearchId获取热搜")
    public ServerResponseEntity<HotSearchVO> getByHotSearchId(@RequestParam Long hotSearchId) {
        return ServerResponseEntity.success(hotSearchService.getByHotSearchId(hotSearchId));
    }

    @PostMapping
    @Operation(summary = "保存热搜", description = "保存热搜")
    public ServerResponseEntity<Void> save(@Valid @RequestBody HotSearchDTO hotSearchDTO) {
        HotSearch hotSearch = BeanUtil.map(hotSearchDTO, HotSearch.class);
        hotSearch.setShopId(AuthUserContext.get().getTenantId());
        if (Objects.isNull(hotSearch.getType())) {
            hotSearch.setType(HotSearchType.PROD.value());
        }
        // 热搜标题不能重复
        if (hotSearchService.checkExist(hotSearch.getShopId(), hotSearch.getTitle(), hotSearch.getType())) {
            log.error("热搜标题不能重复");
            throw new LuckException("热搜标题不能重复");
        }
        hotSearchService.save(hotSearch);
        hotSearchService.removeHotSearchListCache(hotSearch.getShopId(), hotSearch.getType());
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新热搜", description = "更新热搜")
    public ServerResponseEntity<Void> update(@Valid @RequestBody HotSearchDTO hotSearchDTO) {
        HotSearch hotSearch = BeanUtil.map(hotSearchDTO, HotSearch.class);
        HotSearchVO dbHotSearch = hotSearchService.getByHotSearchId(hotSearchDTO.getHotSearchId());
        if (Objects.isNull(dbHotSearch)) {
            throw new LuckException(ResponseEnum.DATA_ERROR);
        }
        hotSearch.setShopId(AuthUserContext.get().getTenantId());
        if (!Objects.equals(dbHotSearch.getShopId(), hotSearch.getShopId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        hotSearchService.update(hotSearch);
        hotSearchService.removeHotSearchListCache(hotSearch.getShopId(), dbHotSearch.getType());
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除热搜", description = "根据热搜id删除热搜")
    public ServerResponseEntity<Void> delete(@RequestParam Long hotSearchId) {
        HotSearchVO dbHotSearch = hotSearchService.getByHotSearchId(hotSearchId);
        if (Objects.isNull(dbHotSearch)) {
            throw new LuckException(ResponseEnum.DATA_ERROR);
        }
        if (!Objects.equals(dbHotSearch.getShopId(), AuthUserContext.get().getTenantId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        Long shopId = AuthUserContext.get().getTenantId();
        hotSearchService.deleteById(hotSearchId, shopId);
        hotSearchService.removeHotSearchListCache(shopId, dbHotSearch.getType());
        return ServerResponseEntity.success();
    }
}
