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.OrderStatus;
import com.rf.psychological.scale.dao.model.UserRecordEntity;
import com.rf.psychological.scale.service.UserRecordService;
import com.rf.psychological.security.AesEncryptUtils;
import com.rf.psychological.security.SafetyProcess;
import com.rf.psychological.utils.Result;
import com.rf.psychological.wxpay.model.OrderInfo;
import com.rf.psychological.wxpay.service.OrderInfoService;
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.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;


/**
 * @author zsy
 * @description:测试记录信息管理
 * @date 2021/6/17 15:55
 */

@Slf4j
@RestController
@RequestMapping("/record")
@Api(tags = "测试记录信息管理")
@ConditionalOnProperty(prefix="wx.pay",name = "is_lan", havingValue = "false")
public class UserRecordController extends BaseController {

    @Autowired
    private UserRecordService userRecordService;

    @Autowired
    private OrderInfoService orderInfoService;

    /**
     * 微博分享-通过手机号查询测试记录
     * @param jsonParam 参数
     * @return
     */
    @SafetyProcess
    @ApiOperation(value = "微博分享-通过手机号查询测试记录",notes = "json字符串形式传参（加密）,data参数包括:pageNum:第几页,pageSize:每页条数,modelPhone:手机号")
    @PostMapping("/getRecordByModelPhone")
    public Result getRecordByModelPhone(@RequestBody String jsonParam) throws Exception {
        JSONObject dataParam = JSONObject.parseObject(AesEncryptUtils.decrypt(JSONObject.parseObject(jsonParam).getString("data")));
        String modelPhone = dataParam.getString("modelPhone");
        int pageNum = dataParam.getInteger("pageNum");
        int pageSize = dataParam.getInteger("pageSize");
        String authCode = dataParam.getString("authCode");
        JSONObject jsonVerification = dataParam.getJSONObject("verification");
        String jsonPhone = jsonVerification.getString("phone");
        String jsonAuthCode = jsonVerification.getString("authCode");
        if (StringUtils.isEmpty(jsonAuthCode) ) {
            return fail("", "请重新获取验证码");
        }
        if (jsonAuthCode.equals(authCode) && jsonPhone.equals(modelPhone)) {
            Page<OrderInfo> orderInfos = this.orderInfoService.getOrderByUserId(pageNum, pageSize, modelPhone,OrderStatus.SUCCESS.getType());
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("orderList",orderInfos.getContent());
            jsonObject.put("allNum", orderInfos.getTotalElements());
            return success(jsonObject);
        } else {
            return fail("", "验证码错误");
        }


    }


    /**
     * 根据id查询测试详细信息
     *
     * @return
     */
    @SafetyProcess
    @GetMapping("/getWeiboRecordById")
    @ApiOperation(value = "根据id查询测试详细信息")
    public Result getWeiboRecordById(@RequestParam(name = "id") String id) {
        try {
            log.info("获取记录开始时间==========================="+DateUtil.current());
            //获取测试记录信息
            UserRecordEntity userRecordEntity = this.userRecordService.getUserRecordById(id);
            log.info("获取记录查询时间==========================="+DateUtil.current());
            if (userRecordEntity == null){
                return fail("参数错误");
            }
            JSONObject result = new JSONObject();
            result.put("userRecordEntity", userRecordEntity);
            log.info("获取记录结束时间==========================="+DateUtil.current());
            return success(result);
        }catch (Exception e){
            log.error(e.getMessage());
            return fail("操作失败");
        }
    }




}
