package com.rf.psychological.opLog.rest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.base.rest.BaseController;
import com.rf.psychological.opLog.dao.UserOperateLogEntity;
import com.rf.psychological.opLog.service.UserOperateLogService;
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.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.Map;

/**
 * @Description:用户操作记录埋点接口
 * @Author: mimang
 * @Date: 2023/5/6
 */
@Slf4j
@RestController
@RequestMapping("/userOperateLog")
@Api(tags = "用户操作埋点记录管理")
public class UserOperateLogController extends BaseController {

    @Autowired
    private UserOperateLogService userOperateLogService;


    @SafetyProcess
    @ApiOperation(value = "埋点保存",notes = "json字符串形式传参(加密)，" +
            "data参数中包括(必传)：userId:用户id,userName:用户名称，operateType:操作类型0搜索1分享2查看3提交4支付，businessType：业务类型0量表1认知任务2测试记录3量表分类4认知任务分类，" +
            "businessValue：业务ID值")
    @PostMapping("/saveLog")
    public Result saveLog(@RequestBody String paramJson){
        try {
            String paramData = AesEncryptUtils.decrypt(JSONObject.parseObject(paramJson).getString("data"));
            log.info(paramData);
            if (StringUtils.isEmpty(paramData)){
                return fail(null,"参数不能为空");
            }
            UserOperateLogEntity entity = JSONObject.parseObject(paramData,UserOperateLogEntity.class);
            entity = userOperateLogService.saveLog(entity);
            return success(entity);
        }catch (Exception e){
            e.printStackTrace();
            return fail("操作失败");
        }
    }
}
