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

import cn.hutool.core.util.BooleanUtil;
import cn.hutool.core.util.PhoneUtil;
import com.tmerclub.cloud.api.auth.constant.SysTypeEnum;
import com.tmerclub.cloud.api.auth.feign.AccountFeignClient;
import com.tmerclub.cloud.api.auth.vo.AuthAccountVO;
import com.tmerclub.cloud.api.supplier.constant.SupplierStatus;
import com.tmerclub.cloud.api.supplier.vo.SupplierApiDetailVO;
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.common.util.PrincipalUtil;
import com.tmerclub.cloud.supplier.constant.AuditStatus;
import com.tmerclub.cloud.supplier.dto.AuditingSupplierInfoDTO;
import com.tmerclub.cloud.supplier.dto.SupplierAuditingDTO;
import com.tmerclub.cloud.supplier.service.SupplierAllinpayService;
import com.tmerclub.cloud.supplier.service.SupplierAuditingService;
import com.tmerclub.cloud.supplier.service.SupplierDetailService;
import com.tmerclub.cloud.supplier.vo.SupplierAuditingInfoVO;
import com.tmerclub.cloud.supplier.vo.SupplierAuditingVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
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 YXF
 * @date 2021-02-01 17:10:35
 */
@RestController("platformSupplierAuditingController")
@RequestMapping("/p/supplier_auditing")
@Tag(name = "platform-平台审核信息")
public class SupplierAuditingController {

    @Autowired
    private SupplierAuditingService supplierAuditingService;

    @Autowired
    private SupplierDetailService supplierDetailService;

    @DubboReference
    private AccountFeignClient accountFeignClient;

    @Autowired
    private SupplierAllinpayService supplierAllinpayService;

    @GetMapping("/page")
    @Operation(summary = "分页查询", description = "分页查询")
    public ServerResponseEntity<PageVO<SupplierAuditingInfoVO>> getSupplierAuditingPage(PageDTO pageDTO, AuditingSupplierInfoDTO auditingInfoDTO) {
        checkAuthorized();
        return ServerResponseEntity.success(supplierAuditingService.auditingInfoPage(pageDTO, auditingInfoDTO));
    }

    @GetMapping("/{supplierId}")
    @Operation(summary = "根据供应商id查询详情信息", description = "根据供应商id查询详情信息")
    public ServerResponseEntity<SupplierAuditingVO> getSupplierAuditing(@PathVariable Long supplierId) {
        checkAuthorized();
        SupplierAuditingVO supplierAuditing = supplierAuditingService.getBySupplierId(supplierId);
        if (Objects.isNull(supplierAuditing)) {
            supplierAuditing = new SupplierAuditingVO();
        }
        SupplierApiDetailVO supplierApiDetailVO = supplierDetailService.getBySupplierId(supplierId);
        supplierApiDetailVO.setContactPhone(PhoneUtil.hideBetween(supplierApiDetailVO.getContactPhone()).toString());
        if (PrincipalUtil.isMobile(supplierApiDetailVO.getSupplierAccount())) {
            supplierApiDetailVO.setSupplierAccount(PhoneUtil.hideBetween(supplierApiDetailVO.getSupplierAccount()).toString());
        }
        if (Objects.equals(supplierApiDetailVO.getSupplierStatus(), SupplierStatus.OFFLINE.value())) {
            supplierAuditing.setStatus(AuditStatus.OFFLINE.value());
        } else if (Objects.equals(supplierApiDetailVO.getSupplierStatus(), SupplierStatus.OFFLINE_AWAIT_AUDIT.value()) || Objects.equals(supplierApiDetailVO.getSupplierStatus(), SupplierStatus.OPEN_AWAIT_AUDIT.value())) {
            supplierAuditing.setStatus(AuditStatus.WAITAUDIT.value());
        }
        ServerResponseEntity<AuthAccountVO> responseEntity = accountFeignClient.getAccountInfoByTenantId(supplierId, SysTypeEnum.SUPPLIER.value());
        if (responseEntity.isSuccess() && Objects.nonNull(responseEntity.getData())) {
            AuthAccountVO authAccountVO = responseEntity.getData();
            supplierApiDetailVO.setSupplierAccount(authAccountVO.getPhone());
            supplierApiDetailVO.setAccountStatus(authAccountVO.getStatus());
        }
        supplierAuditing.setSupplierDetail(supplierApiDetailVO);
        return ServerResponseEntity.success(supplierAuditing);
    }

    @PutMapping("/audit")
    @Operation(summary = "审核信息(审核供应商)", description = "审核信息(审核供应商)")
    public ServerResponseEntity<Void> audit(@Valid @RequestBody SupplierAuditingDTO supplierAuditingDTO) {
        if (supplierAllinpayService.getIsAllinpay()) {
            throw new LuckException("开启通联后，平台无需审核供应商");
        }
        checkAuthorized();
        supplierDetailService.audit(supplierAuditingDTO);
        supplierDetailService.removeCacheBySupplierId(supplierAuditingDTO.getSupplierId());

        return ServerResponseEntity.success();
    }

    /**
     * 校验权限
     */
    private void checkAuthorized() {
        if (!Objects.equals(Constant.PLATFORM_SHOP_ID, AuthUserContext.get().getTenantId())) {
            throw new LuckException(ResponseEnum.UNAUTHORIZED);
        }
    }
}
