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


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.supplier.constant.AuditStatus;
import com.tmerclub.cloud.supplier.dto.SupplierCompanyDTO;
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/13 14:06
 */
@RestController("supplierCompanyAuditingController")
@RequestMapping("/s/supplier_company_auditing")
@Tag(name = "supplier-供应商变更工商信息")
public class CompanyAuditingController {

    @Autowired
    private SupplierCompanyAuditingService supplierCompanyAuditingService;


    @PostMapping("/applyChangeCompanyInfo")
    @Operation(summary = "申请变更工商信息", description = "申请变更工商信息")
    public ServerResponseEntity<Void> applyChangeCompanyInfo(@RequestBody @Valid SupplierCompanyDTO supplierCompanyDTO) {
        Long supplierId = AuthUserContext.get().getTenantId();
        // 校验是否已经存在申请中记录
        SupplierCompanyAuditingVO latestAuditing = supplierCompanyAuditingService.getLatestAuditingBySupplierId(supplierId);
        if (Objects.nonNull(latestAuditing) && Objects.equals(latestAuditing.getStatus(), AuditStatus.WAITAUDIT.value())) {
            throw new LuckException("不能同时提交多次签约申请，请耐心等候平台审核，或者把上次提交申请撤销后重新申请");
        }
        supplierCompanyDTO.setSupplierId(supplierId);
        supplierCompanyAuditingService.applyChangeCompanyInfo(supplierCompanyDTO);
        return ServerResponseEntity.success();
    }

    @PutMapping("/revoke")
    @Operation(summary = "撤销申请", description = "撤销申请")
    public ServerResponseEntity<Void> revoke() {
        Long supplierId = AuthUserContext.get().getTenantId();
        supplierCompanyAuditingService.revoke(supplierId);
        return ServerResponseEntity.success();
    }

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