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

package com.yami.shop.multishop.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yami.shop.bean.enums.AuditStatus;
import com.yami.shop.bean.enums.SmsType;
import com.yami.shop.bean.model.ShopBankCard;
import com.yami.shop.bean.model.ShopDetail;
import com.yami.shop.bean.model.ShopWithdrawCash;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.exception.GlobalException;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.security.multishop.util.SecurityUtils;
import com.yami.shop.service.ShopBankCardService;
import com.yami.shop.service.ShopDetailService;
import com.yami.shop.service.ShopWithdrawCashService;
import com.yami.shop.service.SmsLogService;
import lombok.AllArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Objects;


/**
 * 商家提现申请信息
 *
 * @author Dwl
 * @date 2019-09-19 14:22:08
 */
@RestController
@AllArgsConstructor
@RequestMapping("/shop/withdrawCash" )
public class ShopWithdrawCashController {

    private final ShopWithdrawCashService shopWithdrawCashService;
    private ShopDetailService shopDetailService;
    private SmsLogService smsLogService;


    @PostMapping("/apply")
    public ResponseEntity<Void> apply(@RequestBody ShopWithdrawCash shopWithdrawCash) {
        ShopDetail shopDetail = shopDetailService.getById(SecurityUtils.getShopUser().getShopId());
        if(Objects.equals(shopDetail.getShopId(), Constant.MAIN_SHOP)){
            throw  new YamiShopBindException("自营店无法提现");
        }
        if(shopWithdrawCash.getAmount() < 1){
            throw new GlobalException("提现金额不能少于1.00元");
        }
        if (!smsLogService.checkValidCode(shopDetail.getMobile(), shopWithdrawCash.getCode(), SmsType.VALID)){
            throw new GlobalException("验证码有误或已过期");
        }

        shopWithdrawCash.setUpdateTime(new Date());
        shopWithdrawCash.setCreateTime(new Date());
        shopWithdrawCash.setStatus(AuditStatus.WAITAUDIT.value());
        shopWithdrawCash.setShopId(SecurityUtils.getShopUser().getShopId());

        shopWithdrawCashService.withdrawCash(shopWithdrawCash);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/getShopInfo")
    public ResponseEntity<ShopWithdrawCash> getShopInfo() {
        Long shopId = SecurityUtils.getShopUser().getShopId();
        ShopWithdrawCash shopWithdrawCash = shopWithdrawCashService.getShopWithdrawCashByShopId(shopId);
        return ResponseEntity.ok(shopWithdrawCash);
    }
}
