
package com.tmerclub.cloud.supplier.feign;

import com.tmerclub.cloud.api.auth.constant.SysTypeEnum;
import com.tmerclub.cloud.api.supplier.bo.SupplierDetailBO;
import com.tmerclub.cloud.api.supplier.feign.SupplierDetailFeignClient;
import com.tmerclub.cloud.api.supplier.vo.SupplierApiDetailVO;
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.supplier.dto.SupplierDetailDTO;
import com.tmerclub.cloud.supplier.mapper.SupplierDetailMapper;
import com.tmerclub.cloud.supplier.model.SupplierDetail;
import com.tmerclub.cloud.supplier.service.SupplierAllinpayService;
import com.tmerclub.cloud.supplier.service.SupplierCompanyAuditingService;
import com.tmerclub.cloud.supplier.service.SupplierDetailService;
import com.tmerclub.cloud.supplier.service.SupplierUserService;
import com.tmerclub.cloud.supplier.vo.SupplierDetailVO;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author tmerclub
 * @date 2020/11/23
 */
@DubboService
public class SupplierDetailFeignController implements SupplierDetailFeignClient {
    private static final Logger logger = LoggerFactory.getLogger(SupplierDetailFeignController.class);

    @Autowired
    private SupplierDetailService supplierDetailService;
    @Autowired
    private SupplierUserService supplierUserService;
    @Autowired
    private SupplierAllinpayService supplierAllinpayService;
    @Autowired
    private SupplierDetailMapper supplierDetailMapper;
    @Autowired
    private SupplierCompanyAuditingService companyAuditingService;

    @Override
    public ServerResponseEntity<SupplierDetailBO> getSupplierBySupplierId(Long supplierId) {
        SupplierApiDetailVO supplierDetail = supplierDetailService.getBySupplierId(supplierId);
        if (Objects.isNull(supplierDetail)) {
            return ServerResponseEntity.success(new SupplierDetailBO());
        }
        return ServerResponseEntity.success(BeanUtil.map(supplierDetail, SupplierDetailBO.class));
    }

    @Override
    public ServerResponseEntity<List<SupplierApiDetailVO>> listBySupplierIds(List<Long> supplierIds) {
        List<SupplierDetail> supplierDetail = supplierDetailService.listBySupplierIds(supplierIds);
        return ServerResponseEntity.success(BeanUtil.mapAsList(supplierDetail, SupplierApiDetailVO.class));
    }

    @Override
    public ServerResponseEntity<List<SupplierApiDetailVO>> getSupplierDetailBySupplierName(String supplierShopName) {
        return ServerResponseEntity.success(supplierDetailService.getSupplierDetailBySupplierIdAndSupplierName(null, supplierShopName));
    }

    @Override
    public ServerResponseEntity<List<SupplierApiDetailVO>> listSupplierDetailBySupplierIds(List<Long> supplierIds) {
        return ServerResponseEntity.success(supplierDetailService.listSupplierDetailBySupplierIds(supplierIds));
    }

    @Override
    public ServerResponseEntity<List<Long>> listSupplierIdByShopIdAndSupplierName(Long shopId, String supplierName) {
        SupplierDetailDTO supplierDetailDTO = new SupplierDetailDTO();
        supplierDetailDTO.setSupplierName(supplierName);
        supplierDetailDTO.setSysType(SysTypeEnum.MULTISHOP.value());
        List<SupplierDetailVO> supplierDetailVOList = supplierDetailService.listSupplierByShopId(supplierDetailDTO, shopId);
        return ServerResponseEntity.success(supplierDetailVOList.stream().map(SupplierDetailVO::getSupplierId).collect(Collectors.toList()));

    }

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

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

    @Override
    public ServerResponseEntity<Void> updateAllinpayBindPhoneStatus(String bizUserId, Integer status) {
        supplierDetailMapper.updateSupplierBindPhone(Long.valueOf(bizUserId.substring(AllinpayConstant.SUPPLIER_LENGTH)), status);
        supplierDetailService.removeCacheBySupplierId(Long.valueOf(bizUserId.substring(AllinpayConstant.SUPPLIER_LENGTH)));
        return ServerResponseEntity.success();
    }

    @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 supplierId, Integer signAcctType, String acctProtocolNo) {
        supplierDetailMapper.updateAcctProtocolNoByType(supplierId, signAcctType, acctProtocolNo);
        supplierDetailService.removeCacheBySupplierId(supplierId);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<List<SupplierApiDetailVO>> listSupplierIds() {
        return ServerResponseEntity.success(supplierDetailService.listSupplierIds());
    }
}
