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.service.SysUserCashAccountService;
import com.ym.mec.common.controller.BaseController;
import com.ym.mec.common.page.QueryInfo;
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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

import static com.ym.mec.biz.dal.enums.PlatformCashAccountDetailTypeEnum.FILL_ACCOUNT;

@RequestMapping("userCashAccount")
@Api(tags = "用户账户服务")
@RestController
public class SysUserCashAccountController extends BaseController {

	@Autowired
	private SysUserCashAccountService sysUserCashAccountService;
	@Autowired
	private SysUserFeignService sysUserFeignService;

	@ApiOperation(value = "获取用户账户信息")
	@GetMapping("/get")
	@PreAuthorize("@pcs.hasPermissions('userCashAccount/get')")
	public Object add() {
		SysUser sysUser = sysUserFeignService.queryUserInfo();
		if (sysUser == null) {
			return failed("请重新登录");
		}
		return succeed(sysUserCashAccountService.get(sysUser.getId()));
	}

	@ApiOperation(value = "分页查询收费类型列表")
	@GetMapping("/queryPage")
	@PreAuthorize("@pcs.hasPermissions('userCashAccount/queryPage')")
	public Object queryPage(QueryInfo queryInfo) {
		return succeed(sysUserCashAccountService.queryPage(queryInfo));
	}

	@ApiOperation(value = "将课程余额转入到可用余额")
	@PostMapping("/transferCourseBalanceToBalance")
	@PreAuthorize("@pcs.hasPermissions('userCashAccount/transferCourseBalanceToBalance')")
	public Object transferCourseBalanceToBalance(Integer userId,BigDecimal money) {
		sysUserCashAccountService.transferCourseBalanceToBalance(userId,money);
		return succeed();
	}

	@ApiOperation(value = "更新指定用户的账户课程余额")
	@PostMapping("/updateCourseBalance")
	@PreAuthorize("@pcs.hasPermissions('userCashAccount/updateCourseBalance')")
	public Object updateCourseBalance(Integer userId, BigDecimal decimal, String memo) {
		sysUserCashAccountService.updateCourseBalance(userId,decimal,FILL_ACCOUNT,decimal, memo);
		return succeed();
	}

	@ApiOperation(value = "更新指定用户的账户可用余额")
	@PostMapping("/updateBalance")
	@PreAuthorize("@pcs.hasPermissions('userCashAccount/updateBalance')")
	public Object updateBalance(Integer userId, BigDecimal decimal, String memo) {
		sysUserCashAccountService.updateBalance(userId,decimal,FILL_ACCOUNT, memo);
		return succeed();
	}
}
