package org.jeecg.modules.adweb.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import jakarta.annotation.Resource;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.collections4.CollectionUtils;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.modules.adweb.system.entity.MasterSubAccountRelation;
import org.jeecg.modules.adweb.system.mapper.MasterSubAccountRelationMapper;
import org.jeecg.modules.adweb.system.service.IMasterSubAccountRelationService;
import org.jeecg.modules.adweb.system.service.SysAdwebApi;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class MasterSubAccountRelationServiceImpl extends ServiceImpl<MasterSubAccountRelationMapper, MasterSubAccountRelation> implements IMasterSubAccountRelationService {

    @Resource
    private ISysBaseAPI sysBaseAPI;

    @Resource
    private SysAdwebApi sysAdwebApi;

    @Override
    public String getMasterAccountIdBySub(String subId){
        String masterId = "";
        LambdaQueryWrapper<MasterSubAccountRelation> query = new LambdaQueryWrapper<>();
        query.eq(MasterSubAccountRelation::getSubId,subId);
        query.eq(MasterSubAccountRelation::getStatus,1);
        MasterSubAccountRelation relation = null;
        try {
            relation = this.getBaseMapper().selectOne(query);
            if(relation != null){
                masterId = relation.getMasterId();
            }
        } catch (Exception e){
            log.error("通过子账户ID获取主账户ID失败");
        }
        return masterId;
    }

    @Override
    public String getMasterAccountNameBySub(String subId){
        String masterName = "";
        LambdaQueryWrapper<MasterSubAccountRelation> query = new LambdaQueryWrapper<>();
        query.eq(MasterSubAccountRelation::getSubId,subId);
        query.eq(MasterSubAccountRelation::getStatus,1);
        MasterSubAccountRelation relation = null;
        try {
            relation = this.getBaseMapper().selectOne(query);
            if(relation != null){
                masterName = relation.getMasterName();
            }
        } catch (Exception e){
            log.error("通过子账户ID获取主账户ID失败");
        }
        return masterName;
    }

    @Override
    public List<String> getSubAccountIdByMaster(String masterId){
        List<String> subIdList = null;
        LambdaQueryWrapper<MasterSubAccountRelation> query = new LambdaQueryWrapper<>();
        query.eq(MasterSubAccountRelation::getMasterId,masterId);
        query.eq(MasterSubAccountRelation::getStatus,1);
        List<MasterSubAccountRelation> relationList = null;
        try {
            relationList = this.getBaseMapper().selectList(query);
        } catch (Exception e){
            log.error("通过主账户ID获取子账户ID失败");
        }
        subIdList = relationList.stream().map(MasterSubAccountRelation::getSubId).collect(Collectors.toList());
        return subIdList;
    }

    @Override
    public void insertAccountRelation(MasterSubAccountRelation relation){
        relation.setCreateTime(new Date());
        try {
            if(relation != null){
                this.getBaseMapper().insert(relation);
            }
        }catch (Exception e){
            log.error("保存主账户和子账户ID失败");
        }
    }

    /**
     * 根据uid获取一组uid，包括父账户和子账户
     *
     * @param uid 可以是父账户的uid，也可以是某一个子账户的uid
     * @return 根据uid获取一组uid
     */
    @Override
    public List<String> getUidList(String uid) {
        List<String> defaultList = Arrays.asList(uid);

        String masterId = uid;
        if (sysAdwebApi.isAdwebSubVip()) {
            QueryWrapper<MasterSubAccountRelation> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("sub_id", uid);
            queryWrapper.eq("status", 1);
            List<MasterSubAccountRelation> relations = this.list(queryWrapper);
            if (CollectionUtils.isEmpty(relations)) {
                return defaultList;
            }
            masterId = relations.get(0).getMasterId();
        }

        QueryWrapper<MasterSubAccountRelation> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("master_id", masterId);
        queryWrapper.eq("status", 1);
        List<MasterSubAccountRelation> relations = this.list(queryWrapper);
        if (CollectionUtils.isEmpty(relations)) {
            return defaultList;
        }
        List<String> uidList = relations.stream().map(MasterSubAccountRelation::getSubId).collect(Collectors.toList());
        uidList.add(masterId);
        return uidList;
    }

}
