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

import cn.hutool.core.util.PhoneUtil;
import cn.hutool.core.util.StrUtil;
import com.tmerclub.cloud.api.multishop.vo.ShopDetailVO;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.constant.IdCardCollectProcessStatus;
import com.tmerclub.cloud.common.constant.StatusEnum;
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.common.util.BeanUtil;
import com.tmerclub.cloud.multishop.constant.AuditStatus;
import com.tmerclub.cloud.multishop.dto.ShopCompanyDTO;
import com.tmerclub.cloud.multishop.mapper.ShopDetailMapper;
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.Parameters;
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
 * @Date 2021/4/25 16:31
 */
@RestController
@RequestMapping("/m/applyShop/shopCompany")
@Tag(name = "multishop-店铺工商信息")
public class ShopCompanyController {

    @Autowired
    private ShopCompanyService shopCompanyService;
    @Autowired
    private ShopDetailMapper shopDetailMapper;
    @Autowired
    private ShopAllinpayService shopAllinpayService;
    @Autowired
    private ShopDetailService shopDetailService;

    @PostMapping
    @Operation(summary = "新增店铺工商信息", description = "新增店铺工商信息")
    public ServerResponseEntity<Void> save(@Valid @RequestBody ShopCompanyDTO shopCompanyDTO) {
        Long shopId = this.getShopId();
        checkAllinpay(shopCompanyDTO);
        ShopCompany shopCompany = BeanUtil.map(shopCompanyDTO, ShopCompany.class);
        shopCompany.setShopId(shopId);
        shopCompany.setStatus(AuditStatus.WAITAUDIT.value());
        shopCompanyService.save(shopCompany, true);
        return ServerResponseEntity.success();
    }

    private void checkAllinpay(ShopCompanyDTO shopCompanyDTO) {
        boolean needInfo = Objects.isNull(shopCompanyDTO.getLegalIds()) || Objects.isNull(shopCompanyDTO.getLegalPhone());
        if (shopAllinpayService.getIsAllinpay() && needInfo) {
            throw new LuckException("开店所需要提交的信息有所变更，请重新填写");
        }
    }

    @PutMapping
    @Operation(summary = "商家端更新店铺工商信息,仅在申请开店时调用", description = "更新店铺工商信息")
    public ServerResponseEntity<Void> update(@Valid @RequestBody ShopCompanyDTO shopCompanyDTO) {
        checkAllinpay(shopCompanyDTO);
        ShopCompany shopCompany = BeanUtil.map(shopCompanyDTO, ShopCompany.class);
        shopCompany.setStatus(AuditStatus.WAITAUDIT.value());
        shopCompanyService.checkInfo(shopCompany, false, null);
        shopCompanyService.update(shopCompany);
        return ServerResponseEntity.success();
    }

    @PostMapping("/storage")
    @Operation(summary = "存储店铺工商信息", description = "存储店铺工商信息,已存在则更新，不存在则新增")
    public ServerResponseEntity<Void> storage(@Valid @RequestBody ShopCompanyDTO shopCompanyDTO) {
        Long shopId = this.getShopId();
        checkAllinpay(shopCompanyDTO);
        ShopCompanyVO shopCompanyVO = shopCompanyService.getShopCompanyByShopIdAndStatus(shopId, AuditStatus.WAITAUDIT.value());
        ShopCompany shopCompany = BeanUtil.map(shopCompanyDTO, ShopCompany.class);
        shopCompany.setShopId(shopId);
        shopCompany.setStatus(AuditStatus.WAITAUDIT.value());
        if (Objects.isNull(shopCompanyVO)) {
            // 不存在，新增
            shopCompanyService.save(shopCompany, true);
        } else {
            // 已存在，更新
            shopCompany.setShopCompanyId(shopCompanyVO.getShopCompanyId());
            shopCompanyService.checkInfo(shopCompany, false, null);
            shopCompanyService.update(shopCompany);
        }
        return ServerResponseEntity.success();
    }

    @GetMapping
    @Operation(summary = "获取店铺工商信息")
    @Parameter(name = "status", description = "审核状态：1：已通过 0待审核 -1未通过")
    public ServerResponseEntity<ShopCompanyVO> getShopCompanyByShopId(@RequestParam(value = "status", required = false, defaultValue = "1") Integer status) {
        Long shopId = this.getShopId();
        Integer isPassShop = AuthUserContext.get().getIsPassShop();
        if (Objects.equals(isPassShop, StatusEnum.DISABLE.value())) {
            status = AuditStatus.WAITAUDIT.value();
        }
        if (shopAllinpayService.getIsAllinpay()) {
            // 影印件还没通过审核
            ShopDetailVO shopDetailVO = shopDetailService.getByShopId(shopId);
            if (!Objects.equals(shopDetailVO.getIdCardCollectProcessStatus(), IdCardCollectProcessStatus.ALL.value())) {
                status = AuditStatus.WAITAUDIT.value();
            }
        }
        ShopCompanyVO shopCompanyVO = shopCompanyService.getShopCompanyByShopIdAndStatus(shopId, status);
        if (Objects.nonNull(shopCompanyVO) && StrUtil.isNotBlank(shopCompanyVO.getLegalPhone())) {
            shopCompanyVO.setLegalPhone(PhoneUtil.hideBetween(shopCompanyVO.getLegalPhone()).toString());
        }
        return ServerResponseEntity.success(shopCompanyVO);
    }

    @PutMapping("/updateAcctProtocolNo")
    @Operation(summary = "更新店铺账户协议编号")
    @Parameters({
            @Parameter(name = "signAcctType", description = "签约类型 1.个人 2.法人 3.企业"),
            @Parameter(name = "acctProtocolNo", description = "账户提现协议编号")
    })
    public ServerResponseEntity<Void> updateAcctProtocolNo(@RequestParam("signAcctType") Integer signAcctType,
                                                           @RequestParam("acctProtocolNo") String acctProtocolNo) {
        Long shopId = this.getShopId();
        // 企业会员-“企业名称”“法人姓名”变更后，需重新签约。
        shopDetailMapper.updateAcctProtocolNoByType(shopId, signAcctType, acctProtocolNo);
        shopDetailService.removeCacheByShopId(shopId);
        return ServerResponseEntity.success();
    }

    private Long getShopId() {
        Long shopId = AuthUserContext.get().getTenantId();
        if (Objects.equals(shopId, Constant.DEFAULT_SHOP_ID)) {
            throw new LuckException("店铺不存在");
        }
        return shopId;
    }

}
