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

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.common.security.AuthUserContext;
import com.tmerclub.cloud.multishop.constant.AuditStatus;
import com.tmerclub.cloud.multishop.dto.AllinpayShopBankCardDTO;
import com.tmerclub.cloud.multishop.dto.ShopBankCardDTO;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author lth
 */
@RestController("multishopShopBankCardController")
@RequestMapping("/m/applyShop/shopBankCard")
@Tag(name = "multishop-店铺银行卡信息")
public class ShopBankCardController {

    private final Logger LOGGER = LoggerFactory.getLogger(ShopBankCardController.class);

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

    @PostMapping("/saveAndApplyShop")
    @Operation(summary = "批量保存店铺银行卡信息并提交店铺审核信息", description = "批量保存店铺银行卡信息并提交店铺审核信息")
    public ServerResponseEntity<Void> saveAndApplyShop(@Valid @RequestBody List<ShopBankCardDTO> shopBankCardDTOList) {
        Long shopId = AuthUserContext.get().getTenantId();
        if (shopAllinpayService.getIsAllinpay()) {
            LOGGER.info("通联支付--批量保存店铺银行卡信息并提交店铺审核信息");
            throw new LuckException("开店所需要提交的信息有所变更，请重新填写");
        }
        shopBankCardService.insertBatchAndSubmitApply(shopBankCardDTOList, shopId);
        return ServerResponseEntity.success();
    }

    @PostMapping("/allinpay_save_and_apply_shop")
    @Operation(summary = "通联支付--批量保存店铺银行卡信息并提交店铺审核信息", description = "批量保存店铺银行卡信息并提交店铺审核信息")
    public ServerResponseEntity<Void> allinpaySaveAndApplyShop(@Valid @RequestBody AllinpayShopBankCardDTO allinpayShopBankCardDTO) {
        Long shopId = AuthUserContext.get().getTenantId();
        // 查找工商信息
        ShopCompanyVO shopCompany = shopCompanyService.getShopCompanyByShopIdAndStatus(shopId, AuditStatus.WAITAUDIT.value());
        if (Objects.isNull(shopCompany)) {
            LOGGER.info("店铺工商信息为空，请刷新页面重新填写");
            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("工商信息需要提交的内容有所变更，请重新填写");
        }
    }

    @GetMapping
    @Operation(summary = "根据银行卡id获取银行卡信息", description = "根据银行卡id获取银行卡信息")
    public ServerResponseEntity<ShopBankCardVO> getById(@RequestParam("shopBankCardId") Long shopBankCardId) {
        Long shopId = AuthUserContext.get().getTenantId();
        ShopBankCardVO shopBankCardVO = shopBankCardService.getByShopBankCardIdAndShopId(shopBankCardId, shopId);
        return ServerResponseEntity.success(shopBankCardVO);
    }

    @GetMapping("/getShopIdList")
    @Operation(summary = "获取店铺下的银行卡列表", description = "获取店铺下的银行卡列表")
    public ServerResponseEntity<List<ShopBankCardVO>> getShopIdList() {
        Long shopId = AuthUserContext.get().getTenantId();
        List<ShopBankCardVO> shopBankCardVOList = shopBankCardService.listByShopId(shopId);
        return ServerResponseEntity.success(shopBankCardVOList);
    }

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

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

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

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

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

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