package com.ruoyi.cms.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.cms.domain.CommercialArea;
import com.ruoyi.cms.domain.CompanyCardCollection;
import com.ruoyi.cms.domain.Statics;
import com.ruoyi.cms.domain.query.Staticsquery;
import com.ruoyi.cms.mapper.CompanyCardCollectionMapper;
import com.ruoyi.cms.mapper.StaticsMapper;
import com.ruoyi.cms.service.ISubwayLineService;
import com.ruoyi.cms.service.StaticsqueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class StaticsqueryServiceImpl extends ServiceImpl<StaticsMapper, Statics> implements StaticsqueryService {
    @Autowired
    private StaticsMapper staticsMapper;
    @Override
    public Map<String, Object> industry(Staticsquery staticsquery) {
        HashMap<String, Object> result = new HashMap<>();
        List<String> query = staticsquery.generateTimeRange();
        for (String time : query) {
            List<Statics> statics = staticsMapper.selectList(Wrappers.<Statics>lambdaQuery().eq(Statics::getType, "分行业/" + staticsquery.getTimeDimension()+"/"+staticsquery.getType()).eq(Statics::getTime, time));
            result.put(time, statics);
        }
        return result;
    }

    private List<String> getQueryTime(Staticsquery staticsquery) {
        return null;
    }

    @Override
    public Map<String, Object> industryArea(Staticsquery staticsquery) {
        HashMap<String, Object> result = new HashMap<>();
        List<String> query = staticsquery.generateTimeRange();
        for (String time : query) {
            List<Statics> statics = staticsMapper.selectList(Wrappers.<Statics>lambdaQuery().eq(Statics::getType, "分区域/" + staticsquery.getTimeDimension()+"/"+staticsquery.getType()).eq(Statics::getTime, time));
            result.put(time, statics);
        }
        return result;
    }

    @Override
    public Map<String, Object> salary(Staticsquery staticsquery) {
        HashMap<String, Object> result = new HashMap<>();
        List<String> query = staticsquery.generateTimeRange();
        for (String time : query) {
            List<Statics> statics = staticsMapper.selectList(Wrappers.<Statics>lambdaQuery().eq(Statics::getType, "分薪资/" + staticsquery.getTimeDimension()+"/"+staticsquery.getType()).eq(Statics::getTime, time));
            result.put(time, statics);
        }
        return result;
    }

    @Override
    public Map<String, Object> education(Staticsquery staticsquery) {
        HashMap<String, Object> result = new HashMap<>();
        List<String> query = staticsquery.generateTimeRange();
        for (String time : query) {
            List<Statics> statics = staticsMapper.selectList(Wrappers.<Statics>lambdaQuery().eq(Statics::getType, "分学历/" + staticsquery.getTimeDimension()+"/"+staticsquery.getType()).eq(Statics::getTime, time));
            result.put(time, statics);
        }
        return result;
    }
    @Override
    public Map<String, Object> workYear(Staticsquery staticsquery) {
        HashMap<String, Object> result = new HashMap<>();
        List<String> query = staticsquery.generateTimeRange();
        for (String time : query) {
            List<Statics> statics = staticsMapper.selectList(Wrappers.<Statics>lambdaQuery().eq(Statics::getType, "分工作年限/" + staticsquery.getTimeDimension()+"/"+staticsquery.getType()).eq(Statics::getTime, time));
            result.put(time, statics);
        }
        return result;
    }
    @Override
    public void industryGen() {
        // 定义岗位分类
        String[] jobCategories = {
                "销售/商务拓展",
                "人事/行政/财务/法务",
                "互联网/通信及硬件",
                "运维/测试",
                "视觉/交互/设计",
                "运营/专业分析",
                "产品/项目/高级管理",
                "市场/品牌/公关",
                "金融/保险",
                "房地产/工程/建筑",
                "物流/采购/供应链",
                "汽车",
                "生产制造/营运管理",
                "农业/能源/环保",
                "医疗/医美/医务",
                "教育/培训/科研",
                "编辑/记者/翻译",
                "影视传媒",
                "商务服务/生活服务",
                "管培生/非企业从业者"
        };

        // 定义时间周期
        String[] timePeriods = {
                "2025-01", "2025-02", "2025-03", "2025-04", "2025-05",
                "2024-11", "2024-12",
                "2024-第一季度", "2024-第二季度", "2024-第三季度", "2024-第四季度",
                "2025-第一季度", "2025-第二季度",
                "2025", "2024", "2023"
        };

        // 所有支持的类型
        Map<String, List<String>> typeMap = new HashMap<>();
        typeMap.put("month", Arrays.asList(
                "分行业/月/岗位发布数量",
                "分行业/月/招聘增长率"
        ));
        typeMap.put("quarter", Arrays.asList(
                "分行业/季度/岗位发布数量",
                "分行业/季度/招聘增长率"
        ));
        typeMap.put("year", Arrays.asList(
                "分行业/年/岗位发布数量",
                "分行业/年/招聘增长率"
        ));

        for (String time : timePeriods) {
            String granularity;

            // 判断时间粒度：月、季度、年
            if (time.matches("\\d{4}-\\d{2}")) {
                granularity = "month";  // 如 "2025-01"
            } else if (time.matches("\\d{4}-第[一二三四]季度")) {
                granularity = "quarter"; // 如 "2024-第一季度"
            } else if (time.matches("\\d{4}")) {
                granularity = "year";   // 如 "2025"
            } else {
                continue; // 非法格式跳过
            }

            List<String> applicableTypes = typeMap.get(granularity);

            for (String type : applicableTypes) {
                for (String category : jobCategories) {
                    Statics stat = new Statics();
                    stat.setTime(time);
                    stat.setType(type);
                    stat.setName(category);

                    // 设置数据值
                    if (type.contains("岗位发布数量")) {
                        int baseValue = 100;
                        if (granularity.equals("quarter")) baseValue = 300;
                        if (granularity.equals("year")) baseValue = 1200;

                        int variation = (int)(baseValue * 0.2 * (Math.random() - 0.5));
                        stat.setData(String.valueOf(baseValue + variation));
                    } else {
                        double baseRate = 0.05;
                        double variation = 0.01 * (Math.random() - 0.5);
                        stat.setData(String.format("%.2f", baseRate + variation));
                    }

                    stat.setDelFlag("0");
                    staticsMapper.insert(stat);
                }
            }
        }
    }

    @Override
    public void salarysalaryGen() {
        // 定义岗位分类
        String[] jobCategories = {
                "3k-5k",
                "5k-8k",
                "10k-15K",
                "15k-20k",
                "20k+"
        };

        // 定义时间周期
        String[] timePeriods = {
                "2025-01", "2025-02", "2025-03", "2025-04", "2025-05",
                "2024-11", "2024-12"
        };

        // 所有支持的类型
        Map<String, List<String>> typeMap = new HashMap<>();
        typeMap.put("month", Arrays.asList(
                "分薪资/月/岗位发布数量"
        ));

        for (String time : timePeriods) {
            String granularity;

            // 判断时间粒度：月、季度、年
            if (time.matches("\\d{4}-\\d{2}")) {
                granularity = "month";  // 如 "2025-01"
            } else if (time.matches("\\d{4}-第[一二三四]季度")) {
                granularity = "quarter"; // 如 "2024-第一季度"
            } else if (time.matches("\\d{4}")) {
                granularity = "year";   // 如 "2025"
            } else {
                continue; // 非法格式跳过
            }

            List<String> applicableTypes = typeMap.get(granularity);

            for (String type : applicableTypes) {
                for (String category : jobCategories) {
                    Statics stat = new Statics();
                    stat.setTime(time);
                    stat.setType(type);
                    stat.setName(category);

                    // 设置数据值
                    if (type.contains("岗位发布数量")) {
                        int baseValue = 694;
                        if (granularity.equals("quarter")) baseValue = 300;
                        if (granularity.equals("year")) baseValue = 1200;

                        int variation = (int)(baseValue * 0.2 * (Math.random() - 0.5));
                        stat.setData(String.valueOf(baseValue + variation));
                    } else {
                        double baseRate = 0.05;
                        double variation = 0.01 * (Math.random() - 0.5);
                        stat.setData(String.format("%.2f", baseRate + variation));
                    }

                    stat.setDelFlag("0");
                    staticsMapper.insert(stat);
                }
            }
        }
    }
    @Override
    public void areaGen() {
        // 定义岗位分类
        String[] jobCategories = {
                "市南区",
                "市北区",
                "李沧区",
                "崂山区",
                "黄岛区",
                "城阳区",
                "即墨区",
                "胶州市",
                "平度市",
                "莱西市"
        };

        // 定义时间周期
        String[] timePeriods = {
                "2025-01", "2025-02", "2025-03", "2025-04", "2025-05",
                "2024-11", "2024-12"
        };

        // 所有支持的类型
        Map<String, List<String>> typeMap = new HashMap<>();
        typeMap.put("month", Arrays.asList(
                "分区域/月/岗位发布数量"
        ));


        for (String time : timePeriods) {
            String granularity;

            // 判断时间粒度：月、季度、年
            if (time.matches("\\d{4}-\\d{2}")) {
                granularity = "month";  // 如 "2025-01"
            } else if (time.matches("\\d{4}-第[一二三四]季度")) {
                granularity = "quarter"; // 如 "2024-第一季度"
            } else if (time.matches("\\d{4}")) {
                granularity = "year";   // 如 "2025"
            } else {
                continue; // 非法格式跳过
            }

            List<String> applicableTypes = typeMap.get(granularity);

            for (String type : applicableTypes) {
                for (String category : jobCategories) {
                    Statics stat = new Statics();
                    stat.setTime(time);
                    stat.setType(type);
                    stat.setName(category);

                    // 设置数据值
                    if (type.contains("岗位发布数量")) {
                        int baseValue = 1000;
                        if (granularity.equals("quarter")) baseValue = 300;
                        if (granularity.equals("year")) baseValue = 1200;

                        int variation = (int)(baseValue * 0.2 * (Math.random() - 0.5));
                        stat.setData(String.valueOf(baseValue + variation));
                    } else {
                        double baseRate = 0.05;
                        double variation = 0.01 * (Math.random() - 0.5);
                        stat.setData(String.format("%.2f", baseRate + variation));
                    }

                    stat.setDelFlag("0");
                    staticsMapper.insert(stat);
                }
            }
        }
    }


    @Override
    public void workYearGen() {
// 定义岗位分类
        String[] jobCategories = {
                "实习生",
                "应届毕业生",
                "1年以下",
                "1-3年",
                "3-5年",
                "5-10年",
                "10年以上",
                "经验不限"
        };

        // 定义时间周期
        String[] timePeriods = {
                "2025-01", "2025-02", "2025-03", "2025-04", "2025-05",
                "2024-11", "2024-12"
        };

        // 所有支持的类型
        Map<String, List<String>> typeMap = new HashMap<>();
        typeMap.put("month", Arrays.asList(
                "分工作年限/月/岗位发布数量"
        ));


        for (String time : timePeriods) {
            String granularity;

            // 判断时间粒度：月、季度、年
            if (time.matches("\\d{4}-\\d{2}")) {
                granularity = "month";  // 如 "2025-01"
            } else if (time.matches("\\d{4}-第[一二三四]季度")) {
                granularity = "quarter"; // 如 "2024-第一季度"
            } else if (time.matches("\\d{4}")) {
                granularity = "year";   // 如 "2025"
            } else {
                continue; // 非法格式跳过
            }

            List<String> applicableTypes = typeMap.get(granularity);

            for (String type : applicableTypes) {
                for (String category : jobCategories) {
                    Statics stat = new Statics();
                    stat.setTime(time);
                    stat.setType(type);
                    stat.setName(category);

                    // 设置数据值
                    if (type.contains("岗位发布数量")) {
                        int baseValue = 1000;
                        if (granularity.equals("quarter")) baseValue = 300;
                        if (granularity.equals("year")) baseValue = 1200;

                        int variation = (int)(baseValue * 0.2 * (Math.random() - 0.5));
                        stat.setData(String.valueOf(baseValue + variation));
                    } else {
                        double baseRate = 0.05;
                        double variation = 0.01 * (Math.random() - 0.5);
                        stat.setData(String.format("%.2f", baseRate + variation));
                    }

                    stat.setDelFlag("0");
                    staticsMapper.insert(stat);
                }
            }
        }
    }

    @Override
    public void educationGen() {
// 定义岗位分类
        String[] jobCategories = {
                "初中及以下",
                "中专/中技",
                "高中",
                "大专",
                "本科",
                "硕士",
                "博士",
                "MBA/EMBA",
                "留学-学士",
                "留学-硕士",
                "留学-博士",
                "不限"
        };

        // 定义时间周期
        String[] timePeriods = {
                "2025-01", "2025-02", "2025-03", "2025-04", "2025-05",
                "2024-11", "2024-12"
        };

        // 所有支持的类型
        Map<String, List<String>> typeMap = new HashMap<>();
        typeMap.put("month", Arrays.asList(
                "分学历年限/月/岗位发布数量"
        ));


        for (String time : timePeriods) {
            String granularity;

            // 判断时间粒度：月、季度、年
            if (time.matches("\\d{4}-\\d{2}")) {
                granularity = "month";  // 如 "2025-01"
            } else if (time.matches("\\d{4}-第[一二三四]季度")) {
                granularity = "quarter"; // 如 "2024-第一季度"
            } else if (time.matches("\\d{4}")) {
                granularity = "year";   // 如 "2025"
            } else {
                continue; // 非法格式跳过
            }

            List<String> applicableTypes = typeMap.get(granularity);

            for (String type : applicableTypes) {
                for (String category : jobCategories) {
                    Statics stat = new Statics();
                    stat.setTime(time);
                    stat.setType(type);
                    stat.setName(category);

                    // 设置数据值
                    if (type.contains("岗位发布数量")) {
                        int baseValue = 1000;
                        if (granularity.equals("quarter")) baseValue = 300;
                        if (granularity.equals("year")) baseValue = 1200;

                        int variation = (int)(baseValue * 0.2 * (Math.random() - 0.5));
                        stat.setData(String.valueOf(baseValue + variation));
                    } else {
                        double baseRate = 0.05;
                        double variation = 0.01 * (Math.random() - 0.5);
                        stat.setData(String.format("%.2f", baseRate + variation));
                    }

                    stat.setDelFlag("0");
                    staticsMapper.insert(stat);
                }
            }
        }
    }


    public void educationSalaryGen() {
        // 定义岗位分类
        String[] jobCategories = {
                "初中及以下",
                "中专/中技",
                "高中",
                "大专",
                "本科",
                "硕士",
                "博士",
                "MBA/EMBA",
                "留学-学士",
                "留学-硕士",
                "留学-博士",
                "不限"
        };
        //薪资类型
        String[] salarys = {
                "3k-5k",
                "5k-8k",
                "10k-15K",
                "15k-20k",
                "20k+"
        };

        // 定义时间周期
        String[] timePeriods = {
                "2025-01", "2025-02", "2025-03", "2025-04", "2025-05",
                "2024-11", "2024-12"
        };

        // 所有支持的类型
        Map<String, List<String>> typeMap = new HashMap<>();
        typeMap.put("month", Arrays.asList(
                "分学历/分薪资/月/岗位发布数量"
        ));


        for (String time : timePeriods) {
            String granularity;

            // 判断时间粒度：月、季度、年
            if (time.matches("\\d{4}-\\d{2}")) {
                granularity = "month";  // 如 "2025-01"
            } else if (time.matches("\\d{4}-第[一二三四]季度")) {
                granularity = "quarter"; // 如 "2024-第一季度"
            } else if (time.matches("\\d{4}")) {
                granularity = "year";   // 如 "2025"
            } else {
                continue; // 非法格式跳过
            }

            List<String> applicableTypes = typeMap.get(granularity);

            for (String type : applicableTypes) {
                for (String category : jobCategories) {
                    for(String salary:salarys){
                        Statics stat = new Statics();
                        stat.setTime(time);
                        stat.setType(type);
                        stat.setName(category+"/"+salary);

                        // 设置数据值
                        if (type.contains("岗位发布数量")) {
                            int baseValue = 1000;
                            if (granularity.equals("quarter")) baseValue = 300;
                            if (granularity.equals("year")) baseValue = 1200;

                            int variation = (int)(baseValue * 0.2 * (Math.random() - 0.5));
                            stat.setData(String.valueOf(baseValue + variation));
                        } else {
                            double baseRate = 0.05;
                            double variation = 0.01 * (Math.random() - 0.5);
                            stat.setData(String.format("%.2f", baseRate + variation));
                        }

                        stat.setDelFlag("0");
                        staticsMapper.insert(stat);
                    }
                }
            }
        }
    }

    @Override
    public Map<String, Object> educationSalary(Staticsquery staticsquery) {
        HashMap<String, Object> result = new HashMap<>();
        List<String> query = staticsquery.generateTimeRange();
        for (String time : query) {
            List<Statics> statics = staticsMapper.selectList(Wrappers.<Statics>lambdaQuery().eq(Statics::getType, "分学历/分薪资/" + staticsquery.getTimeDimension()+"/"+staticsquery.getType()).eq(Statics::getTime, time));
            result.put(time, statics);
        }
        return result;
    }
}
