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

import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.base.rest.BaseController;
import com.rf.psychological.miniapp.browse.model.BrowseEntity;
import com.rf.psychological.miniapp.collect.model.CollectEntity;
import com.rf.psychological.miniapp.collect.service.CollectService;
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.*;

/**
 * @Description:收藏接口
 * @Author: mimang
 * @Date: 2023/5/19
 */
@Slf4j
@RestController
@RequestMapping("/collect")
@Api(tags = "收藏管理")
public class CollectController extends BaseController {

    @Autowired
    private CollectService collectService;

    @SafetyProcess
    @ApiOperation(value = "收藏保存",notes = "json字符串形式传参(加密)，" +
            "data参数中包括(必传)：userId:用户id,businessId:量表或者认知任务id,type:类型0量表1认知任务")
    @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,"参数不能为空");
            }
            CollectEntity entity = JSONObject.parseObject(paramData,CollectEntity.class);
            entity = collectService.save(entity);
            return success(entity);
        }catch (Exception e){
            e.printStackTrace();
            return fail("操作失败");
        }
    }

    @SafetyProcess
    @ApiOperation(value = "取消收藏",notes = "id收藏Id")
    @GetMapping("/cancel")
    public Result cancel(@RequestParam String id){
        try {
            if (StringUtils.isEmpty(id)){
                return fail(null,"参数不能为空");
            }
            CollectEntity entity = collectService.findById(id);
            if (entity == null){
                return fail(null,"ID有误");
            }
            entity = collectService.cancel(entity);
            return success(entity);
        }catch (Exception e){
            e.printStackTrace();
            return fail("操作失败");
        }
    }

    @SafetyProcess
    @ApiOperation(value = "判断是否收藏过",notes = "id收藏Id")
    @GetMapping("/isCollect")
    public Result isCollect(@RequestParam  String userId,@RequestParam  String businessId){
        try {
            if (StringUtils.isEmpty(userId) || StringUtils.isEmpty(businessId)){
                return fail(null,"参数不能为空");
            }
            CollectEntity entity = collectService.findByUserIdAndBusinessId(userId,businessId);
            return success(entity);
        }catch (Exception e){
            e.printStackTrace();
            return fail("操作失败");
        }
    }

    @SafetyProcess
    @ApiOperation(value = "查询收藏记录",notes = " userId:用户id,type:类型,page:页码,pageSize:数量")
    @GetMapping("/query")
    public Result queryByUserId(@RequestParam String userId,@RequestParam(required = false) String type,@RequestParam int page,@RequestParam int pageSize){
        try {
            Page<CollectEntity> entities = collectService.queryByUserId(userId,type,page,pageSize);
            return success(entities);
        }catch (Exception e){
            e.printStackTrace();
            return fail("操作失败");
        }
    }
}
