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

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.supplier.dto.SupplierCompanyAuditingDTO;
import com.tmerclub.cloud.supplier.service.SupplierAllinpayService;
import com.tmerclub.cloud.supplier.service.SupplierCompanyAuditingService;
import com.tmerclub.cloud.supplier.vo.SupplierCompanyAuditingVO;
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.Objects;

/**
 * @author chiley
 * @date 2022/9/14 14:32
 */
@RestController("companyAuditingController")
@RequestMapping("/p/supplier_company_auditing")
@Tag(name = "platform-审核供应商工商信息")
public class CompanyAuditingController {


    @Autowired
    private SupplierCompanyAuditingService supplierCompanyAuditingService;
    @Autowired
    private SupplierAllinpayService supplierAllinpayService;

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

    }

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

    @GetMapping("/auditInfo")
    @Operation(summary = "查看申请审核情况", description = "查看申请审核情况")
    public ServerResponseEntity<SupplierCompanyAuditingVO> auditInfo(@RequestParam("supplierId") Long supplierId) {
        return ServerResponseEntity.success(supplierCompanyAuditingService.getAuditInfo(supplierId));
    }
}
