package com.rf.psychological.scale.rest;

import com.alibaba.fastjson.JSONObject;
import com.rf.psychological.base.rest.BaseController;
import com.rf.psychological.opLog.annotation.OperationLogAnnotation;
import com.rf.psychological.scale.dao.model.PsychologicalCapitalEntity;
import com.rf.psychological.scale.dao.model.UserRecordEntity;
import com.rf.psychological.scale.service.PsychologicalCapitalService;
import com.rf.psychological.scale.service.UserRecordService;
import com.rf.psychological.security.AesEncryptUtils;
import com.rf.psychological.security.SafetyProcess;
import com.rf.psychological.utils.*;
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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;


/**
 * @author zsy
 * @description:心理资本认知任务
 * @date 2021/6/17 15:55
 */

@Slf4j
@RestController
@RequestMapping("/psycapital")
@Api(tags = "心理资本认知任务")
public class PsychologicalCapitalController extends BaseController {

    @Autowired
    private UserRecordService userRecordService;

    @Autowired
    private PsychologicalCapitalService psychologicalCapitalService;

    /**
     * 添加心理资本信息
     *
     * @param
     * @return
     */
    @SafetyProcess
    @OperationLogAnnotation
    @PostMapping("/savePsycapital")
    @ApiOperation("添加心理资本信息")
    public Result savePsycapital(@RequestBody String json) throws Exception {
        //public Result saveMemoryCheck(@RequestBody JSONObject jsonObject ) throws Exception {
        String data = AesEncryptUtils.decrypt(JSONObject.parseObject(json).getString("data"));
        JSONObject Psycapital = JSONObject.parseObject(data);

        DateFormat df = new SimpleDateFormat("yyyyMMddHHmm");
        //当前时间
        String testTime = df.format(new Date());
        PsychologicalCapitalEntity psychologicalCapitalEntity = new PsychologicalCapitalEntity();
        psychologicalCapitalEntity.setTaskMark(Psycapital.getString("taskMark"));
        psychologicalCapitalEntity.setHierarchy(Psycapital.getString("hierarchy"));
        psychologicalCapitalEntity.setRecordId(Psycapital.getString("recordId"));
        psychologicalCapitalEntity.setTestDate(testTime);
        if (Psycapital.getString("hierarchy").equals("1.3")){
            UserRecordEntity userRecordEntity = userRecordService.getUserRecordById(Psycapital.getString("recordId"));
            String testresult = userRecordEntity.getTestResults();
            String[] testresults = testresult.split(";");
            String[] scores = testresults[0].split(":");
            double score = Double.parseDouble(scores[1]);
            if (score <= 126){
                psychologicalCapitalEntity.setGroupIndex("2");
            }else {
                psychologicalCapitalEntity.setGroupIndex("1");
            }
            Random r = new Random();
            //r.nextInt(n)  n为几  就产生0~n-1的随机数
            int num = r.nextInt(2)+1;
            psychologicalCapitalEntity.setStrongType(String.valueOf(num));
        }
        this.psychologicalCapitalService.save(psychologicalCapitalEntity);
        return success(psychologicalCapitalEntity, "完成");
    }

}
