package com.hfkj.service.impl;

import com.hfkj.channel.saas.SaasMerService;
import com.hfkj.channel.tianque.service.TianQueMerService;
import com.hfkj.channel.weixin.WeiXinMerService;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.entity.*;
import com.hfkj.channel.lakala.LaKaLaMerService;
import com.hfkj.service.*;
import com.hfkj.sysenum.MerCompleteStatusEnum;
import com.hfkj.sysenum.PlatformTypeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Service("merContractService")
public class BsMerContractServiceImpl implements BsMerContractService {

    @Resource
    private BsPlatformService platformService;

    @Resource
    private WeiXinMerService weiXinMerService;

    @Resource
    private TianQueMerService tianQueMerService;

    @Resource
    private BsMerService merService;

    @Resource
    private BsStoreService storeService;

    @Resource
    private SaasMerService saasMerService;

    @Override
    public void addMerContract(Long merId, PlatformTypeEnum platformType) throws Exception {
        BsMer mer = merService.getMer(merId);
        if (mer == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到商户");
        }
        if (mer.getCompleteStatus() != 1){
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "商户资料未完善");
        }

        if (platformType == null) {
            // 当前的进件平台
            List<BsPlatform> platformList = platformService.getPlatformList();
            for (BsPlatform platform : platformList) {
                if (platform.getPlatformType().equals(PlatformTypeEnum.type1.getNumber())) {

                    new Thread(() -> saasMerService.addMer(merId)).start();

                } else if (platform.getPlatformType().equals(PlatformTypeEnum.type5.getNumber())) {
                    new Thread(() -> tianQueMerService.addMer(merId)).start();

                }
            }
        } else {
            // 进固定的平台
            if (PlatformTypeEnum.type1.getNumber().equals(platformType.getNumber())) {
                new Thread(() -> saasMerService.addMer(merId)).start();

            } else if (PlatformTypeEnum.type5.getNumber().equals(platformType.getNumber())) {
                new Thread(() -> tianQueMerService.addMer(merId)).start();
            }
        }

    }

    @Override
    public void  reconsider(Long merId) throws Exception {
        BsMer mer = merService.getMer(merId);
        if (mer == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到商户");
        }
        // 当前的进件平台
        BsPlatform platform = platformService.getCurrentPlatform();
        if (platform.getPlatformType().equals(PlatformTypeEnum.type1.getNumber())) {
            saasMerService.reconsider(merId);
        }
    }

    @Override
    public void addStoreContract(Long storeId) throws Exception {
        BsStore store = storeService.getStoreById(storeId);
        if (store == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未找到门店");
        }
    }

}
