/*
 * Copyright (c) 2018-2999 广州亚米信息科技有限公司 All rights reserved.
 *
 * https://www.gz-yami.com/
 *
 */

package com.yami.shop.platform.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.enums.ShopWalletChangeReason;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.model.ShopWithdrawCash;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.common.util.Arith;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.comment.model.AppConnect;
import com.yami.shop.security.comment.service.AppConnectService;
import com.yami.shop.security.platform.util.SecurityUtils;
import com.yami.shop.service.ShopDetailService;
import com.yami.shop.service.ShopWithdrawCashService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import com.yami.shop.common.util.R;

/**
 * 商家提现申请信息管理实现
 *
 * @author LGH
 * @version 1.0.0
 * @since 2019-09-29
 */
@RestController
@AllArgsConstructor
@RequestMapping("/shop/shopWithdrawCash")
public class ShopWithdrawCashController {

    private final ShopWithdrawCashService shopWithdrawCashService;

    private final AppConnectService appConnectService;

    private final ShopDetailService shopDetailService;


    /**
     * 分页查询
     *
     * @param page             分页对象
     * @param shopWithdrawCash 商家提现申请信息
     * @return 分页数据
     */
    @GetMapping("/page")
    public R<IPage<ShopWithdrawCash>> getShopWithdrawCashPage(PageParam<ShopWithdrawCash> page, ShopWithdrawCash shopWithdrawCash) {
        return R.SUCCESS(shopWithdrawCashService.pageShopWithdrawCash(page, shopWithdrawCash));
    }


    /**
     * 通过id查询商家提现申请信息
     *
     * @param cashId id
     * @return 单个数据
     */
    @GetMapping("/info/{cashId}")
    public R<ShopWithdrawCash> getById(@PathVariable("cashId") Long cashId) {
        return R.SUCCESS(shopWithdrawCashService.getById(cashId));
    }


    /**
     * 审核信息
     */
    @SysLog("审核商家提现信息")
    @PutMapping("/audit")
    @PreAuthorize("@pms.hasPermission('shop:shopWithdrawCash:audit')")
    public R<Void> audit(@RequestBody ShopWithdrawCash shopWithdrawCash) {
        ShopWithdrawCash dbShopWithdrawCash = shopWithdrawCashService.getById(shopWithdrawCash.getCashId());
        if (dbShopWithdrawCash == null) {
            throw new GlobalException("未找到申请信息");
        }
        ShopDetail shopdetail = shopDetailService.getShopDetailByShopId(dbShopWithdrawCash.getShopId());
        if (shopdetail == null) {
            throw new GlobalException("未找到该店铺信息");
        }
        AppConnect appConnect = appConnectService.getOne(new LambdaQueryWrapper<AppConnect>()
                .eq(AppConnect::getAppId, 1)
                .eq(AppConnect::getUserId, shopdetail.getUserId()));
        if (appConnect == null) {
            throw new GlobalException("未找到该店铺身份信息");
        }
        shopWithdrawCash.setCreateTime(null);

        shopWithdrawCashService.auditWithdrawCash(shopWithdrawCash.getCashId(), shopWithdrawCash, SecurityUtils.getSysUser().getUserId(), appConnect.getBizUserId());
        // 减少冻结金额
        return R.SUCCESS();
    }
}
