package com.citygis.web.service.impl;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.citygis.common.core.domain.AjaxResult;
import com.citygis.web.domain.*;
import com.citygis.web.mapper.TabDatasourceMsgMapper;
import com.citygis.web.service.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 接入数据资源详情 服务实现类
 * </p>
 *
 * @author citygis
 * @since 2025-01-08
 */
@Service
public class TabDatasourceMsgServiceImpl extends ServiceImpl<TabDatasourceMsgMapper, TabDatasourceMsg> implements ITabDatasourceMsgService {


    @Resource
    ITabEtlService tabEtlService;

    @Resource
    ITabKafkaService tabKafkaService;

    @Resource
    ITabKbService tabKbService;

    @Resource
    ITabSftpService tabSftpService;

    @Resource
    ITabUrlService tabUrlService;

    @Resource
    ITabSystemMsgService tabSystemMsgService;

    @Resource
    ITabKafkaSunService tabKafkaSunService;

    @Resource
    ITabUrlSunService tabUrlSunService;

    @Resource
    ITabDatasourceMsgService tabDatasourceMsgService;

    @Override
    @Transactional
    public AjaxResult insertDataSun(DataSun dataSun) {
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);

        List<TabEtl> etl = dataSun.getEtl();
        if (etl != null) {
            etl.forEach(tabEtl -> {
                long etlId = snowflake.nextId();
                tabEtl.setId(etlId);
                tabEtl.setAccessFeedbackId(dataSun.getAccessFeedbackId());
                tabEtlService.save(tabEtl);

                List<TabDatasourceMsg> tabDatasourceMsgs = tabEtl.getTabDatasourceMsgs();
                if (tabDatasourceMsgs != null) {
                    tabDatasourceMsgs.forEach(tabDatasourceMsg -> {
                        tabDatasourceMsg.setId(snowflake.nextId());
                        tabDatasourceMsg.setAccessTypeId(etlId);
                        tabDatasourceMsgService.save(tabDatasourceMsg);
                    });
                }

//                TabSystemMsg tsm = new TabSystemMsg();
//                tsm.setTableEnglishName(tabEtl.getTableEnglishName());
//                tsm.setTableName(tabEtl.getTableName());
//                tsm.setSystemName(tabEtl.getScenario());
//                tsm.setAccessManner("ETL");

//                tabSystemMsgService.save(tsm);
            });
        }

        List<TabKafka> kafka = dataSun.getKafka();
        if (kafka != null) {
            kafka.forEach(tabKafka -> {
                long kafkaId = snowflake.nextId();
                tabKafka.setId(kafkaId);
                tabKafka.setAccessFeedbackId(dataSun.getAccessFeedbackId());
                tabKafkaService.save(tabKafka);

                List<TabKafkaSun> keySuns = tabKafka.getTabKafkaKeySuns();
                if (keySuns != null) {
                    keySuns.forEach(tabKafkaSun -> {
                        tabKafkaSun.setId(snowflake.nextId());
                        tabKafkaSun.setKafkaId(kafkaId);
                        tabKafkaSun.setType("key");
                        tabKafkaSunService.save(tabKafkaSun);
                    });
                }

                List<TabKafkaSun> valueSuns = tabKafka.getTabKafkaValueSuns();
                if (valueSuns != null) {
                    valueSuns.forEach(tabKafkaSun -> {
                        tabKafkaSun.setId(snowflake.nextId());
                        tabKafkaSun.setKafkaId(kafkaId);
                        tabKafkaSun.setType("value");
                        tabKafkaSunService.save(tabKafkaSun);
                    });
                }

                List<TabKafkaSun> dataSuns = tabKafka.getTabKafkaDataSuns();
                if (dataSuns != null) {
                    dataSuns.forEach(tabKafkaSun -> {
                        tabKafkaSun.setId(snowflake.nextId());
                        tabKafkaSun.setKafkaId(kafkaId);
                        tabKafkaSun.setType("data");
                        tabKafkaSunService.save(tabKafkaSun);
                    });
                }
            });
        }

        List<TabKb> kb = dataSun.getKb();
        if (kb != null) {
            kb.forEach(tabKb -> {
                long kbId = snowflake.nextId();
                tabKb.setId(kbId);
                tabKb.setAccessFeedbackId(dataSun.getAccessFeedbackId());
                tabKbService.save(tabKb);

                List<TabDatasourceMsg> tabDatasourceMsgs = tabKb.getTabDatasourceMsgs();
                if (tabDatasourceMsgs != null) {
                    tabDatasourceMsgs.forEach(tabDatasourceMsg -> {
                        tabDatasourceMsg.setId(snowflake.nextId());
                        tabDatasourceMsg.setAccessTypeId(kbId);
                        tabDatasourceMsgService.save(tabDatasourceMsg);
                    });
                }
            });
        }

        List<TabSftp> sftp = dataSun.getSftp();
        if (sftp != null) {
            sftp.forEach(tabSftp -> {
                tabSftp.setId(snowflake.nextId());
                tabSftp.setAccessFeedbackId(dataSun.getAccessFeedbackId());
                tabSftpService.save(tabSftp);
            });
        }

        List<TabUrl> url = dataSun.getUrl();
        if (url != null) {
            url.forEach(tabUrl -> {
                long urlId = snowflake.nextId();
                tabUrl.setAccessFeedbackId(dataSun.getAccessFeedbackId());
                tabUrl.setId(urlId);
                tabUrlService.save(tabUrl);

                tabUrl.getTabEtlHeaderSuns().forEach(tabUrlSun -> {
                    tabUrlSun.setId(snowflake.nextId());
                    tabUrlSun.setUrlId(urlId);
                    tabUrlSun.setType("header");
                    tabUrlSunService.save(tabUrlSun);
                });

                tabUrl.getTabEtlBodySuns().forEach(tabUrlSun -> {
                    tabUrlSun.setId(snowflake.nextId());
                    tabUrlSun.setUrlId(urlId);
                    tabUrlSun.setType("body");
                    tabUrlSunService.save(tabUrlSun);
                });

                tabUrl.getTabEtlQuerySuns().forEach(tabUrlSun -> {
                    tabUrlSun.setId(snowflake.nextId());
                    tabUrlSun.setUrlId(urlId);
                    tabUrlSun.setType("query");
                    tabUrlSunService.save(tabUrlSun);
                });

                tabUrl.getTabEtlResponseSuns().forEach(tabUrlSun -> {
                    tabUrlSun.setId(snowflake.nextId());
                    tabUrlSun.setUrlId(urlId);
                    tabUrlSun.setType("response");
                    tabUrlSunService.save(tabUrlSun);
                });

                tabUrl.getTabEtlDataSuns().forEach(tabUrlSun -> {
                    tabUrlSun.setId(snowflake.nextId());
                    tabUrlSun.setUrlId(urlId);
                    tabUrlSun.setType("data");
                    tabUrlSunService.save(tabUrlSun);
                });
            });
        }

        return AjaxResult.success("添加成功");
    }

    @Override
    @Transactional
    public AjaxResult updateDataSun(DataSun dataSun) {
        Snowflake snowflake = IdUtil.getSnowflake(1, 1);

        List<TabEtl> etl = dataSun.getEtl();
        if (etl != null) {
            etl.forEach(tabEtl -> {
                tabEtl.setAccessFeedbackId(dataSun.getAccessFeedbackId());
                tabEtlService.saveOrUpdate(tabEtl);

                tabEtl.getTabDatasourceMsgs().forEach(tabDatasourceMsg -> {
                    LambdaQueryWrapper<TabDatasourceMsg> kql = new LambdaQueryWrapper<>();
                    kql.eq(TabDatasourceMsg::getId, tabDatasourceMsg.getId());
                    TabDatasourceMsg one = tabDatasourceMsgService.getOne(kql);
                    if (one != null) {
                        tabDatasourceMsgService.updateById(one);
                    } else {
                        tabDatasourceMsg.setId(snowflake.nextId());
                        tabDatasourceMsgService.save(tabDatasourceMsg);
                    }
                });
            });
        }

        List<TabKafka> kafka = dataSun.getKafka();
        if (kafka != null) {
            kafka.forEach(tabKafka -> {
                tabKafka.setAccessFeedbackId(dataSun.getAccessFeedbackId());
                tabKafkaService.saveOrUpdate(tabKafka);

                tabKafka.getTabKafkaKeySuns().forEach(tabKafkaSun -> {
                    LambdaQueryWrapper<TabKafkaSun> kql = new LambdaQueryWrapper<>();
                    kql.eq(TabKafkaSun::getId, tabKafkaSun.getId());
                    TabKafkaSun one = tabKafkaSunService.getOne(kql);
                    if (one != null) {
                        tabKafkaSunService.updateById(one);
                    } else {
                        tabKafkaSun.setId(snowflake.nextId());
                        tabKafkaSunService.save(tabKafkaSun);
                    }
                });

                tabKafka.getTabKafkaValueSuns().forEach(tabKafkaSun -> {
                    LambdaQueryWrapper<TabKafkaSun> kql = new LambdaQueryWrapper<>();
                    kql.eq(TabKafkaSun::getId, tabKafkaSun.getId());
                    TabKafkaSun one = tabKafkaSunService.getOne(kql);
                    if (one != null) {
                        tabKafkaSunService.updateById(one);
                    } else {
                        tabKafkaSun.setId(snowflake.nextId());
                        tabKafkaSunService.save(tabKafkaSun);
                    }
                });

                tabKafka.getTabKafkaDataSuns().forEach(tabKafkaSun -> {
                    LambdaQueryWrapper<TabKafkaSun> kql = new LambdaQueryWrapper<>();
                    kql.eq(TabKafkaSun::getId, tabKafkaSun.getId());
                    TabKafkaSun one = tabKafkaSunService.getOne(kql);
                    if (one != null) {
                        tabKafkaSunService.updateById(one);
                    } else {
                        tabKafkaSun.setId(snowflake.nextId());
                        tabKafkaSunService.save(tabKafkaSun);
                    }
                });

            });
        }

        List<TabKb> kb = dataSun.getKb();
        if (kb != null) {
            kb.forEach(tabKb -> {
                tabKb.setAccessFeedbackId(dataSun.getAccessFeedbackId());
                tabKbService.saveOrUpdate(tabKb);

                tabKb.getTabDatasourceMsgs().forEach(tabDatasourceMsg -> {
                    LambdaQueryWrapper<TabDatasourceMsg> kql = new LambdaQueryWrapper<>();
                    kql.eq(TabDatasourceMsg::getId, tabDatasourceMsg.getId());
                    TabDatasourceMsg one = tabDatasourceMsgService.getOne(kql);
                    if (one != null) {
                        tabDatasourceMsgService.updateById(one);
                    } else {
                        tabDatasourceMsg.setId(snowflake.nextId());
                        tabDatasourceMsgService.save(tabDatasourceMsg);
                    }
                });
            });
        }

        List<TabSftp> sftp = dataSun.getSftp();
        if (sftp != null) {
            sftp.forEach(tabSftp -> {
                tabSftp.setAccessFeedbackId(dataSun.getAccessFeedbackId());
                tabSftpService.saveOrUpdate(tabSftp);
            });
        }

        List<TabUrl> url = dataSun.getUrl();
        if (url != null) {
            url.forEach(tabUrl -> {
                tabUrl.setAccessFeedbackId(dataSun.getAccessFeedbackId());
                tabUrlService.saveOrUpdate(tabUrl);

                tabUrl.getTabEtlHeaderSuns().forEach(tabUrlSun -> {
                    LambdaQueryWrapper<TabUrlSun> kql = new LambdaQueryWrapper<>();
                    kql.eq(TabUrlSun::getId, tabUrlSun.getId());
                    TabUrlSun one = tabUrlSunService.getOne(kql);
                    if (one != null) {
                        tabUrlSunService.updateById(one);
                    } else {
                        tabUrlSun.setId(snowflake.nextId());
                        tabUrlSunService.save(tabUrlSun);
                    }
                });

                tabUrl.getTabEtlBodySuns().forEach(tabUrlSun -> {
                    LambdaQueryWrapper<TabUrlSun> kql = new LambdaQueryWrapper<>();
                    kql.eq(TabUrlSun::getId, tabUrlSun.getId());
                    TabUrlSun one = tabUrlSunService.getOne(kql);
                    if (one != null) {
                        tabUrlSunService.updateById(one);
                    } else {
                        tabUrlSun.setId(snowflake.nextId());
                        tabUrlSunService.save(tabUrlSun);
                    }
                });

                tabUrl.getTabEtlQuerySuns().forEach(tabUrlSun -> {
                    LambdaQueryWrapper<TabUrlSun> kql = new LambdaQueryWrapper<>();
                    kql.eq(TabUrlSun::getId, tabUrlSun.getId());
                    TabUrlSun one = tabUrlSunService.getOne(kql);
                    if (one != null) {
                        tabUrlSunService.updateById(one);
                    } else {
                        tabUrlSun.setId(snowflake.nextId());
                        tabUrlSunService.save(tabUrlSun);
                    }
                });

                tabUrl.getTabEtlResponseSuns().forEach(tabUrlSun -> {
                    LambdaQueryWrapper<TabUrlSun> kql = new LambdaQueryWrapper<>();
                    kql.eq(TabUrlSun::getId, tabUrlSun.getId());
                    TabUrlSun one = tabUrlSunService.getOne(kql);
                    if (one != null) {
                        tabUrlSunService.updateById(one);
                    } else {
                        tabUrlSun.setId(snowflake.nextId());
                        tabUrlSunService.save(tabUrlSun);
                    }
                });

                tabUrl.getTabEtlDataSuns().forEach(tabUrlSun -> {
                    LambdaQueryWrapper<TabUrlSun> kql = new LambdaQueryWrapper<>();
                    kql.eq(TabUrlSun::getId, tabUrlSun.getId());
                    TabUrlSun one = tabUrlSunService.getOne(kql);
                    if (one != null) {
                        tabUrlSunService.updateById(one);
                    } else {
                        tabUrlSun.setId(snowflake.nextId());
                        tabUrlSunService.save(tabUrlSun);
                    }
                });

            });
        }

        return AjaxResult.success("修改成功");

    }

    @Override
    @Transactional
    public AjaxResult deleteDataSun(Long id) {
        TabEtl tabEtl = new TabEtl();
        tabEtl.setId(id);
        tabEtlService.removeById(tabEtl);

        LambdaQueryWrapper<TabDatasourceMsg> ql1 = new LambdaQueryWrapper<>();
        ql1.eq(TabDatasourceMsg::getAccessTypeId, id);
        tabDatasourceMsgService.remove(ql1);

        TabKafka tabKafka = new TabKafka();
        tabKafka.setId(id);
        tabKafkaService.removeById(tabKafka);

        LambdaQueryWrapper<TabKafkaSun> ql = new LambdaQueryWrapper<>();
        ql.eq(TabKafkaSun::getKafkaId, id);
        tabKafkaSunService.remove(ql);

        TabKb tabKb = new TabKb();
        tabKb.setId(id);
        tabKbService.removeById(tabKb);

        LambdaQueryWrapper<TabDatasourceMsg> ql2 = new LambdaQueryWrapper<>();
        ql2.eq(TabDatasourceMsg::getAccessTypeId, id);
        tabDatasourceMsgService.remove(ql2);

        TabSftp tabSftp = new TabSftp();
        tabSftp.setId(id);
        tabSftpService.removeById(tabSftp);

        TabUrl tabUrl = new TabUrl();
        tabUrl.setId(id);
        tabUrlService.removeById(tabUrl);

        LambdaQueryWrapper<TabUrlSun> uql = new LambdaQueryWrapper<>();
        uql.eq(TabUrlSun::getUrlId, id);
        tabUrlSunService.remove(uql);

        return AjaxResult.success("删除成功");

    }

    @Override
    public AjaxResult getDataSun(Long id) {
        LambdaQueryWrapper<TabEtl> ql = new LambdaQueryWrapper<>();
        ql.eq(TabEtl::getAccessFeedbackId, id);
        List<TabEtl> tabEtls = tabEtlService.list(ql);

        tabEtls.forEach(tabEtl -> {
            LambdaQueryWrapper<TabDatasourceMsg> eql = new LambdaQueryWrapper<>();
            eql.eq(TabDatasourceMsg::getAccessTypeId, tabEtl.getId());
            List<TabDatasourceMsg> tabDatasourceMsgs = tabDatasourceMsgService.list(eql);
            tabEtl.setTabDatasourceMsgs(tabDatasourceMsgs);
        });

        LambdaQueryWrapper<TabKafka> kql = new LambdaQueryWrapper<>();
        kql.eq(TabKafka::getAccessFeedbackId, id);
        List<TabKafka> tabKafkas = tabKafkaService.list(kql);

        tabKafkas.forEach(tabKafka -> {
            LambdaQueryWrapper<TabKafkaSun> ks = new LambdaQueryWrapper<>();
            ks.eq(TabKafkaSun::getKafkaId, tabKafka.getId());
            ks.eq(TabKafkaSun::getType, "key");
            List<TabKafkaSun> tabKafkaSuns = tabKafkaSunService.list(ks);
            tabKafka.setTabKafkaKeySuns(tabKafkaSuns);

            LambdaQueryWrapper<TabKafkaSun> vs = new LambdaQueryWrapper<>();
            vs.eq(TabKafkaSun::getKafkaId, tabKafka.getId());
            vs.eq(TabKafkaSun::getType, "value");
            List<TabKafkaSun> tabKafkaValueSuns = tabKafkaSunService.list(vs);
            tabKafka.setTabKafkaValueSuns(tabKafkaValueSuns);

            LambdaQueryWrapper<TabKafkaSun> ds = new LambdaQueryWrapper<>();
            ds.eq(TabKafkaSun::getKafkaId, tabKafka.getId());
            ds.eq(TabKafkaSun::getType, "data");
            List<TabKafkaSun> tabKafkaDataSuns = tabKafkaSunService.list(ds);
            tabKafka.setTabKafkaDataSuns(tabKafkaDataSuns);
        });


        LambdaQueryWrapper<TabKb> kbql = new LambdaQueryWrapper<>();
        kbql.eq(TabKb::getAccessFeedbackId, id);
        List<TabKb> tabKbs = tabKbService.list(kbql);

        tabKbs.forEach(tabKb -> {
            LambdaQueryWrapper<TabDatasourceMsg> bql = new LambdaQueryWrapper<>();
            bql.eq(TabDatasourceMsg::getAccessTypeId, tabKb.getId());
            List<TabDatasourceMsg> tabDatasourceMsgs = tabDatasourceMsgService.list(bql);
            tabKb.setTabDatasourceMsgs(tabDatasourceMsgs);
        });

        LambdaQueryWrapper<TabSftp> sftpql = new LambdaQueryWrapper<>();
        sftpql.eq(TabSftp::getAccessFeedbackId, id);
        List<TabSftp> tabSftps = tabSftpService.list(sftpql);

        LambdaQueryWrapper<TabUrl> urlql = new LambdaQueryWrapper<>();
        urlql.eq(TabUrl::getAccessFeedbackId, id);
        List<TabUrl> tabUrls = tabUrlService.list(urlql);

        tabUrls.forEach(tabUrl -> {
            LambdaQueryWrapper<TabUrlSun> hql = new LambdaQueryWrapper<>();
            hql.eq(TabUrlSun::getUrlId, tabUrl.getId());
            hql.eq(TabUrlSun::getType, "header");
            List<TabUrlSun> tabUrlSunS = tabUrlSunService.list(hql);
            tabUrl.setTabEtlHeaderSuns(tabUrlSunS);

            LambdaQueryWrapper<TabUrlSun> bql = new LambdaQueryWrapper<>();
            bql.eq(TabUrlSun::getUrlId, tabUrl.getId());
            bql.eq(TabUrlSun::getType, "body");
            List<TabUrlSun> tabUrlSunS1 = tabUrlSunService.list(bql);
            tabUrl.setTabEtlBodySuns(tabUrlSunS1);

            LambdaQueryWrapper<TabUrlSun> qql = new LambdaQueryWrapper<>();
            qql.eq(TabUrlSun::getUrlId, tabUrl.getId());
            qql.eq(TabUrlSun::getType, "query");
            List<TabUrlSun> tabUrlSunS2 = tabUrlSunService.list(qql);
            tabUrl.setTabEtlQuerySuns(tabUrlSunS2);

            LambdaQueryWrapper<TabUrlSun> rql = new LambdaQueryWrapper<>();
            rql.eq(TabUrlSun::getUrlId, tabUrl.getId());
            rql.eq(TabUrlSun::getType, "response");
            List<TabUrlSun> tabUrlSunS3 = tabUrlSunService.list(rql);
            tabUrl.setTabEtlResponseSuns(tabUrlSunS3);

            LambdaQueryWrapper<TabUrlSun> dql = new LambdaQueryWrapper<>();
            dql.eq(TabUrlSun::getUrlId, tabUrl.getId());
            dql.eq(TabUrlSun::getType, "data");
            List<TabUrlSun> tabUrlSunS4 = tabUrlSunService.list(dql);
            tabUrl.setTabEtlDataSuns(tabUrlSunS4);
        });

        DataSun dataSun = new DataSun();
        dataSun.setEtl(tabEtls);
        dataSun.setKafka(tabKafkas);
        dataSun.setKb(tabKbs);
        dataSun.setSftp(tabSftps);
        dataSun.setUrl(tabUrls);

        return AjaxResult.success(dataSun);
    }

    @Override
    public AjaxResult deleteKafkaSun(Long id) {
        boolean b = tabKafkaSunService.removeById(id);
        if (b) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.success("删除失败");
        }
    }

    @Override
    public AjaxResult deleteUrlSun(Long id) {
        boolean b = tabUrlSunService.removeById(id);
        if (b) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.success("删除失败");
        }
    }

    @Override
    public AjaxResult deleteEtlAndKbSun(Long id) {
        boolean b = tabDatasourceMsgService.removeById(id);
        if (b) {
            return AjaxResult.success("删除成功");
        } else {
            return AjaxResult.success("删除失败");
        }
    }


}
