package com.cyksj.web.controller.cps;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.mapper.UserMapper;
import com.cyksj.mapper.manage.distribute.DistributeMoneyApplyMapper;
import com.cyksj.mapper.manage.distribute.DistributePointsExchangeMoneyMapper;
import com.cyksj.model.entity.DistributePointsExchangeMoney;
import com.cyksj.model.entity.User;
import com.cyksj.model.request.ApplyMoneyReq;
import com.cyksj.model.views.CpsApplyMoneyView;
import com.cyksj.model.views.CpsDistributeSubView;
import com.cyksj.model.views.UserDistributeSubDetailView;
import com.cyksj.model.views.UserSubApplyMoneyDetailView;
import com.cyksj.service.pay.TransferFuncService;
import com.cyksj.web.util.StpCpsManageUser;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapBuilder;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * cps后台/提现中心
 * 项目名: yhlxj
 * 文件名: CpsApplyMoneyController
 * 创建者: JavaZou
 * 创建时间:2023/10/31 15:26
 */
@RestController
@RequestMapping("/cps/manage/applyMoney")
@RequiredArgsConstructor
public class CpsApplyMoneyController {
	private final TransferFuncService transferFuncService;

	private final BeanSearcher beanSearcher;

	private final DistributeMoneyApplyMapper distributeMoneyApplyMapper;

	private final UserMapper userMapper;

	private final DistributePointsExchangeMoneyMapper distributePointsExchangeMoneyMapper;

	private final HttpServletRequest request;

	/**
	 * cps用户提现
	 */
	@PostMapping
	public Result<String> applyMoney(@RequestBody ApplyMoneyReq applyMoneyReq) throws Exception {
		long cpsUserId = StpCpsManageUser.getLoginIdAsLong();
		applyMoneyReq.setUserId(cpsUserId);
		return transferFuncService.distributeApply(applyMoneyReq);
	}

	/**
	 * 提现列表
	 */
	@GetMapping("/get")
	public Result<SearchResult<CpsApplyMoneyView>> getApplyMoneyList() {
		long cpsUserId = StpCpsManageUser.getLoginIdAsLong();
		SearchResult<CpsApplyMoneyView> search = beanSearcher.search(CpsApplyMoneyView.class,
				MapUtils.flatBuilder(request.getParameterMap())
						.field(CpsApplyMoneyView::getUserId, cpsUserId)
						.build());
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 渠道关于下级渠道提现详情
	 */
	@GetMapping("/get/sub/applyMoney/detail")
	public Result<UserSubApplyMoneyDetailView> getSubApplyMoneyDetail() {
		long cpsUserId = StpCpsManageUser.getLoginIdAsLong();
		UserSubApplyMoneyDetailView userSubApplyMoneyDetailView = new UserSubApplyMoneyDetailView();
		BigDecimal gotMoney = distributeMoneyApplyMapper.getSubGotAppyMoney(cpsUserId);
		userSubApplyMoneyDetailView.setGotMoney(gotMoney);
		User user = userMapper.selectById(cpsUserId);
		userSubApplyMoneyDetailView.setCanApplyMoney(user.getSubApplyMoney());
		return GatewayResponse.SUCCESS.newBuilder().toResult(userSubApplyMoneyDetailView);
	}

	/**
	 * 下级渠道数据
	 */
	@GetMapping("/get/sub")
	public Result<SearchResult<CpsDistributeSubView>> getSubDistributeData() {
		long cpsUserId = StpCpsManageUser.getLoginIdAsLong();
		MapBuilder builder = MapUtils.flatBuilder(request.getParameterMap());
		builder.put("uid", String.format("user_id = %s", cpsUserId));
		SearchResult<CpsDistributeSubView> search = beanSearcher.search(CpsDistributeSubView.class, builder.build());
		search.getDataList().forEach(data -> {
			data.setDetailView(beanSearcher.searchFirst(UserDistributeSubDetailView.class, MapUtils.builder().put("sid", data.getSubUserId()).build()));
		});
		return GatewayResponse.SUCCESS.newBuilder().toResult(search);
	}

	/**
	 * 下级渠道奖金提现
	 */
	@PostMapping("/sub")
	public Result<String> subApplyMoney(@RequestBody ApplyMoneyReq applyMoneyReq) throws Exception {
		long cpsUserId = StpCpsManageUser.getLoginIdAsLong();
		applyMoneyReq.setUserId(cpsUserId);
		return transferFuncService.subApplyMoney(applyMoneyReq);
	}

	/**
	 * 提现门槛
	 */
	@GetMapping("/applyMoney/threshold")
	public Result<DistributePointsExchangeMoney> getThreshold() {
		return GatewayResponse.SUCCESS.newBuilder().toResult(distributePointsExchangeMoneyMapper.selectOne(Wrappers.lambdaQuery(DistributePointsExchangeMoney.class).last("limit 1")));
	}
}
