package com.yonge.cooleshow.teacher.controller;

import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.service.ContractService;
import com.yonge.cooleshow.common.enums.ContractTemplateSourceEnum;
import com.yonge.cooleshow.common.enums.ContractTemplateTypeEnum;
import com.yonge.cooleshow.common.enums.SysUserType;
import com.yonge.cooleshow.biz.dal.entity.ContractTemplate;
import com.yonge.cooleshow.biz.dal.entity.SysUserContractRecord;
import com.yonge.cooleshow.biz.dal.enums.DealStatusEnum;
import com.yonge.cooleshow.biz.dal.sdk.WithdrawSdk;
import com.yonge.cooleshow.biz.dal.service.ContractTemplateService;
import com.yonge.cooleshow.biz.dal.service.SysUserContractRecordService;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.cooleshow.common.service.IdGeneratorService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 用户协议记录表-一个用户一种协议一个版本一条记录(SysUserContractRecord)表控制层
 *
 * @author hgw
 * @since 2022-05-07 15:04:05
 */
@Api(tags = "用户协议记录表-一个用户一种协议一个版本一条记录")
@RestController
@RequestMapping("/sysUserContractRecord")
public class SysUserContractRecordController extends BaseController {
    @Autowired
    private SysUserFeignService sysUserFeignService;
    @Autowired
    private ContractTemplateService contractTemplateService;
    @Resource
    private SysUserContractRecordService sysUserContractRecordService;
    @Autowired
    private ContractService contractService;

    @ApiOperation("判断用户是否签署协议")
    @GetMapping(value = "/checkContractSign", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResponseResult<Boolean> checkContractSign(@ApiParam(value = "业务类型 REGISTER 注册 COURSES 课程购买 PRODUCT 产品 WITHDRAW 结算", required = true) @RequestParam("contractType") ContractTemplateTypeEnum contractType) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }

        return HttpResponseResult.succeed(sysUserContractRecordService.checkContractSign(user.getId(), SysUserType.TEACHER, contractType));
    }

    @ApiOperation("查看协议-获取协议模板")
    @GetMapping(value = "/queryLatestContractTemplate")
    public HttpResponseResult<ContractTemplate> queryLatestContractTemplate(@ApiParam(value = "业务类型 REGISTER 注册 COURSES 课程购买 PRODUCT 产品 WITHDRAW 结算", required = true) @RequestParam("contractType") ContractTemplateTypeEnum contractType) {
        return succeed(contractTemplateService.queryLatestContractTemplate(contractType));
    }

    @ApiOperation("查看协议-获取协议内容")
    @GetMapping(value = "/queryContract", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResponseResult<String> queryContract(
            @ApiParam(value = "协议类型", required = true) @RequestParam("contractType") ContractTemplateTypeEnum contractType) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return contractService.queryProductContract(user.getId(),SysUserType.TEACHER,contractType);
    }

    @ApiOperation("签署协议")
    @GetMapping(value = "/sign", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public HttpResponseResult<Boolean> sign(
            @ApiParam(value = "协议类型", required = true) @RequestParam("contractType") ContractTemplateTypeEnum contractType) {
        SysUser user = sysUserFeignService.queryUserInfo();
        if (user == null || null == user.getId()) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        return sysUserContractRecordService.sign(contractType, SysUserType.TEACHER, user);
    }



}

