package com.gxwebsoft.open.controller;

import cn.hutool.core.util.StrUtil;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.core.utils.CacheClient;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.system.entity.User;
import com.gxwebsoft.common.system.param.UserParam;
import com.gxwebsoft.common.system.service.TenantService;
import com.gxwebsoft.common.system.service.UserService;
import com.gxwebsoft.oa.entity.App;
import com.gxwebsoft.oa.param.AppParam;
import com.gxwebsoft.oa.service.AppService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import static com.gxwebsoft.common.core.constants.RedisConstants.*;

/**
 * 应用控制器
 *
 * @author WebSoft
 * @since 2022-11-18 13:55:29
 */
@Api(tags = "应用市场")
@RestController
@RequestMapping("/api/open/market")
public class OpenMarketController extends BaseController {
    @Resource
    private AppService appService;
    @Resource
    private UserService userService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private TenantService tenantService;
    @Resource
    private CacheClient cacheClient;

    @ApiOperation("分页查询应用")
    @GetMapping("/page")
    public ApiResult<PageResult<App>> page(AppParam param) {
        // 验证签名
//        isCheckSign();
//        PageParam<App, AppParam> page = new PageParam<>(param);
//        page.setDefaultOrder("create_time desc");
//        return success(appService.page(page, page.getWrapper()));

        // 使用关联查询
        param.setSearch(1);
        return success(appService.pageRel(param));
    }

    @ApiOperation("查询全部应用")
    @GetMapping()
    public ApiResult<List<App>> list(AppParam param) {
        // 验证签名
        isCheckSign();
//        PageParam<App, AppParam> page = new PageParam<>(param);
//        page.setDefaultOrder("create_time desc");
//        return success(appService.list(page.getOrderWrapper()));

        // 使用关联查询
        param.setSearch(1);
        return success(appService.listRel(param));
    }

    @ApiOperation("根据id查询应用")
    @GetMapping("/{id}")
    public ApiResult<App> get(@PathVariable("id") Integer id) {
//        return success(appService.getById(id));
        // 验证签名
        isCheckSign();
        return success(appService.getByIdRel(id));
    }

    @OperationLog
    @ApiOperation("排行榜")
    @GetMapping("/ranking")
    public ApiResult<?> ranking(UserParam param) {
      // 使用关联查询
      Set<String> reverseRange = cacheClient.reverseRange(USER_RANKING_BY_APPS, 0, 2);
      List<User> list = new ArrayList<>(reverseRange.size());
      if(reverseRange.size() == 0) {
        userService.listRanking(param);
      }
      reverseRange.forEach(v -> {
        Double score = cacheClient.score(USER_RANKING_BY_APPS, v);
        User user = userService.getById(v);
        user.setApps(score);
        list.add(user);
      });
      return success("操作成功",list);
    }

    @ApiOperation("搜索历史")
    @GetMapping("/search-history")
    public ApiResult<?> searchHistory(String keyword, Long number) {
      // 验证签名
      isCheckSign();
      String key = cacheClient.key(SEARCH_HISTORY,getLoginUserId());
      if(StrUtil.isNotBlank(keyword)){
        cacheClient.leftPush(key,keyword);
      }
      // 超出100裁剪
      Long size = cacheClient.listSize(key);
      if (number == null) {
        number = 9L;
      }
      return success("操作成功",cacheClient.listRange(key,0L,number));
    }

}
