package com.yonge.cooleshow.teacher.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yonge.cooleshow.auth.api.client.SysUserFeignService;
import com.yonge.cooleshow.auth.api.entity.SysUser;
import com.yonge.cooleshow.biz.dal.dto.UserBankCardDto;
import com.yonge.cooleshow.biz.dal.dto.search.UserBankCardSearch;
import com.yonge.cooleshow.biz.dal.entity.SysConfig;
import com.yonge.cooleshow.biz.dal.entity.UserBankCard;
import com.yonge.cooleshow.biz.dal.service.SysConfigService;
import com.yonge.cooleshow.biz.dal.service.UserBankCardService;
import com.yonge.cooleshow.biz.dal.support.PageUtil;
import com.yonge.cooleshow.biz.dal.vo.UserBankCardVo;
import com.yonge.cooleshow.common.controller.BaseController;
import com.yonge.cooleshow.common.entity.HttpResponseResult;
import com.yonge.toolset.mybatis.page.PageInfo;
import com.yonge.toolset.utils.string.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/userBankCard")
@Api(value = "用户绑卡记录表", tags = "用户绑卡记录表")
public class UserBankCardController extends BaseController {
    @Autowired
    private UserBankCardService userBankCardService;
	@Autowired
	private SysUserFeignService sysUserFeignService;
	@Autowired
	private SysConfigService sysConfigService;

	@PostMapping("/bankList")
	@ApiOperation(value = "查询银行")
	public HttpResponseResult<List<SysConfig>> bankList() {
		Map<String,Object> params = new HashMap<>();
		params.put("group", "BANK");
		List<SysConfig> configs = sysConfigService.findAll(params);
		return succeed(configs);
	}

	/**
	 * 查询分页
	 */
	@PostMapping("/page")
	@ApiOperation(value = "查询分页", notes = "传入userBankCardSearch")
	public HttpResponseResult<PageInfo<UserBankCardVo>> page(@RequestBody UserBankCardSearch query) {
		SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null || null == user.getId()) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		query.setUserId(user.getId());

		IPage<UserBankCardVo> pages = userBankCardService.selectPage(PageUtil.getPage(query), query);
		return succeed(PageUtil.pageInfo(pages));
	}

	@PostMapping("/bankCard")
	@ApiOperation(value = "用户绑卡")
	public HttpResponseResult<UserBankCard> bankCard(@Valid @RequestBody UserBankCardDto bankCard) {
		SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null || null == user.getId()) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		bankCard.setUserId(user.getId());
		return userBankCardService.bankCard(bankCard);
	}

	@PostMapping("/unBind")
	@ApiOperation(value = "用户解绑银行卡")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "银行卡id", paramType = "query", dataType = "long"),
			@ApiImplicitParam(name = "name", value = "真实姓名", paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "idCardNo", value = "身份证号码", paramType = "query", dataType = "String"),
	})
	public HttpResponseResult<UserBankCard> unBind(@ApiIgnore @RequestBody UserBankCardDto bankCard) {
		if(null == bankCard.getId() || StringUtil.isEmpty(bankCard.getIdCardNo()) || StringUtil.isEmpty(bankCard.getName())){
			return HttpResponseResult.failed("缺少参数");
		}
		SysUser user = sysUserFeignService.queryUserInfo();
		if (user == null || null == user.getId()) {
			return failed(HttpStatus.FORBIDDEN, "请登录");
		}
		bankCard.setUserId(user.getId());
		return userBankCardService.unBind(bankCard);
	}

}