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

import com.tmerclub.cloud.api.supplier.vo.SupplierApiDetailVO;
import com.tmerclub.cloud.common.constant.IdCardCollectProcessStatus;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.supplier.constant.AuditStatus;
import com.tmerclub.cloud.supplier.dto.SupplierCompanyDTO;
import com.tmerclub.cloud.supplier.model.SupplierCompany;
import com.tmerclub.cloud.supplier.service.SupplierAllinpayService;
import com.tmerclub.cloud.supplier.service.SupplierCompanyService;
import com.tmerclub.cloud.supplier.service.SupplierDetailService;
import com.tmerclub.cloud.supplier.vo.SupplierCompanyVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
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 lth
 */
@RestController("platformSupplierCompanyController")
@RequestMapping("/p/supplier_company")
@Tag(name = "platform-供应商工商信息")
public class SupplierCompanyController {

    @Autowired
    private SupplierCompanyService supplierCompanyService;


    @Autowired
    private SupplierAllinpayService supplierAllinpayService;

    @Autowired
    private SupplierDetailService supplierDetailService;

    @GetMapping
    @Operation(summary = "根据供应商id获取供应商工商信息", description = "根据供应商id获取供应商工商信息")
    @Parameter(name = "status", description = "审核状态：1：已通过 0待审核 -1未通过")
    public ServerResponseEntity<SupplierCompanyVO> getSupplierCompanyBySupplierId(@RequestParam("supplierId") Long supplierId, @RequestParam(value = "status", required = false, defaultValue = "1") Integer status) {
        if (supplierAllinpayService.getIsAllinpay()) {
            // 影印件还没通过审核
            SupplierApiDetailVO supplierDetail = supplierDetailService.getBySupplierId(supplierId);
            if (!Objects.equals(supplierDetail.getIdCardCollectProcessStatus(), IdCardCollectProcessStatus.ALL.value())) {
                status = AuditStatus.WAITAUDIT.value();
            }
        }
        return ServerResponseEntity.success(supplierCompanyService.getSupplierCompanyBySupplierIdAndStatus(supplierId, status));
    }

    @PutMapping
    @Operation(summary = "更新供应商工商信息", description = "更新供应商工商信息")
    public ServerResponseEntity<Void> editSupplierCompany(@RequestBody @Valid SupplierCompanyDTO supplierCompanyDTO) {
        boolean isAllinpay = supplierAllinpayService.getIsAllinpay();
        if (Objects.isNull(supplierCompanyDTO.getSupplierId())) {
            throw new LuckException("供应商id不能为空");
        }
        supplierCompanyService.batchUpdateStatus(supplierCompanyDTO.getSupplierId(), AuditStatus.FAILAUDIT.value(), null);
        SupplierCompany supplierCompany = BeanUtil.map(supplierCompanyDTO, SupplierCompany.class);
        supplierCompany.setStatus(isAllinpay ? AuditStatus.WAITAUDIT.value() : AuditStatus.SUCCESSAUDIT.value());
        if (isAllinpay) {
            supplierCompanyService.platformUpdateSupplierCompany(supplierCompany, supplierCompany.getSupplierId());
        } else {
            supplierCompanyService.updateBySupplierId(supplierCompany, supplierCompany.getSupplierId(), 0);
        }
        return ServerResponseEntity.success();
    }
}
