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

import com.tmerclub.cloud.api.multishop.vo.ShopDetailVO;
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.multishop.constant.AuditStatus;
import com.tmerclub.cloud.multishop.dto.ShopCompanyDTO;
import com.tmerclub.cloud.multishop.model.ShopCompany;
import com.tmerclub.cloud.multishop.service.ShopAllinpayService;
import com.tmerclub.cloud.multishop.service.ShopCompanyService;
import com.tmerclub.cloud.multishop.service.ShopDetailService;
import com.tmerclub.cloud.multishop.vo.ShopCompanyVO;
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("platformShopCompanyController")
@RequestMapping("/p/shop_company")
@Tag(name = "platform-商家工商信息")
public class ShopCompanyController {

    @Autowired
    private ShopCompanyService shopCompanyService;


    @Autowired
    private ShopAllinpayService shopAllinpayService;

    @Autowired
    private ShopDetailService shopDetailService;

    @GetMapping
    @Operation(summary = "根据店铺id获取店铺工商信息", description = "根据店铺id获取店铺工商信息")
    @Parameter(name = "status", description = "审核状态：1：已通过 0待审核 -1未通过")
    public ServerResponseEntity<ShopCompanyVO> getShopCompanyByShopId(@RequestParam("shopId") Long shopId, @RequestParam(value = "status", required = false, defaultValue = "1") Integer status) {
        if (shopAllinpayService.getIsAllinpay()) {
            // 影印件还没通过审核
            ShopDetailVO shopDetailVO = shopDetailService.getByShopId(shopId);
            if (!Objects.equals(shopDetailVO.getIdCardCollectProcessStatus(), IdCardCollectProcessStatus.ALL.value())) {
                status = AuditStatus.WAITAUDIT.value();
            }
        }
        return ServerResponseEntity.success(shopCompanyService.getShopCompanyByShopIdAndStatus(shopId, status));
    }

    @PutMapping
    @Operation(summary = "更新店铺工商信息", description = "更新店铺工商信息")
    public ServerResponseEntity<Void> editShopCompany(@RequestBody @Valid ShopCompanyDTO shopCompanyDTO) {
        boolean isAllinpay = shopAllinpayService.getIsAllinpay();
        if (Objects.isNull(shopCompanyDTO.getShopId())) {
            throw new LuckException("店铺id不能为空");
        }
        shopCompanyService.batchUpdateStatus(shopCompanyDTO.getShopId(), AuditStatus.FAILAUDIT.value(), null);
        ShopCompany shopCompany = BeanUtil.map(shopCompanyDTO, ShopCompany.class);
        shopCompany.setStatus(isAllinpay ? AuditStatus.WAITAUDIT.value() : AuditStatus.SUCCESSAUDIT.value());
        if (isAllinpay) {
            shopCompanyService.platformUpdateShopCompany(shopCompany, shopCompany.getShopId());
        } else {
            shopCompanyService.updateByShopId(shopCompany, shopCompany.getShopId(), 0);
        }
        return ServerResponseEntity.success();
    }
}
