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

import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
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.multishop.constant.ShopStatus;
import com.tmerclub.cloud.api.multishop.vo.ShopDetailVO;
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.multishop.constant.AuditStatus;
import com.tmerclub.cloud.multishop.dto.AuditingInfoDTO;
import com.tmerclub.cloud.multishop.dto.ShopAuditingDTO;
import com.tmerclub.cloud.multishop.service.ShopAllinpayService;
import com.tmerclub.cloud.multishop.service.ShopAuditingService;
import com.tmerclub.cloud.multishop.service.ShopDetailService;
import com.tmerclub.cloud.multishop.vo.ShopAuditingInfoVO;
import com.tmerclub.cloud.multishop.vo.ShopAuditingVO;
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("platformShopAuditingController")
@RequestMapping("/p/shopAuditing")
@Tag(name = "platform-平台审核信息")
public class ShopAuditingController {

    @Autowired
    private ShopAuditingService shopAuditingService;

    @Autowired
    private ShopDetailService shopDetailService;

    @DubboReference
    private AccountFeignClient accountFeignClient;

    @Autowired
    private ShopAllinpayService shopAllinpayService;

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

    @GetMapping("/{shopId}")
    @Operation(summary = "根据店铺id查询详情信息" , description = "根据店铺id查询详情信息")
    public ServerResponseEntity<ShopAuditingVO> getShopAuditing(@PathVariable Long shopId) {
        checkAuthorized();
        ShopAuditingVO shopAuditing = shopAuditingService.getByshopId(shopId);
        if (Objects.isNull(shopAuditing)){
            shopAuditing = new ShopAuditingVO();
        }
        ShopDetailVO shopDetailVO = shopDetailService.getByShopId(shopId);
        if (StrUtil.isNotBlank(shopDetailVO.getContactPhone())) {
            shopDetailVO.setContactPhone(PhoneUtil.hideBetween(shopDetailVO.getContactPhone()).toString());
        }
        if (PrincipalUtil.isMobile(shopDetailVO.getMerchantAccount())){
            shopDetailVO.setMerchantAccount(PhoneUtil.hideBetween(shopDetailVO.getMerchantAccount()).toString());
        }
        if (Objects.equals(shopDetailVO.getShopStatus(), ShopStatus.OFFLINE.value())) {
            shopAuditing.setStatus(AuditStatus.OFFLINE.value());
        } else if (Objects.equals(shopDetailVO.getShopStatus(), ShopStatus.OFFLINE_AWAIT_AUDIT.value()) || Objects.equals(shopDetailVO.getShopStatus(), ShopStatus.OPEN_AWAIT_AUDIT.value())) {
            shopAuditing.setStatus(AuditStatus.WAITAUDIT.value());
        }
        ServerResponseEntity<AuthAccountVO> responseEntity = accountFeignClient.getAccountInfoByTenantId(shopId, SysTypeEnum.MULTISHOP.value());
        if (responseEntity.isSuccess() && Objects.nonNull(responseEntity.getData())) {
            AuthAccountVO authAccountVO = responseEntity.getData();
            shopDetailVO.setMerchantAccount(authAccountVO.getPhone());
            shopDetailVO.setAccountStatus(authAccountVO.getStatus());
        }
        shopAuditing.setShopDetail(shopDetailVO);
        return ServerResponseEntity.success(shopAuditing);
    }

    @PutMapping("/audit")
    @Operation(summary = "审核信息(审核商家)" , description = "审核信息(审核商家)")
    public ServerResponseEntity<Void> audit(@Valid @RequestBody ShopAuditingDTO shopAuditingDTO) {
        if (shopAllinpayService.getIsAllinpay()) {
            throw new LuckException("开启通联后，平台无需审核商家");
        }
        checkAuthorized();
        shopDetailService.audit(shopAuditingDTO);
        shopDetailService.removeCacheByShopId(shopAuditingDTO.getShopId());

        return ServerResponseEntity.success();
    }

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