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

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.common.security.AuthUserContext;
import com.tmerclub.cloud.supplier.constant.AuditStatus;
import com.tmerclub.cloud.supplier.dto.AllinpaySupplierBankCardDTO;
import com.tmerclub.cloud.supplier.dto.SupplierBankCardDTO;
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
 */
@RestController("supplierSupplierBankCardController")
@RequestMapping("/s/apply_supplier/supplier_bank_card")
@Tag(name = "supplier-供应商银行卡信息")
public class SupplierBankCardController {

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

    @PostMapping("/save_and_apply_supplier")
    @Operation(summary = "批量保存供应商银行卡信息并提交供应商审核信息", description = "批量保存供应商银行卡信息并提交供应商审核信息")
    public ServerResponseEntity<Void> saveAndApplySupplier(@Valid @RequestBody List<SupplierBankCardDTO> supplierBankCardDTOList) {
        Long supplierId = AuthUserContext.get().getTenantId();
        supplierBankCardService.insertBatchAndSubmitApply(supplierBankCardDTOList, supplierId);
        return ServerResponseEntity.success();
    }

    @PostMapping("/allinpay_save_and_apply_supplier")
    @Operation(summary = "通联支付--批量保存供应商银行卡信息并提交供应商审核信息", description = "批量保存供应商银行卡信息并提交供应商审核信息")
    public ServerResponseEntity<Void> allinpaySaveAndApplySupplier(@Valid @RequestBody AllinpaySupplierBankCardDTO allinpaySupplierBankCardDTO) {
        Long supplierId = AuthUserContext.get().getTenantId();
        // 查找工商信息
        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("工商信息需要提交的内容有所变更，请重新填写");
        }
    }

    @GetMapping
    @Operation(summary = "根据银行卡id获取银行卡信息", description = "根据银行卡id获取银行卡信息")
    public ServerResponseEntity<SupplierBankCardVO> getById(@RequestParam("supplierBankCardId") Long supplierBankCardId) {
        Long supplierId = AuthUserContext.get().getTenantId();
        SupplierBankCardVO supplierBankCardVO = supplierBankCardService.getBySupplierBankCardIdAndSupplierId(supplierBankCardId, supplierId);
        return ServerResponseEntity.success(supplierBankCardVO);
    }

    @GetMapping("/list_by_supplierId")
    @Operation(summary = "获取供应商下的银行卡列表", description = "获取供应商下的银行卡列表")
    public ServerResponseEntity<List<SupplierBankCardVO>> listBySupplierId() {
        Long supplierId = AuthUserContext.get().getTenantId();
        List<SupplierBankCardVO> supplierBankCardVOList = supplierBankCardService.listBySupplierId(supplierId);
        return ServerResponseEntity.success(supplierBankCardVOList);
    }

    @PostMapping
    @Operation(summary = "添加单个银行卡信息", description = "添加单个银行卡信息")
    public ServerResponseEntity<Void> insert(@RequestBody @Valid SupplierBankCardDTO supplierBankCardDTO) {
        if (supplierAllinpayService.getIsAllinpay()) {
            throw new LuckException("当前不支持该接口添加银行卡");
        }
        Long supplierId = AuthUserContext.get().getTenantId();
        supplierBankCardService.insertBySupplierId(supplierBankCardDTO, supplierId);
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "根据银行卡id删除银行卡信息", description = "根据银行卡id删除银行卡信息")
    public ServerResponseEntity<Void> deleteById(@RequestParam("supplierBankCardId") Long supplierBankCardId) {
        if (supplierAllinpayService.getIsAllinpay()) {
            throw new LuckException("当前不支持该接口删除银行卡");
        }
        supplierBankCardService.deleteBySupplierBankCardId(supplierBankCardId, AuthUserContext.get().getTenantId());
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新银行卡信息", description = "更新银行卡信息")
    public ServerResponseEntity<Void> update(@RequestBody @Valid SupplierBankCardDTO supplierBankCardDTO) {
        if (supplierAllinpayService.getIsAllinpay()) {
            throw new LuckException("当前不支持该接口更新银行卡");
        }
        supplierBankCardService.updateBySupplierId(supplierBankCardDTO, AuthUserContext.get().getTenantId());
        return ServerResponseEntity.success();
    }

    @PutMapping("/setPrimary")
    @Operation(summary = "设置为主账号", description = "设置为主账号")
    public ServerResponseEntity<Void> setPrimary(@RequestParam("supplierBankCardId") Long supplierBankCardId) {
        supplierBankCardService.setPrimaryBySupplierId(supplierBankCardId, AuthUserContext.get().getTenantId());
        return ServerResponseEntity.success();
    }

    @PutMapping("/cancelPrimary")
    @Operation(summary = "取消主账号", description = "取消主账号")
    public ServerResponseEntity<Void> cancelPrimary(@RequestParam("supplierBankCardId") Long supplierBankCardId) {
        supplierBankCardService.setNotPrimaryBySupplierId(supplierBankCardId, AuthUserContext.get().getTenantId());
        return ServerResponseEntity.success();
    }

    @PostMapping("/refresh")
    @Operation(summary = "刷新银行卡列表", description = "通联独有，防止有银行卡没有同步到数据库")
    public ServerResponseEntity<Void> refresh() {
        Long supplierId = AuthUserContext.get().getTenantId();
        supplierBankCardService.refresh(supplierId);
        return ServerResponseEntity.success();
    }
}
