package com.ym.mec.web.controller;

import com.ym.mec.biz.dal.entity.CloudTeacherStudent;
import com.ym.mec.biz.dal.entity.TenantOrderRecord;
import com.ym.mec.biz.dal.page.CloudTeacherAddQueryInfo;
import com.ym.mec.biz.dal.page.CloudTeacherOrderQueryInfo;
import com.ym.mec.biz.service.CloudTeacherOrderService;
import com.ym.mec.biz.service.OrganizationService;
import com.ym.mec.biz.service.TenantOrderRecordService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.entity.HttpResponseResult;
import com.ym.mec.common.page.PageInfo;
import com.ym.mec.common.page.WrapperUtil;
import com.ym.mec.common.tenant.TenantContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * Description
 *
 * @author: feng-ji
 * @date: 2021-12-21
 */
@RequestMapping("cloudTeacherOrder")
@Api(tags = "团练宝注册,激活,添加用户,支付等")
@RestController
public class CloudTeacherOrderController extends BaseController {

    @Autowired
    private CloudTeacherOrderService cloudTeacherOrderService;

    @Autowired
    private TenantOrderRecordService tenantOrderRecordService;

    @Autowired
    private OrganizationService organizationService;

    @ApiOperation(value = "手动添加会员")
    @PostMapping("/addStudents")
    @PreAuthorize("@pcs.hasPermissions('cloudTeacherOrder/addStudents')")
    public HttpResponseResult<Boolean> addStudents(@RequestBody CloudTeacherAddQueryInfo cloudTeacherAddQueryInfo) {
        return failed("该入口已关闭，请联系管理员");
//        return succeed(cloudTeacherOrderService.addStudents(cloudTeacherAddQueryInfo));
    }

    @ApiOperation(value = "查询未激活团练宝用户")
    @PostMapping("/queryInactive")
    @PreAuthorize("@pcs.hasPermissions('cloudTeacherOrder/queryInActive')")
    public HttpResponseResult<PageInfo<CloudTeacherStudent>> queryInactive(@RequestBody CloudTeacherOrderQueryInfo cloudTeacherOrderQueryInfo) throws Exception {
        Integer tenantId = TenantContextHolder.getTenantId();
        cloudTeacherOrderQueryInfo.setTenantId(tenantId);
        String organIds = organizationService.getEmployeeOrgan("");
        cloudTeacherOrderQueryInfo.setStatus(1);
        cloudTeacherOrderQueryInfo.setOrganIds(organIds); // 1,2,34,
        return succeed(cloudTeacherOrderService.queryInactive(cloudTeacherOrderQueryInfo));
    }

    @ApiOperation(value = "查询已激活团练宝用户")
    @PostMapping("/queryActive")
    @PreAuthorize("@pcs.hasPermissions('cloudTeacherOrder/queryActive')")
    public HttpResponseResult<PageInfo<CloudTeacherStudent>> queryActive(@RequestBody CloudTeacherOrderQueryInfo cloudTeacherOrderQueryInfo) throws Exception {
        Integer tenantId = TenantContextHolder.getTenantId();
        cloudTeacherOrderQueryInfo.setTenantId(tenantId);
        cloudTeacherOrderQueryInfo.setStatus(2);
        return succeed(cloudTeacherOrderService.queryInactive(cloudTeacherOrderQueryInfo));
    }

    @ApiOperation(value = "激活记录")
    @PostMapping("/queryActiveRecord")
    @PreAuthorize("@pcs.hasPermissions('cloudTeacherOrder/queryActiveRecord')")
    public HttpResponseResult<PageInfo<CloudTeacherStudent>> queryActiveRecord(@RequestBody CloudTeacherOrderQueryInfo cloudTeacherOrderQueryInfo) {
        Integer tenantId = TenantContextHolder.getTenantId();
        cloudTeacherOrderQueryInfo.setTenantId(tenantId);
        return succeed(cloudTeacherOrderService.queryRecord(cloudTeacherOrderQueryInfo));
    }

    @ApiOperation(value = "激活团练宝用户")
    @PostMapping("/pay")
    @PreAuthorize("@pcs.hasPermissions('cloudTeacherOrder/pay')")
    public HttpResponseResult<Map<String, Object>> pay(@RequestBody List<CloudTeacherStudent> cloudTeacherStudents) throws Exception {
        return succeed(cloudTeacherOrderService.pay(cloudTeacherStudents));
    }

    @ApiOperation(value = "轮询查询订单状态")
    @GetMapping("/payCheck")
    @PreAuthorize("@pcs.hasPermissions('cloudTeacherOrder/payCheck')")
    public HttpResponseResult<Boolean> payCheck(@RequestParam String orderNo) {
        return succeed(cloudTeacherOrderService.payCheck(orderNo));
    }

    @ApiOperation(value = "测试短信")
    @GetMapping("/testsms")
    public HttpResponseResult<Integer> testsms(@RequestParam String orderNo) throws Exception {
        TenantOrderRecord tor = tenantOrderRecordService.getOne(new WrapperUtil<TenantOrderRecord>().hasEq("order_no_", orderNo).queryWrapper());
        return succeed(cloudTeacherOrderService.sendSms(tor));
    }
}
