package com.rf.psychological.miniapp.praise.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.praise.model.PraiseEntity;
import com.rf.psychological.miniapp.praise.service.PraiseService;
import com.rf.psychological.security.AesEncryptUtils;
import com.rf.psychological.security.SafetyProcess;
import com.rf.psychological.utils.Constant;
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.collections4.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @Description:点赞接口
 * @Author: mimang
 * @Date: 2023/5/26
 */
@Slf4j
@RestController
@RequestMapping("/praise")
@Api(tags = "点赞管理")
public class PraiseController extends BaseController {

    @Autowired
    private PraiseService praiseService;

    @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,"参数不能为空");
            }
            PraiseEntity entity = JSONObject.parseObject(paramData,PraiseEntity.class);
            entity = praiseService.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,"参数不能为空");
            }
            PraiseEntity entity = praiseService.findById(id);
            if (entity == null){
                return fail(null,"ID不存在");
            }
            entity = praiseService.cancel(entity);
            return success(entity);
        }catch (Exception e){
            e.printStackTrace();
            return fail("操作失败");
        }
    }


    @SafetyProcess
    @ApiOperation(value = "判断是否点赞",notes = "businessId:量表或者认知任务id,userId:用户ID,type:0赞同1不赞同")
    @GetMapping("/isPraise")
    public Result isPraise(@RequestParam String businessId,@RequestParam String userId,@RequestParam(required = false) String type){
        try {
            if (StringUtils.isEmpty(businessId)){
                return fail(null,"参数不能为空");
            }
            PraiseEntity entity = praiseService.isPraise(businessId,userId,type);
            return success(entity);
        }catch (Exception e){
            e.printStackTrace();
            return fail("操作失败");
        }
    }

    @SafetyProcess
    @ApiOperation(value = "点赞统计",notes = "businessId:量表或者认知任务id")
    @GetMapping("/query")
    public Result query(@RequestParam String businessId){
        try {
            if (StringUtils.isEmpty(businessId)){
                return fail(null,"参数不能为空");
            }
            int[] result ={0,0};
            List<Map> map = praiseService.queryByBusinessId(businessId);
            if (CollectionUtils.isNotEmpty(map)){
                map.forEach(item->{
                    if (item.get("type").equals(Constant.DEFAULT_VALUE_ZERO)){
                        result[0] = Integer.valueOf(item.get("num").toString());
                    }
                    if (item.get("type").equals(Constant.DEFAULT_VALUE_ONE)){
                        result[1] = Integer.valueOf(item.get("num").toString());
                    }
                });
            }
            return success(result);
        }catch (Exception e){
            e.printStackTrace();
            return fail("操作失败");
        }
    }
}
