package com.ym.mec.web.controller;

import com.ym.mec.auth.api.client.SysUserFeignService;
import com.ym.mec.auth.api.entity.SysUser;
import com.ym.mec.biz.dal.entity.SysTenantAccountDetail;
import com.ym.mec.biz.dal.page.TenantAccountDetailQueryInfo;
import com.ym.mec.biz.service.SysTenantAccountDetailService;
import com.ym.mec.biz.service.SysTenantAccountService;
import com.ym.mec.common.controller.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RequestMapping("sysTenantAccount")
@Api(tags = "后台租客账户管理")
@RestController
public class SysTenantAccountController extends BaseController {

    @Autowired
    private SysTenantAccountService sysTenantAccountService;

    @Autowired
    private SysTenantAccountDetailService sysTenantAccountDetailService;

    @Autowired
    private SysUserFeignService sysUserFeignService;

    @ApiOperation(value = "获取账户信息")
    @GetMapping("/get")
    @PreAuthorize("@pcs.hasPermissions('sysTenantAccount/get')")
    public Object get(Integer teacherId) {
        if (teacherId == null || teacherId <= 0) {
            return failed("用户id必须大于0");
        }

        return succeed(sysTenantAccountService.get(teacherId));
    }

    @ApiOperation(value = "获取账户明细")
    @GetMapping("/queryTenantAccountDetail")
    @PreAuthorize("@pcs.hasPermissions('sysTenantAccount/queryTenantAccountDetail')")
    public Object queryTenantAccountDetail(TenantAccountDetailQueryInfo queryInfo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        if (queryInfo.getUserId() == null || queryInfo.getUserId() <= 0) {
            return failed("用户id不能为空");
        }

        return succeed(sysTenantAccountDetailService.getPageList(queryInfo));
    }

    @ApiOperation(value = "增加分钟数")
    @PostMapping("/addMinutes")
    @PreAuthorize("@pcs.hasPermissions('sysTenantAccount/addMinutes')")
    public Object addMinutes(Integer teacherId, Integer minutes, String memo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        if (minutes == null || minutes <= 0) {
            return failed("充值分钟数必须大于0");
        }
        if (memo == null || memo.isEmpty()) {
            return failed("备注信息不能为空");
        }
        boolean updateRes = sysTenantAccountService.manualUpdate(teacherId, minutes, SysTenantAccountDetail.TransType.MANUAL_ADD, sysUser.getId(), memo);
        if (!updateRes) {
            return failed("充值失败，请重试");
        }
        return succeed("充值成功");
    }

    @ApiOperation(value = "减分钟数")
    @PostMapping("/subtractMinutes")
    @PreAuthorize("@pcs.hasPermissions('sysTenantAccount/subtractMinutes')")
    public Object subtractMinutes(Integer teacherId, Integer minutes, String memo) {
        SysUser sysUser = sysUserFeignService.queryUserInfo();
        if (sysUser == null) {
            return failed(HttpStatus.FORBIDDEN, "请登录");
        }
        if (minutes == null || minutes <= 0) {
            return failed("扣除分钟数必须大于0");
        }
        if (memo == null || memo.isEmpty()) {
            return failed("备注信息不能为空");
        }
        boolean updateRes = sysTenantAccountService.manualUpdate(teacherId, -minutes, SysTenantAccountDetail.TransType.MANUAL_SUB, sysUser.getId(), memo);
        if (!updateRes) {
            return failed("充值失败，请重试");
        }
        return succeed("扣除成功");
    }

}
