
package com.tmerclub.cloud.multishop.controller.platform;

import com.tmerclub.cloud.api.multishop.vo.ShopBankCardVO;
import com.tmerclub.cloud.api.multishop.vo.ShopDetailVO;
import com.tmerclub.cloud.common.constant.CompanyInfoProcessStatus;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.multishop.constant.AuditStatus;
import com.tmerclub.cloud.multishop.dto.AllinpayShopBankCardDTO;
import com.tmerclub.cloud.multishop.service.ShopAllinpayService;
import com.tmerclub.cloud.multishop.service.ShopBankCardService;
import com.tmerclub.cloud.multishop.service.ShopCompanyService;
import com.tmerclub.cloud.multishop.service.ShopDetailService;
import com.tmerclub.cloud.multishop.vo.ShopCompanyVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @Author lth
 * @Date 2021/4/26 10:09
 */
@RestController
@RequestMapping("/p/shopBankCard")
@Tag(name = "platform-店铺银行卡信息")
public class ShopBankCardController {

    @Autowired
    private ShopBankCardService shopBankCardService;
    @Autowired
    private ShopCompanyService shopCompanyService;
    @Autowired
    private ShopDetailService shopDetailService;
    @Autowired
    private ShopAllinpayService shopAllinpayService;

    @GetMapping("/list_by_shop_id")
    @Operation(summary = "根据店铺id批量获取银行卡信息")
    public ServerResponseEntity<List<ShopBankCardVO>> listByShopId(@RequestParam("shopId") Long shopId) {
        List<ShopBankCardVO> shopBankCardVOList = shopBankCardService.listByShopId(shopId);
        return ServerResponseEntity.success(shopBankCardVOList);
    }

    @PostMapping("/allinpaySave")
    @Operation(summary = "通联支付--批量保存店铺银行卡信息并提交店铺审核信息", description = "批量保存店铺银行卡信息并提交店铺审核信息")
    public ServerResponseEntity<Void> allinpaySave(@Valid @RequestBody AllinpayShopBankCardDTO allinpayShopBankCardDTO) {
        Long shopId = allinpayShopBankCardDTO.getShopId();
        if (Objects.isNull(shopId)) {
            throw new LuckException("店铺id不能为空");
        }
        // 查找工商信息
        ShopCompanyVO shopCompany = shopCompanyService.getShopCompanyByShopIdAndStatus(shopId, AuditStatus.WAITAUDIT.value());
        if (Objects.isNull(shopCompany)) {
            throw new LuckException("该店铺工商信息为空，请刷新页面重新填写");
        }
        checkAllinpay(shopCompany);
        ShopDetailVO shopDetailVO = shopDetailService.getByShopId(shopId);
        if (Objects.equals(shopDetailVO.getCompanyInfoProcessStatus(), CompanyInfoProcessStatus.SUCCESS.value())) {
            // 已经审核成功就不必要多次设置企业信息了
            throw new LuckException("该店铺已经审核通过");
        }

        shopBankCardService.insertAndSetCompanyInfo(allinpayShopBankCardDTO, shopId, shopCompany);
        shopDetailService.removeCacheByShopId(shopId);
        return ServerResponseEntity.success();
    }

    private void checkAllinpay(ShopCompanyVO shopCompany) {
        boolean needInfo = Objects.isNull(shopCompany.getLegalIds()) || Objects.isNull(shopCompany.getLegalPhone());
        if (shopAllinpayService.getIsAllinpay() && needInfo) {
            throw new LuckException("工商信息需要提交的内容有所变更，请重新填写");
        }
    }


}
