package com.jeequan.jeepay.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jeequan.jeepay.core.exception.BizException;
import com.jeequan.jeepay.db.entity.AgentConfig;
import com.jeequan.jeepay.service.mapper.AgentConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务商配置表 服务实现类
 * </p>
 *
 * @author [mybatis plus generator]
 * @since 2023-03-28
 */
@Service
public class AgentConfigService extends ServiceImpl<AgentConfigMapper, AgentConfig> {

    @Autowired
    private AgentInfoService agentInfoService;
    @Autowired
    private SysConfigService sysConfigService;

    public AgentConfig getByAgentNoAndConfigKey(String agentNo, String configKey) {
        return baseMapper.selectOne(AgentConfig.gw()
                .eq(AgentConfig::getAgentNo, agentNo)
                .eq(AgentConfig::getConfigKey, configKey)
        );
    }

    public int saveOrUpdateConfig(AgentConfig agentConfig) {

        AgentConfig dbAgentConfig = this.getByAgentNoAndConfigKey(agentConfig.getAgentNo(), agentConfig.getConfigKey());

        int count;
        if (dbAgentConfig != null) {
            count = baseMapper.update(agentConfig, new LambdaUpdateWrapper<AgentConfig>().eq(AgentConfig::getConfigKey, agentConfig.getConfigKey()).eq(AgentConfig::getAgentNo, agentConfig.getAgentNo()));
        } else {
            count = baseMapper.insert(agentConfig);
        }
        return count;
    }

    public int updateBatch(List<AgentConfig> agentConfigList, String agentNo, String groupKey) {
        int count = 0;
        for (AgentConfig agentConfig : agentConfigList) {
            agentConfig.setAgentNo(agentNo);
            agentConfig.setGroupKey(groupKey);
            count += this.saveOrUpdateConfig(agentConfig);
        }
        return count;
    }

    /**
     * 同步更新当前服务商及下级
     **/
    public int updateBatchAll(List<AgentConfig> agentConfigList, String agentNo, String groupKey) {
        int count = 0;
        // 查询当前及下级服务商
        List<String> agentSubList = agentInfoService.queryAllSubAgentNo(agentNo);
        for (String subAgentNo : agentSubList) {
            count += this.updateBatch(agentConfigList, subAgentNo, groupKey);
        }
        return count;
    }

    /**
     * 查询服务商进件是否需要预审核
     */
    public boolean queryAgentApplymentPreAudit(String agentNo) {
        AgentConfig agentConfig = this.getByAgentNoAndConfigKey(agentNo, AgentConfig.KEY_APPLYMENT_AUDIT_MODE);
        if (agentConfig == null || agentConfig.getConfigVal().equals(AgentConfig.KEY_APPLYMENT_AUDIT_MODE_DEF)) {
            return "1".equals(sysConfigService.getById("agentApplymentPreAudit").getConfigVal());
        }
        // 查询当前服务商的进件配置
        AgentConfig agentAuditConfig = this.getByAgentNoAndConfigKey(agentNo, AgentConfig.KEY_AGENT_APPLYMENT_PRE_AUDIT_CONFIG);
        return "1".equals(agentAuditConfig.getConfigVal());
    }

    /**
     * 查询服务商进件是否在允许的时间范围内
     */
    public boolean queryAgentApplymentTimeLimit(String agentNo) {
        AgentConfig agentConfig = this.getByAgentNoAndConfigKey(agentNo, AgentConfig.KEY_APPLYMENT_AUDIT_MODE);
        // 配置为空或配置为默认平台配置
        if (agentConfig == null || agentConfig.getConfigVal().equals(AgentConfig.KEY_APPLYMENT_AUDIT_MODE_DEF)) {
            return !sysConfigService.applyTimeLimit(sysConfigService.getById("agentApplyTimeLimit").getConfigVal());
        }
        // 查询当前服务商的进件配置
        AgentConfig agentTimeConfig = this.getByAgentNoAndConfigKey(agentNo, AgentConfig.KEY_AGENT_APPLY_TIME_LIMIT_CONFIG);
        return !sysConfigService.applyTimeLimit(agentTimeConfig.getConfigVal());
    }

    /**
     * 查询服务商进件允许的时间范围
     */
    public String queryAgentApplymentTime(String agentNo) {
        AgentConfig agentConfig = this.getByAgentNoAndConfigKey(agentNo, AgentConfig.KEY_APPLYMENT_AUDIT_MODE);
        if (agentConfig == null || agentConfig.getConfigVal().equals(AgentConfig.KEY_APPLYMENT_AUDIT_MODE_DEF)) {
            return sysConfigService.applyTimeLimitStr(sysConfigService.getById("agentApplyTimeLimit").getConfigVal());
        }
        // 查询当前服务商的进件配置
        AgentConfig agentTimeConfig = this.getByAgentNoAndConfigKey(agentNo, AgentConfig.KEY_AGENT_APPLY_TIME_LIMIT_CONFIG);
        return sysConfigService.applyTimeLimitStr(agentTimeConfig.getConfigVal());
    }





    public int updateOem(List<AgentConfig> agentConfigList) {
        int count = 0;

        for (AgentConfig agentConfig : agentConfigList) {
            try {
                AgentConfig agentConfigOne = baseMapper.selectOne(AgentConfig.gw()
                        .eq(AgentConfig::getAgentNo, agentConfig.getAgentNo())
                        .eq(AgentConfig::getConfigKey, agentConfig.getConfigKey()));
                if (agentConfigOne != null) {
                    count += baseMapper.update(agentConfig, new LambdaUpdateWrapper<AgentConfig>().eq(AgentConfig::getConfigKey, agentConfig.getConfigKey()).eq(AgentConfig::getAgentNo, agentConfig.getAgentNo()));
                } else {
                    count += baseMapper.insert(agentConfig);
                }
            } catch (Exception e) {
                throw new BizException("更新或新增失败！");
            }
        }

        return count;
    }
}
