package com.yonge.cooleshow.student.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.microsvc.toolkit.common.response.paging.PageInfo;
import com.microsvc.toolkit.common.response.paging.QueryInfo;
import com.microsvc.toolkit.common.webportal.exception.BizException;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.entity.Student;
import com.yonge.cooleshow.biz.dal.service.StudentService;
import com.yonge.cooleshow.biz.dal.service.TenantActivationCodeService;
import com.yonge.cooleshow.biz.dal.wrapper.TenantActivationCodeWrapper;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.cooleshow.common.enums.EActivationCode;
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.validation.annotation.Validated;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@Slf4j
@Validated
@RestController
@RequestMapping("${app-config.url.student:}/tenantActivationCode")
@Api(tags = "机构激活码")
public class TenantActivationCodeController extends BaseController {

    @Autowired
    private TenantActivationCodeService tenantActivationCodeService;

    @Autowired
    private SysUserFeignService sysUserFeignService;

    @Autowired
    private StudentService studentService;

    @ApiOperation(value = "查询分页", notes = "机构激活码- 传入 TenantActivationCodeVo.TenantActivationCodeQuery")
    @PostMapping("/page")
    public HttpResponseResult<PageInfo<TenantActivationCodeWrapper.TenantActivationCode>> page(
            @RequestBody TenantActivationCodeWrapper.TenantActivationCodeQuery query) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null || null == sysUser.getId()) {
            throw new BizException("请登录");
        }
        Student student = studentService.getById(sysUser.getId());
        if (student == null) {
            throw new BizException("学生不存在");
        }
        query.setSendStatus(EActivationCode.SEND);
        query.setActivationPhone(sysUser.getPhone());
        // 查询数据
        IPage<TenantActivationCodeWrapper.TenantActivationCode> pages =
                tenantActivationCodeService.selectPage(QueryInfo.getPage(query), query);
        for (TenantActivationCodeWrapper.TenantActivationCode record : pages.getRecords()) {
            String activationCode = record.getActivationCode();
            String substring = activationCode.substring(3);
            record.setActivationCode("***" + substring);
        }
        return succeed(QueryInfo.pageInfo(pages, pages.getRecords()));
    }

    @ApiOperation(value = "激活激活码")
    @PostMapping("/active")
    public HttpResponseResult<Boolean> active(@RequestParam("activationCode") String activationCode) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null || null == sysUser.getId()) {
            throw new BizException("请登录");
        }
        tenantActivationCodeService.active(activationCode, sysUser.getId());
        return succeed();
    }

    @ApiOperation(value = "激活激活码")
    @PostMapping("/activeById")
    public HttpResponseResult<Boolean> activeById(@RequestParam("id") String id) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null || null == sysUser.getId()) {
            throw new BizException("请登录");
        }
        tenantActivationCodeService.activeById(id, sysUser.getId());
        return succeed();
    }
}