package com.hfkj.channel.alipay;

import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.entity.BsMerAttach;
import com.hfkj.model.MerBasisModel;
import com.hfkj.service.BsMerAttachService;
import com.hfkj.service.BsMerContractRecordService;
import com.hfkj.service.BsMerService;
import com.hfkj.sysenum.MerAttachType;
import com.hfkj.sysenum.PlatformTypeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @className: AlipayMerService
 * @author: HuRui
 * @date: 2023/2/20
 **/
@Service("alipayMerService")
public class AlipayMerService {

    @Resource
    private BsMerService merService;
    @Resource
    private BsMerAttachService merAttachService;
    @Resource
    private BsMerContractRecordService merContractRecordService;

    private List<BsMerAttach> merAttachList;

    public void add(Long merId) {
        MerBasisModel basisModel = merService.getMerDetail(merId);

        // 查询商户附件列表
        merAttachList = merAttachService.getAttachListByMer(merId, PlatformTypeEnum.type0, null);

        Map<String,Object> body = new HashMap<>();
        body.put("external_id", basisModel.getMerNo());
        body.put("name", basisModel.getMerName());
        body.put("alias_name", basisModel.getMerAbbreviate());
        body.put("merchant_type", "");
        body.put("mcc", "");
        body.put("cert_no", "");
        body.put("cert_type", "");
        body.put("cert_image", "");
        body.put("cert_name", "");
        body.put("cert_image_back", "");
        body.put("legal_name", "");
        body.put("legal_cert_no", "");
        body.put("legal_cert_front_image", "");
        body.put("legal_cert_back_image", "");
        body.put("service_phone", "");

    }


    /**
     * 获取商户附件
     * @param attachType
     * @return
     */
    private List<BsMerAttach> getMerAttach(MerAttachType attachType) {
        if (merAttachList == null || merAttachList.size() == 0) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未上传相关图片");
        }
        List<BsMerAttach> collect = merAttachList.stream()
                .filter(o -> o.getType().equals(attachType.getNumber()))
                .collect(Collectors.toList());
        if (collect.size() == 0) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未上传" + attachType.getName() + "图片");
        }
        return collect;
    }

}
