package com.czg.controller.admin;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.czg.annotation.SaAdminCheckPermission;
import com.czg.market.dto.MkDistributionConfigDTO;
import com.czg.market.service.*;
import com.czg.market.vo.MkDistributionConfigVO;
import com.czg.order.dto.MkDistributionPayDTO;
import com.czg.resp.CzgResult;
import com.czg.sa.StpKit;
import com.czg.utils.AssertUtil;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Map;

/**
 *
 * 分销相关
 * @author Administrator
 */
@RestController
@RequestMapping("/admin/distribution")
public class DistributionController {
    @Resource
    private MkDistributionConfigService configService;
    @Resource
    private MkDistributionUserService distributionUserService;
    @Resource
    private MkDistributionAmountFlowService distributionAmountFlowService;
    @Resource
    private MkDistributionFlowService distributionFlowService;
    @Resource
    private MkDistributionWithdrawFlowService withdrawFlowService;

    /**
     * 配置信息详情
     */
    @SaAdminCheckPermission(parentName = "分销相关",value = "distribution:detail", name = "分销配置")
    @GetMapping
    public CzgResult<MkDistributionConfigVO> detail() {
        return CzgResult.success(configService.detail(StpKit.USER.getShopId()));
    }

    /**
     * 配置信息修改
     *
     * @return 是否成功
     */
    @SaAdminCheckPermission(parentName = "分销相关",value = "distribution:edit", name = "分销修改")
    @PutMapping
    public CzgResult<Boolean> edit( @RequestBody MkDistributionConfigDTO dto) {
        return CzgResult.success(configService.edit(StpKit.USER.getShopId(), dto));
    }


    /**
     * 现金充值
     * @param payParam 充值信息
     * @return 是否成功
     */
    @PostMapping("/cashPay")
    public CzgResult<Boolean> cashPayOrder(@Validated @RequestBody MkDistributionPayDTO payParam) {
        AssertUtil.isNull(payParam.getShopId(), "店铺id不能为空");
        AssertUtil.isNull(payParam.getAmount(), "充值金额不能为空");
        AssertUtil.isTrue(payParam.getAmount().compareTo(BigDecimal.ZERO) == 0, "金额不为0");
        return CzgResult.success(distributionUserService.cashPayOrder(StpKit.USER.getLoginIdAsLong(), payParam));
    }

    /**
     * 金额记录
     * @param type manual_recharge充值 self_recharge自助充值 refund退款 manual_sub手动扣减 sub统扣减
     * @param key 搜索
     */
    @GetMapping("/flow")
    public CzgResult<Map<String, Object>> flow(@RequestParam(required = false) Long shopId, @RequestParam(required = false) String type,
                                               @RequestParam(required = false) String key,@RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime) {
        return CzgResult.success(distributionAmountFlowService.pageInfo(StpKit.USER.isAdmin() ? shopId : StpKit.USER.getShopId(), type, key,
                StrUtil.isBlank(startTime) ? null : DateUtil.parseLocalDateTime(startTime), StrUtil.isBlank(endTime) ? null : DateUtil.parseLocalDateTime(endTime)));
    }

    /**
     * 开通记录
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param key 用户id昵称
     */
    @GetMapping("openFlow")
    public CzgResult<Map<String, Object>> openFlow(@RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime, @RequestParam(required = false) String key) {
        return CzgResult.success(distributionAmountFlowService.openPageInfo(StpKit.USER.getShopId(),
                StrUtil.isNotBlank(startTime) ? DateUtil.parseLocalDateTime(startTime) : null, StrUtil.isNotBlank(endTime) ? DateUtil.parseLocalDateTime(endTime) : null, key));
    }


    /**
     * 分销明细
     * @param startTime 开始时间
     * @param endTime 结束时间
     * @param key 用户id昵称
     * @param status  pending待入账 success已入账
     */
    @GetMapping("distributionFlow")
    public CzgResult<Map<String, Object>> distributionFlow(@RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime,
                                                           @RequestParam(required = false) String key, @RequestParam(required = false) String status,
                                                           @RequestParam(required = false) Long id) {
        return CzgResult.success(distributionFlowService.pageInfo(StpKit.USER.getShopId(),
                StrUtil.isNotBlank(startTime) ? DateUtil.parseLocalDateTime(startTime) : null, StrUtil.isNotBlank(endTime) ? DateUtil.parseLocalDateTime(endTime) : null, key, status, id));
    }


    /**
     * 用户提现列表
     * @param userId 用户id
     * @param key 搜索
     * @param startTime 开始时间
     * @param endTime 结束时间
     */
    @GetMapping("/withdrawFlow")
    public CzgResult<Map<String, Object>> withdrawPageInfo(@RequestParam(required = false) Long userId, @RequestParam(required = false) String key,
                                                           @RequestParam(required = false) String startTime, @RequestParam(required = false) String endTime) {
        return CzgResult.success(withdrawFlowService.withdrawPageInfo(userId, StrUtil.isBlank(startTime) ? null : DateUtil.parseLocalDateTime(startTime),
                StrUtil.isBlank(endTime) ? null : DateUtil.parseLocalDateTime(endTime), key));
    }


    /**
     * 充值二维码获取
     * @param amount 金额
     * @return base64
     */
    @GetMapping("/rechargeQrCode")
    public CzgResult<String> rechargeQrCode(@RequestParam BigDecimal amount) {
        return CzgResult.success(configService.rechargeQrCode(StpKit.USER.getShopId(), amount));
    }





}
