package com.rf.psychological.miniapp.searchhistory.controller;

import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.base.rest.BaseController;
import com.rf.psychological.miniapp.searchhistory.model.SearchHistoryEntity;
import com.rf.psychological.miniapp.searchhistory.service.SearchHistoryService;
import com.rf.psychological.security.AesEncryptUtils;
import com.rf.psychological.security.SafetyProcess;
import com.rf.psychological.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description:搜索历史接口
 * @Author: mimang
 * @Date: 2023/5/16
 */
@Slf4j
@RestController
@RequestMapping("/searchHistory")
@Api(tags = "搜索历史管理")
public class SearchHistoryController extends BaseController {

    @Autowired
    private SearchHistoryService service;

    @SafetyProcess
    @ApiOperation(value = "搜索保存",notes = "json字符串形式传参(加密)，" +
            "data参数中包括(必传)：userId:用户id,searchValue:搜索内容")
    @PostMapping("/save")
    public Result save(@RequestBody String paramJson){
        try {
            String paramData = AesEncryptUtils.decrypt(JSONObject.parseObject(paramJson).getString("data"));
            log.info(paramData);
            if (StringUtils.isEmpty(paramData)){
                return fail(null,"参数不能为空");
            }
            SearchHistoryEntity entity = JSONObject.parseObject(paramData,SearchHistoryEntity.class);
            entity = service.save(entity);
            return success(entity);
        }catch (Exception e){
            e.printStackTrace();
            return fail("操作失败");
        }
    }

    @SafetyProcess
    @ApiOperation(value = "查询搜索内容",notes = " userId:用户id,page:页码,pageSize:数量")
    @GetMapping("/query")
    public Result queryByOpenId(@RequestParam String userId,@RequestParam int page,@RequestParam int pageSize){
        try {
            Page<SearchHistoryEntity> historyEntities = service.queryByOpenId(userId,page,pageSize);
            return success(historyEntities);
        }catch (Exception e){
            e.printStackTrace();
            return fail("操作失败");
        }
    }

    @SafetyProcess
    @ApiOperation(value = "查询搜索内容",notes = " userId:用户id,page:页码,pageSize:数量")
    @GetMapping("/queryListByUserId")
    public Result queryListByUserId(@RequestParam String userId){
        try {
            List<String> values =  service.queryListByUserId( userId);
            return success(values);
        }catch (Exception e){
            e.printStackTrace();
            return fail("操作失败");
        }
    }

    @SafetyProcess
    @ApiOperation(value = "删除搜索记录",notes = "id:历史id")
    @GetMapping("/delete")
    public Result deleteById(@RequestParam String id){
        try {
            SearchHistoryEntity entity = service.deleteById(id);
            return success(entity);
        }catch (Exception e){
            e.printStackTrace();
            return fail("操作失败");
        }
    }

    @SafetyProcess
    @ApiOperation(value = "删除所有搜索记录",notes = "id:用户id")
    @GetMapping("/deleteAll")
    public Result deleteByUserId(@RequestParam String id){
        try {
            service.deleteByUserId(id);
            return success();
        }catch (Exception e){
            e.printStackTrace();
            return fail("操作失败");
        }
    }
}
