
package com.tmerclub.cloud.multishop.feign;

import com.tmerclub.cloud.api.multishop.bo.EsShopDetailBO;
import com.tmerclub.cloud.api.multishop.bo.ShopSimpleBO;
import com.tmerclub.cloud.api.multishop.feign.ShopDetailFeignClient;
import com.tmerclub.cloud.api.multishop.vo.ShopDetailVO;
import com.tmerclub.cloud.api.multishop.vo.ShopUserVO;
import com.tmerclub.cloud.common.constant.AllinpayConstant;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.multishop.mapper.ShopDetailMapper;
import com.tmerclub.cloud.multishop.model.ShopDetail;
import com.tmerclub.cloud.multishop.service.CompanyAuditingService;
import com.tmerclub.cloud.multishop.service.ShopAllinpayService;
import com.tmerclub.cloud.multishop.service.ShopDetailService;
import com.tmerclub.cloud.multishop.service.ShopUserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;

/**
 * @author tmerclub
 * @date 2020/11/23
 */
@DubboService
public class ShopDetailFeignController implements ShopDetailFeignClient {
    @Autowired
    private ShopDetailService shopDetailService;
    @Autowired
    private ShopUserService shopUserService;
    @Autowired
    private ShopDetailMapper shopDetailMapper;
    @Autowired
    private ShopAllinpayService shopAllinpayService;
    @Autowired
    private CompanyAuditingService companyAuditingService;

    @Override
    public ServerResponseEntity<EsShopDetailBO> getShopByShopId(Long shopId) {
        if (Objects.isNull(shopId)) {
            return ServerResponseEntity.success(new EsShopDetailBO());
        }
        ShopDetailVO shopDetail = shopDetailService.getByShopId(shopId);
        if (Objects.isNull(shopDetail)) {
            return ServerResponseEntity.success(new EsShopDetailBO());
        }
        return ServerResponseEntity.success(BeanUtil.map(shopDetail, EsShopDetailBO.class));
    }

    @Override
    public ServerResponseEntity<ShopDetailVO> getShopDetailByShopId(Long shopId) {
        ShopDetailVO shopDetail = shopDetailService.getByShopId(shopId);
        if (Objects.isNull(shopDetail)) {
            return ServerResponseEntity.success(new ShopDetailVO());
        }
        return ServerResponseEntity.success(shopDetail);
    }


    @Override
    public ServerResponseEntity<List<ShopDetailVO>> listByShopIds(List<Long> shopIds) {
        List<ShopDetail> shopDetail = shopDetailService.listByShopIds(shopIds);
        return ServerResponseEntity.success(BeanUtil.mapAsList(shopDetail, ShopDetailVO.class));
    }

    @Override
    public ServerResponseEntity<EsShopDetailBO> shopExtensionData(Long shopId) {
        return ServerResponseEntity.success(shopDetailService.shopExtensionData(shopId));
    }

    @Override
    public ServerResponseEntity<List<ShopDetailVO>> getShopDetailByShopIdAndShopName(String shopName) {
        return ServerResponseEntity.success(shopDetailService.getShopDetailByShopIdsShopName(null, shopName));
    }

    @Override
    public ServerResponseEntity<List<ShopSimpleBO>> listSimple(ShopSimpleBO shopSimpleBO) {
        return ServerResponseEntity.success(shopDetailService.listSimple(shopSimpleBO));
    }

    @Override
    public ServerResponseEntity<ShopUserVO> getShopUserByShopUserId(Long shopUserId) {
        return ServerResponseEntity.success(BeanUtil.map(shopUserService.getSimpleByUserId(shopUserId), ShopUserVO.class));
    }

    @Override
    public ServerResponseEntity<List<Long>> getShopIdsByShopStatus(Integer shopStatus) {
        return ServerResponseEntity.success(shopDetailService.getShopIdByShopStatus(shopStatus));
    }

    @Override
    public ServerResponseEntity<List<Long>> getShopIdsByLikeShopName(String shopName) {
        return ServerResponseEntity.success(shopDetailService.getShopIdsLikeName(shopName.trim()));
    }

    @Override
    public ServerResponseEntity<Void> updateAllinpayIdCardStatus(String bizUserId, Integer status) {
        shopDetailService.updateAllinpayIdCardStatus(bizUserId, status);
        shopDetailService.removeCacheByShopId(Long.valueOf(bizUserId.substring(AllinpayConstant.SHOP_LENGTH)));
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Void> updateAllinpayBindPhoneStatus(String bizUserId, Integer status) {
        shopDetailMapper.updateShopBindPhone(Long.valueOf(bizUserId.substring(AllinpayConstant.SHOP_LENGTH)), status);
        shopDetailService.removeCacheByShopId(Long.valueOf(bizUserId.substring(AllinpayConstant.SHOP_LENGTH)));
        return ServerResponseEntity.success();
    }

    @Override
    public void deleteAllinpayCache() {
        shopAllinpayService.deleteAllinpayCache();
    }

    @Override
    public ServerResponseEntity<Void> updateCompanyInfoResult(String bizUserId, Integer status, Integer result, String failReason) {
        companyAuditingService.updateCompanyInfoResult(bizUserId, status, result, failReason);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Void> updateAcctProtocolNoByType(Long shopId, Integer signAcctType, String acctProtocolNo) {
        shopDetailMapper.updateAcctProtocolNoByType(shopId, signAcctType, acctProtocolNo);
        shopDetailService.removeCacheByShopId(shopId);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<List<ShopDetailVO>> listShopIds() {
        return ServerResponseEntity.success(shopDetailService.listShopIds());
    }
}
