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

import com.tmerclub.cloud.api.auth.feign.AccountFeignClient;
import com.tmerclub.cloud.api.auth.vo.AuthAccountVO;
import com.tmerclub.cloud.api.supplier.constant.SupplierStatus;
import com.tmerclub.cloud.api.supplier.vo.SupplierApiDetailVO;
import com.tmerclub.cloud.common.constant.Constant;
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.service.SupplierAllinpayService;
import com.tmerclub.cloud.supplier.service.SupplierAuditingService;
import com.tmerclub.cloud.supplier.service.SupplierDetailService;
import com.tmerclub.cloud.supplier.vo.SupplierAuditingVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Objects;

/**
 * @author person
 */
@RestController("supplierSupplierApplyController")
@RequestMapping("/s/apply_supplier/supplier_apply")
@Tag(name = "supplier-供应商申请")
public class SupplierApplyController {

    @Autowired
    private SupplierDetailService supplierDetailService;

    @Autowired
    private SupplierAuditingService supplierAuditingService;

    @DubboReference
    private AccountFeignClient accountFeignClient;

    @Autowired
    private SupplierAllinpayService supplierAllinpayService;

    @GetMapping("/get_auditing_info")
    @Operation(summary = "获取供应商审核状态信息", description = "获取供应商审核状态信息,返回值为空代表该供应商没有提交过审核信息")
    public ServerResponseEntity<SupplierAuditingVO> getAuditingInfo() {
        Long supplierId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        if (Objects.equals(supplierId, Constant.DEFAULT_SHOP_ID)) {
            return ServerResponseEntity.success(null);
        }
        SupplierAuditingVO supplierAuditingVO = supplierAuditingService.getBySupplierId(supplierId);
        if (Objects.isNull(supplierAuditingVO)) {
            return ServerResponseEntity.success(null);
        }
        ServerResponseEntity<AuthAccountVO> responseEntity = accountFeignClient.getAccountInfoByTenantId(supplierId, sysType);
        SupplierApiDetailVO supplierApiDetailVO = supplierDetailService.getBySupplierId(supplierId);
        if (Objects.equals(supplierApiDetailVO.getSupplierStatus(), SupplierStatus.OFFLINE.value())) {
            supplierAuditingVO.setStatus(AuditStatus.OFFLINE.value());
        } else if (Objects.equals(supplierApiDetailVO.getSupplierStatus(), SupplierStatus.OFFLINE_AWAIT_AUDIT.value()) || Objects.equals(supplierApiDetailVO.getSupplierStatus(), SupplierStatus.OPEN_AWAIT_AUDIT.value())) {
            if (!supplierAllinpayService.getIsAllinpay()) {
                supplierAuditingVO.setStatus(AuditStatus.WAITAUDIT.value());
            }
        }
        if (responseEntity.isSuccess() && Objects.nonNull(responseEntity.getData())) {
            AuthAccountVO authAccountVO = responseEntity.getData();
            supplierApiDetailVO.setSupplierAccount(authAccountVO.getPhone());
            supplierApiDetailVO.setAccountStatus(authAccountVO.getStatus());
        }
        supplierAuditingVO.setSupplierDetail(supplierApiDetailVO);
        supplierAuditingVO.setUserId(null);
        supplierAuditingVO.setSupplierId(null);
        supplierAuditingVO.setAuditorId(null);
        return ServerResponseEntity.success(supplierAuditingVO);
    }

}
