package org.dromara.app;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wzj.soopin.content.domain.bo.SearchBO;
import com.wzj.soopin.content.enums.SearchTypeEnum;
import com.wzj.soopin.content.service.VlogService;
import com.wzj.soopin.goods.domain.bo.ProductBo;
import com.wzj.soopin.goods.service.ProductService;
import com.wzj.soopin.member.domain.bo.MemberBO;
import com.wzj.soopin.member.service.IMemberService;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.domain.model.LoginUser;
import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.satoken.utils.LoginHelper;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

@Slf4j
@RestController
@RequestMapping("/app/common/search")
@Tag(name = "公共检索")
@RequiredArgsConstructor
public class AppSearchController {

    private final ProductService productService;
    private final IMemberService memberService;
    private final VlogService vlogService;

    @Tag(name = "视频、商品和用户检索")
    @PostMapping("")
    public R<IPage<?>> search(@RequestBody SearchBO searchBO, @RequestBody Page page) {
        try {
            LoginUser loginUser = LoginHelper.getLoginUser();
            if (loginUser == null) {
                throw new ServiceException("用户未登录");
            }
            searchBO.setUserId(String.valueOf(loginUser.getUserId()));
        } catch (Exception e) {
            log.error("用户没登陆", e);
        }
        if(searchBO.getTitle()==null || "".equals(searchBO.getTitle())){
            throw new ServiceException("请输入检索内容");
        }

        IPage<?> pages = null;
        // 视频检索信息
        if (Objects.nonNull(searchBO) && Objects.nonNull(searchBO.getType()) && (SearchTypeEnum.VIDEO.type.intValue() == searchBO.getType().intValue())) {
            pages = vlogService.getIndexSearchVlogList(searchBO, page);
            // 商品检索信息
        } else if (Objects.nonNull(searchBO) && Objects.nonNull(searchBO.getType()) && (SearchTypeEnum.PRODUCT.type.intValue() == searchBO.getType().intValue())) {
            ProductBo productBo = new ProductBo();
            productBo.setNameLike(searchBO.getTitle());
            pages = productService.getProductPageList(productBo, page);
            // 用户检索信息
        } else if (Objects.nonNull(searchBO) && Objects.nonNull(searchBO.getType()) && (SearchTypeEnum.MEMBER.type.intValue() == searchBO.getType().intValue())) {
            MemberBO memberBO = new MemberBO();
            memberBO.setNickname(searchBO.getTitle());
            pages = memberService.getMemberPageList(memberBO, page);
        }
        return R.ok(pages);
    }
}
