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

import com.tmerclub.cloud.api.auth.feign.AccountFeignClient;
import com.tmerclub.cloud.api.auth.vo.AuthAccountVO;
import com.tmerclub.cloud.api.multishop.constant.ShopStatus;
import com.tmerclub.cloud.api.multishop.vo.ShopDetailVO;
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.multishop.constant.AuditStatus;
import com.tmerclub.cloud.multishop.service.ShopAllinpayService;
import com.tmerclub.cloud.multishop.service.ShopAuditingService;
import com.tmerclub.cloud.multishop.service.ShopDetailService;
import com.tmerclub.cloud.multishop.vo.ShopAuditingVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
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("multishopShopApplyController")
@RequestMapping("/m/applyShop/shopApply")
@Tag(name = "multishop-店铺申请")
public class ShopApplyController {

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

    @Autowired
    private ShopDetailService shopDetailService;

    @Autowired
    private ShopAuditingService shopAuditingService;

    @DubboReference
    private AccountFeignClient accountFeignClient;

    @Autowired
    private ShopAllinpayService shopAllinpayService;

    @GetMapping("/getAuditingInfo")
    @Operation(summary = "获取店铺审核状态信息", description = "获取店铺审核状态信息,返回值为空代表该店铺没有提交过审核信息")
    public ServerResponseEntity<ShopAuditingVO> getAuditingInfo() {
        Long shopId = AuthUserContext.get().getTenantId();
        Integer sysType = AuthUserContext.get().getSysType();
        if (Objects.equals(shopId, Constant.DEFAULT_SHOP_ID)) {
            LOGGER.info("获取店铺审核状态信息,该店铺没有提交过审核信息");
            return ServerResponseEntity.success(null);
        }
        ShopAuditingVO shopAuditingVO = shopAuditingService.getByshopId(shopId);
        if (Objects.isNull(shopAuditingVO)) {
            return ServerResponseEntity.success(null);
        }
        ServerResponseEntity<AuthAccountVO> responseEntity = accountFeignClient.getAccountInfoByTenantId(shopId, sysType);
        ShopDetailVO shopDetailVO = shopDetailService.getByShopId(shopId);
        if (Objects.equals(shopDetailVO.getShopStatus(), ShopStatus.OFFLINE.value())) {
            shopAuditingVO.setStatus(AuditStatus.OFFLINE.value());
        } else if (Objects.equals(shopDetailVO.getShopStatus(), ShopStatus.OFFLINE_AWAIT_AUDIT.value()) || Objects.equals(shopDetailVO.getShopStatus(), ShopStatus.OPEN_AWAIT_AUDIT.value())) {
            if (!shopAllinpayService.getIsAllinpay()) {
                shopAuditingVO.setStatus(AuditStatus.WAITAUDIT.value());
            }
        }
        if (responseEntity.isSuccess() && Objects.nonNull(responseEntity.getData())) {
            AuthAccountVO authAccountVO = responseEntity.getData();
            shopDetailVO.setMerchantAccount(authAccountVO.getPhone());
            shopDetailVO.setAccountStatus(authAccountVO.getStatus());
        }
        shopAuditingVO.setShopDetail(shopDetailVO);
        shopAuditingVO.setUserId(null);
        shopAuditingVO.setShopId(null);
        shopAuditingVO.setAuditorId(null);
        return ServerResponseEntity.success(shopAuditingVO);
    }

}
