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

import com.tmerclub.cloud.api.multishop.vo.ShopDetailVO;
import com.tmerclub.cloud.api.payment.feign.AllinpayFeignClient;
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.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.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("adminShopCompanyController")
@RequestMapping("/ma/shopCompany")
@Tag(name = "admin-商家工商信息")
public class ShopCompanyController {

    @Autowired
    private ShopCompanyService shopCompanyService;
    @DubboReference
    private AllinpayFeignClient allinpayFeignClient;
    @Autowired
    private ShopAllinpayService shopAllinpayService;
    @Autowired
    private ShopDetailService shopDetailService;

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

    @PutMapping("/updateCardCtatus")
    @Operation(summary = "更新影印件状态", description = "通联独有")
    public ServerResponseEntity<Integer> updateCardStatus(@RequestParam("shopId") Long shopId) {
        if (!shopAllinpayService.getIsAllinpay()) {
            return ServerResponseEntity.success();
        }
        if (Objects.isNull(shopId)) {
            throw new LuckException("店铺id不能为空");
        }
        ShopCompanyVO shopCompanyVO = shopCompanyService.getShopCompanyByShopIdAndStatus(shopId, AuditStatus.WAITAUDIT.value());
        if (Objects.isNull(shopCompanyVO)) {
            shopCompanyVO = shopCompanyService.getShopCompanyByShopIdAndStatus(shopId, AuditStatus.SUCCESSAUDIT.value());
        }
        ShopDetailVO shopDetailVO = shopDetailService.getByShopId(shopId);
        if (Objects.equals(shopDetailVO.getIdCardCollectProcessStatus(), IdCardCollectProcessStatus.ALL.value())) {
            // 更新下更新时间
            shopCompanyService.updateById(BeanUtil.map(shopCompanyVO, ShopCompany.class));
            // 已经审核通过不用更新
            return ServerResponseEntity.success(shopDetailVO.getIdCardCollectProcessStatus());
        }
        // 可能回调有问题导致这里没刷新，所以多给一个接口
        ServerResponseEntity<Integer> idCardStatus = allinpayFeignClient.getIdCardStatus(AllinpayConstant.SHOP + shopId);
        if (!idCardStatus.isSuccess()) {
            throw new LuckException(idCardStatus.getMsg());
        }
        if (Objects.equals(shopDetailVO.getIdCardCollectProcessStatus(), idCardStatus.getData())) {
            // 更新下更新时间
            shopCompanyService.updateById(BeanUtil.map(shopCompanyVO, ShopCompany.class));
            // 状态一致不用更新
            return ServerResponseEntity.success(idCardStatus.getData());
        }
        shopDetailService.updateAllinpayIdCardStatus(AllinpayConstant.SHOP + shopId, idCardStatus.getData());
        shopDetailService.removeCacheByShopId(shopId);
        return ServerResponseEntity.success(idCardStatus.getData());
    }

    @PutMapping("/uploadCard")
    @Operation(summary = "重新上传影印件", description = "通联独有")
    public ServerResponseEntity<String> uploadCard(@RequestBody ShopCompanyDTO shopCompanyDTO) {
        if (!shopAllinpayService.getIsAllinpay()) {
            return ServerResponseEntity.success("非通联环境");
        }
        Long shopId = shopCompanyDTO.getShopId();
        if (Objects.isNull(shopId)) {
            throw new LuckException("店铺id不能为空");
        }
        ShopDetailVO shopDetailVO = shopDetailService.getByShopId(shopId);
        ServerResponseEntity<Integer> idCardStatus = allinpayFeignClient.getIdCardStatus(AllinpayConstant.SHOP + shopId);
        if (!idCardStatus.isSuccess()) {
            throw new LuckException(idCardStatus.getMsg());
        }
        String result = shopCompanyService.uploadIdCard(shopCompanyDTO, shopDetailVO, idCardStatus.getData());
        shopDetailService.removeCacheByShopId(shopId);
        if (!Objects.equals(result, AllinpayConstant.ALLINPAY_AUDIT_SUCCESS) || Objects.equals(result, AllinpayConstant.ID_CARD_UPDATE_SUCCESS)) {
            return ServerResponseEntity.showFailMsg(result);
        }
        return ServerResponseEntity.success(result);
    }
}
