package com.rf.psychological.scale.rest;

import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.base.rest.BaseController;
import com.rf.psychological.enums.UserRole;
import com.rf.psychological.group.service.GroupInfoService;
import com.rf.psychological.scale.dao.model.ScaleExternalSourceEntity;
import com.rf.psychological.scale.service.ScaleExternalSourceService;
import com.rf.psychological.security.AesEncryptUtils;
import com.rf.psychological.security.SafetyProcess;
import com.rf.psychological.user.dao.model.UserEntity;
import com.rf.psychological.user.service.UserService;
import com.rf.psychological.utils.Constant;
import com.rf.psychological.utils.JWTUtil;
import com.rf.psychological.utils.Result;
import com.rf.psychological.utils.SnowFlakeUtil;
import com.rf.psychological.wxpay.service.PromotionInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * @author zsy
 * @description:量表外部来源信息管理
 * @date 2023/10/09 15:55
 */

@Slf4j
@RestController
@RequestMapping("/scaleExternalSource")
@Api(tags = "量表外部来源信息管理")
public class ScaleExternalSourceController extends BaseController {

    @Autowired
    private ScaleExternalSourceService scaleExternalSourceService;

    @Autowired
    private GroupInfoService groupInfoService;

    @Autowired
    private UserService userService;

    @Autowired
    private PromotionInfoService promotionInfoService;



    /**
     * 添加量表类别信息
     *
     * @return
     */
    @SafetyProcess
    @PostMapping("/save")
    @ApiOperation(value = "量表外部来源信息--保存")
    public Result saveScaleExternalSource(@RequestBody String json)  {
        try {
            String data = AesEncryptUtils.decrypt(JSONObject.parseObject(json).getString("data"));
            JSONObject jsonObject = JSONObject.parseObject(data);
            System.out.println("----------------result-------------------");
            System.out.println(jsonObject);
            String uid = jsonObject.getString("uid");
            int source = jsonObject.getInteger("source");
            String flag = jsonObject.getString("flag");
            String userId = jsonObject.getString("currentUserId");
            String resultId = jsonObject.getString("resultId");
            ScaleExternalSourceEntity scaleExternalSourceEntity = new ScaleExternalSourceEntity();
            scaleExternalSourceEntity.setUid(uid);
            scaleExternalSourceEntity.setSource(source);
            scaleExternalSourceEntity.setFlag(flag);
            scaleExternalSourceEntity.setUserId(userId);
            scaleExternalSourceEntity.setResultId(resultId);
            scaleExternalSourceEntity.setCreateTime(DateUtil.date());
            this.scaleExternalSourceService.save(scaleExternalSourceEntity);
            return success();
        }catch (Exception e){
            log.debug(e.getMessage());
            return fail();
        }

    }

    /**
     * 量表查询所有类别信息
     *
     * @return
     */
    /*@SafetyProcess
    @GetMapping("/getCategoryList")
    @ApiOperation(value = "量表查询所有类别信息")
    public Result getCategoryList(String institutionNo, String gId) {
        //游客用户无gid，数据库配置特定用户
        if (StringUtils.isEmpty(gId)){
            GroupEntity group = groupInfoService.findGroupByInstitutionNoAndName(institutionNo, Constant.DEFAULT_GROUP_NAME);
            if (group != null){
                gId = group.getId();
            }
        }
        return success(this.categoryService.findALLCategorySubject(institutionNo, gId));
    }

    *//**
     * 认知任务查询所有类别信息
     *
     * @return
     *//*
    @SafetyProcess
    @GetMapping("/getCognizeCategoryList")
    @ApiOperation(value = "认知任务查询所有类别信息")
    public Result getCognizeCategoryList(String institutionNo, String gId) {
        //游客用户无id，数据库配置特定用户
        if (StringUtils.isEmpty(gId)){
            GroupEntity group = groupInfoService.findGroupByInstitutionNoAndName(institutionNo, Constant.DEFAULT_GROUP_NAME);
            if (group != null){
                gId = group.getId();
            }
        }
        return success(this.categoryService.findAllCT(institutionNo, gId));
    }

    *//**
     * 全量查询所有类别信息-不加密，后台维护管理使用
     *//*
    @GetMapping("/loadCategory")
    public Result<List<CategoryEntity>> loadCategory() {
        List<CategoryEntity> categoryEntities = this.categoryService.findAlls();
        return success(categoryEntities);
    }

    *//**
     * 相关测评
     *//*
    @SafetyProcess
    @GetMapping("/getRelevantEvaluation")
    @ApiOperation(value = "获取相关测评信息",notes = " flag:量表或认知任务flag,type:类型0量表1认知任务")
    public Result getRelevantEvaluation(String flag, String type) {
        if (type.equals(Constant.DEFAULT_VALUE_ZERO)){
            List<SubjectEntity> subjectEntities = this.subjectService.getRelevantEvaluation(flag);
            return success(subjectEntities);
        }else if (type.equals(Constant.DEFAULT_VALUE_ONE)){
            List<CognitiveTaskEntity> cognitiveTaskEntities = this.cognitiveTaskService.getRelevantEvaluation(flag);
            List<CognitiveTaskEntityDto> dto2List = new ArrayList<>();
            if(cognitiveTaskEntities.size()>0){
                cognitiveTaskEntities.forEach(cognitiveTaskEntity -> {
                    CognitiveTaskEntityDto dto = new CognitiveTaskEntityDto(cognitiveTaskEntity);
                    PromotionInfo infoBySubjectId = promotionInfoService.findInfoBySubjectId(cognitiveTaskEntity.getId());
                    dto.setPrice( infoBySubjectId ==null?0.0:infoBySubjectId.getPrice());
                    dto2List.add(dto);
                });
            }
            return success(dto2List);
        }else {
            return fail("类型参数错误！");
        }
    }*/

}
