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

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.multishop.constant.AuditStatus;
import com.tmerclub.cloud.multishop.dto.ShopCompanyDTO;
import com.tmerclub.cloud.multishop.service.CompanyAuditingService;
import com.tmerclub.cloud.multishop.service.ShopDetailService;
import com.tmerclub.cloud.multishop.vo.ShopCompanyAuditingVO;
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("multishopCompanyAuditingController")
@RequestMapping("/m/shopCompanyAuditing")
@Tag(name = "multishop-店铺变更工商信息")
public class CompanyAuditingController {

    @Autowired
    private CompanyAuditingService companyAuditingService;
    @Autowired
    private ShopDetailService shopDetailService;

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

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

}
