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

import com.alibaba.fastjson2.JSONObject;
import com.google.gson.Gson;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ResponseEnum;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.multishop.dto.ShopCompanyAuditingDTO;
import com.tmerclub.cloud.multishop.service.CompanyAuditingService;
import com.tmerclub.cloud.multishop.service.ShopAllinpayService;
import com.tmerclub.cloud.multishop.vo.ShopCompanyAuditingVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @author chiley
 * @date 2022/9/14 14:32
 */
@RestController("platformCompanyAuditingController")
@RequestMapping("/p/shopCompanyAuditing")
@Tag(name = "platform-审核店铺工商信息")
@Slf4j
public class CompanyAuditingController {

    @Autowired
    private CompanyAuditingService companyAuditingService;
    @Autowired
    private ShopAllinpayService shopAllinpayService;

    @GetMapping("/page")
    @Operation(summary = "分页获取待审核的工商信息", description = "分页获取待审核的工商信息")
    public ServerResponseEntity<PageVO<ShopCompanyAuditingVO>> getCompanyAuditingPage(PageDTO pageDTO, ShopCompanyAuditingDTO shopCompanyAuditingDTO) {
        log.debug("AuthUserContext:{}", new Gson().toJson(AuthUserContext.get()));
        if (!Objects.equals(Constant.PLATFORM_SHOP_ID, AuthUserContext.get().getTenantId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        return ServerResponseEntity.success(companyAuditingService.page(pageDTO, shopCompanyAuditingDTO));

    }

    @PutMapping("/audit")
    @Operation(summary = "审核签约信息", description = "审核签约信息")
    public ServerResponseEntity<Void> audit(@Valid @RequestBody ShopCompanyAuditingDTO shopCompanyAuditingDTO) {
        if (shopAllinpayService.getIsAllinpay()) {
            throw new LuckException("开启通联后，平台无需审核商家工商信息");
        }
        if (!Objects.equals(Constant.PLATFORM_SHOP_ID, AuthUserContext.get().getTenantId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
        shopCompanyAuditingDTO.setAuditorId(AuthUserContext.get().getUserId());
        companyAuditingService.audit(shopCompanyAuditingDTO);
        return ServerResponseEntity.success();
    }
}
