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

import com.tmerclub.cloud.api.supplier.vo.SupplierApiDetailVO;
import com.tmerclub.cloud.api.supplier.vo.SupplierBankCardVO;
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.supplier.constant.AuditStatus;
import com.tmerclub.cloud.supplier.dto.AllinpaySupplierBankCardDTO;
import com.tmerclub.cloud.supplier.service.SupplierAllinpayService;
import com.tmerclub.cloud.supplier.service.SupplierBankCardService;
import com.tmerclub.cloud.supplier.service.SupplierCompanyService;
import com.tmerclub.cloud.supplier.service.SupplierDetailService;
import com.tmerclub.cloud.supplier.vo.SupplierCompanyVO;
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/supplier_bank_card")
@Tag(name = "platform-供应商银行卡信息")
public class SupplierBankCardController {

    @Autowired
    private SupplierBankCardService supplierBankCardService;
    @Autowired
    private SupplierCompanyService supplierCompanyService;
    @Autowired
    private SupplierDetailService supplierDetailService;
    @Autowired
    private SupplierAllinpayService supplierAllinpayService;

    @GetMapping("/list_by_supplier_id")
    @Operation(summary = "根据供应商id批量获取银行卡信息")
    public ServerResponseEntity<List<SupplierBankCardVO>> listBySupplierId(@RequestParam("supplierId") Long supplierId) {
        List<SupplierBankCardVO> supplierBankCardVOList = supplierBankCardService.listBySupplierId(supplierId);
        return ServerResponseEntity.success(supplierBankCardVOList);
    }

    @PostMapping("/allinpay_save_and_apply_supplier")
    @Operation(summary = "通联支付--批量保存供应商银行卡信息并提交供应商审核信息", description = "批量保存供应商银行卡信息并提交供应商审核信息")
    public ServerResponseEntity<Void> allinpaySaveAndApplySupplier(@Valid @RequestBody AllinpaySupplierBankCardDTO allinpaySupplierBankCardDTO) {
        Long supplierId = allinpaySupplierBankCardDTO.getSupplierId();
        if (Objects.isNull(supplierId)) {
            throw new LuckException("供应商id不能为空");
        }
        // 查找工商信息
        SupplierCompanyVO supplierCompanyVO = supplierCompanyService.getSupplierCompanyBySupplierIdAndStatus(supplierId, AuditStatus.WAITAUDIT.value());
        if (Objects.isNull(supplierCompanyVO)) {
            throw new LuckException("该供应商工商信息为空，请刷新页面重新填写");
        }
        checkAllinpay(supplierCompanyVO);
        SupplierApiDetailVO supplierApiDetailVO = supplierDetailService.getBySupplierId(supplierId);
        if (Objects.equals(supplierApiDetailVO.getCompanyInfoProcessStatus(), CompanyInfoProcessStatus.SUCCESS.value())) {
            // 已经审核成功就不必要多次设置企业信息了
            throw new LuckException("该供应商已经审核通过");
        }
        supplierBankCardService.insertAndSetCompanyInfo(allinpaySupplierBankCardDTO, supplierId, supplierCompanyVO);
        supplierDetailService.removeCacheBySupplierId(supplierId);
        return ServerResponseEntity.success();
    }

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

}
