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

import com.tmerclub.cloud.api.payment.feign.AllinpayFeignClient;
import com.tmerclub.cloud.api.supplier.vo.SupplierApiDetailVO;
import com.tmerclub.cloud.common.constant.AllinpayConstant;
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.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Objects;

/**
 * @Author lth
 * @Date 2021/8/14 16:53
 */
@RestController("adminSupplierCompanyController")
@RequestMapping("/mp/supplier_company")
@Tag(name = "admin-供应商工商信息")
public class SupplierCompanyController {

    @Autowired
    private SupplierCompanyService supplierCompanyService;
    @DubboReference
    private AllinpayFeignClient allinpayFeignClient;
    @Autowired
    private SupplierAllinpayService supplierAllinpayService;
    @Autowired
    private SupplierDetailService supplierDetailService;

    @GetMapping("/checkCreditCode")
    @Operation(summary = "检查统一信用码是否已存在", description = "检查统一信用码是否已存在")
    public ServerResponseEntity<Boolean> checkCreditCode(@RequestParam("creditCode") String creditCode, @RequestParam("supplierId") Long supplierId) {
        Boolean res = supplierCompanyService.checkCreditCode(creditCode, supplierId);
        return ServerResponseEntity.success(res);
    }

    @PutMapping("/updateCardCtatus")
    @Operation(summary = "更新影印件状态", description = "通联独有")
    public ServerResponseEntity<Integer> updateCardStatus(@RequestParam("supplierId") Long supplierId) {
        if (!supplierAllinpayService.getIsAllinpay()) {
            return ServerResponseEntity.success();
        }
        if (Objects.isNull(supplierId)) {
            throw new LuckException("供应商id不能为空");
        }
        SupplierCompanyVO supplierCompanyVO = supplierCompanyService.getSupplierCompanyBySupplierIdAndStatus(supplierId, AuditStatus.WAITAUDIT.value());
        if (Objects.isNull(supplierCompanyVO)) {
            supplierCompanyVO = supplierCompanyService.getSupplierCompanyBySupplierIdAndStatus(supplierId, AuditStatus.SUCCESSAUDIT.value());
        }
        SupplierApiDetailVO supplierApiDetailVO = supplierDetailService.getBySupplierId(supplierId);
        if (Objects.equals(supplierApiDetailVO.getIdCardCollectProcessStatus(), IdCardCollectProcessStatus.ALL.value())) {
            // 更新下更新时间
            supplierCompanyService.updateById(BeanUtil.map(supplierCompanyVO, SupplierCompany.class));
            // 已经审核通过不用更新
            return ServerResponseEntity.success(supplierApiDetailVO.getIdCardCollectProcessStatus());
        }
        // 可能回调有问题导致这里没刷新，所以多给一个接口
        ServerResponseEntity<Integer> idCardStatus = allinpayFeignClient.getIdCardStatus(AllinpayConstant.SUPPLIER + supplierId);
        if (!idCardStatus.isSuccess()) {
            throw new LuckException(idCardStatus.getMsg());
        }
        if (Objects.equals(supplierApiDetailVO.getIdCardCollectProcessStatus(), idCardStatus.getData())) {
            // 更新下更新时间
            supplierCompanyService.updateById(BeanUtil.map(supplierCompanyVO, SupplierCompany.class));
            // 状态一致不用更新
            return ServerResponseEntity.success(idCardStatus.getData());
        }
        supplierDetailService.updateAllinpayIdCardStatus(AllinpayConstant.SUPPLIER + supplierId, idCardStatus.getData());
        supplierDetailService.removeCacheBySupplierId(supplierId);
        return ServerResponseEntity.success();
    }

    @PutMapping("/uploadCard")
    @Operation(summary = "重新上传影印件", description = "通联独有")
    public ServerResponseEntity<String> uploadIdCard(@RequestBody SupplierCompanyDTO supplierCompanyDTO) {
        if (!supplierAllinpayService.getIsAllinpay()) {
            return ServerResponseEntity.success("非通联环境");
        }
        Long supplierId = supplierCompanyDTO.getSupplierId();
        if (Objects.isNull(supplierId)) {
            throw new LuckException("供应商id不能为空");
        }
        SupplierApiDetailVO supplierApiDetailVO = supplierDetailService.getBySupplierId(supplierId);
        ServerResponseEntity<Integer> idCardStatus = allinpayFeignClient.getIdCardStatus(AllinpayConstant.SUPPLIER + supplierId);
        if (!idCardStatus.isSuccess()) {
            throw new LuckException(idCardStatus.getMsg());
        }
        String result = supplierCompanyService.uploadIdCard(supplierCompanyDTO, supplierApiDetailVO, idCardStatus.getData());
        supplierDetailService.removeCacheBySupplierId(supplierId);
        if (!Objects.equals(result, AllinpayConstant.ALLINPAY_AUDIT_SUCCESS) || Objects.equals(result, AllinpayConstant.ID_CARD_UPDATE_SUCCESS)) {
            return ServerResponseEntity.showFailMsg(result);
        }
        return ServerResponseEntity.success(result);
    }
}
