package org.xyzh.system.module.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.xyzh.common.core.page.PageParam;
import org.xyzh.common.dto.system.TbSysModule;
import org.xyzh.common.utils.IDUtils;
import org.xyzh.common.core.domain.ResultDomain;
import org.xyzh.system.mapper.ModuleMapper;
import org.xyzh.system.mapper.PermissionMapper;
import org.xyzh.api.system.module.ModuleService;

import java.util.Date;
import java.util.List;

/**
 * @description 系统模块服务实现类
 * @filename ModuleServiceImpl.java
 * @author yslg
 * @copyright xyzh
 * @since 2025-10-16
 */
@Service
public class ModuleServiceImpl implements ModuleService {

    @Autowired
    private ModuleMapper moduleMapper;

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public ResultDomain<TbSysModule> getModuleList(TbSysModule filter) {
        try {
            List<TbSysModule> modules = moduleMapper.selectModules(filter);
            ResultDomain<TbSysModule> result = new ResultDomain<>();
            result.success("查询成功", modules);
            return result;
        } catch (Exception e) {
            ResultDomain<TbSysModule> result = new ResultDomain<>();
            result.fail("查询模块列表失败：" + e.getMessage());
            return result;
        }
    }

    @Override
    public ResultDomain<TbSysModule> getModuleById(String moduleID) {
        try {
            TbSysModule module = moduleMapper.selectByModuleID(moduleID);
            if (module == null) {
                ResultDomain<TbSysModule> result = new ResultDomain<>();
                result.fail("模块不存在");
                return result;
            }
            ResultDomain<TbSysModule> result = new ResultDomain<>();
            result.success("查询成功", module);
            return result;
        } catch (Exception e) {
            ResultDomain<TbSysModule> result = new ResultDomain<>();
            result.fail("查询模块信息失败：" + e.getMessage());
            return result;
        }
    }

    @Override
    public ResultDomain<TbSysModule> getModuleByCode(String code) {
        try {
            TbSysModule module = moduleMapper.selectByCode(code);
            if (module == null) {
                ResultDomain<TbSysModule> result = new ResultDomain<>();
                result.fail("模块不存在");
                return result;
            }
            ResultDomain<TbSysModule> result = new ResultDomain<>();
            result.success("查询成功", module);
            return result;
        } catch (Exception e) {
            ResultDomain<TbSysModule> result = new ResultDomain<>();
            result.fail("查询模块信息失败：" + e.getMessage());
            return result;
        }
    }

    @Override
    public ResultDomain<TbSysModule> getActiveModules() {
        try {
            List<TbSysModule> modules = moduleMapper.selectActiveModules();
            ResultDomain<TbSysModule> result = new ResultDomain<>();
            result.success("查询成功", modules);
            return result;
        } catch (Exception e) {
            ResultDomain<TbSysModule> result = new ResultDomain<>();
            result.fail("查询启用模块列表失败：" + e.getMessage());
            return result;
        }
    }

    @Override
    @Transactional
    public ResultDomain<TbSysModule> createModule(TbSysModule module) {
        try {
            // 检查模块代码是否已存在
            int count = moduleMapper.countByCode(module.getCode(), null);
            if (count > 0) {
                ResultDomain<TbSysModule> result = new ResultDomain<>();
                result.fail("模块代码已存在");
                return result;
            }

            // 设置ID和创建时间
            module.setID(IDUtils.generateID());
            module.setModuleID(IDUtils.generateID());
            module.setCreateTime(new Date());
            module.setUpdateTime(new Date());
            module.setDeleted(false);

            int result = moduleMapper.insertModule(module);
            if (result > 0) {
                ResultDomain<TbSysModule> resultDomain = new ResultDomain<>();
                resultDomain.success("创建成功", module);
                return resultDomain;
            } else {
                ResultDomain<TbSysModule> resultDomain = new ResultDomain<>();
                resultDomain.fail("创建模块失败");
                return resultDomain;
            }
        } catch (Exception e) {
            ResultDomain<TbSysModule> result = new ResultDomain<>();
            result.fail("创建模块失败：" + e.getMessage());
            return result;
        }
    }

    @Override
    @Transactional
    public ResultDomain<TbSysModule> updateModule(TbSysModule module) {
        try {
            // 检查模块是否存在
            TbSysModule existingModule = moduleMapper.selectByModuleID(module.getModuleID());
            if (existingModule == null) {
                ResultDomain<TbSysModule> result = new ResultDomain<>();
                result.fail("模块不存在");
                return result;
            }

            // 检查模块代码是否已存在（排除自身）
            int count = moduleMapper.countByCode(module.getCode(), existingModule.getID());
            if (count > 0) {
                ResultDomain<TbSysModule> result = new ResultDomain<>();
                result.fail("模块代码已存在");
                return result;
            }

            // 设置更新时间和ID
            module.setID(existingModule.getID());
            module.setUpdateTime(new Date());

            int result = moduleMapper.updateModule(module);
            if (result > 0) {
                ResultDomain<TbSysModule> resultDomain = new ResultDomain<>();
                resultDomain.success("更新成功", module);
                return resultDomain;
            } else {
                ResultDomain<TbSysModule> resultDomain = new ResultDomain<>();
                resultDomain.fail("更新模块失败");
                return resultDomain;
            }
        } catch (Exception e) {
            ResultDomain<TbSysModule> result = new ResultDomain<>();
            result.fail("更新模块失败：" + e.getMessage());
            return result;
        }
    }

    @Override
    @Transactional
    public ResultDomain<Boolean> deleteModule(String moduleID) {
        try {
            TbSysModule module = moduleMapper.selectByModuleID(moduleID);
            if (module == null) {
                ResultDomain<Boolean> result = new ResultDomain<>();
                result.fail("模块不存在");
                return result;
            }

            module.setDeleteTime(new Date());
            int result = moduleMapper.deleteModule(module);
            if (result > 0) {
                ResultDomain<Boolean> resultDomain = new ResultDomain<>();
                resultDomain.success("删除成功", true);
                return resultDomain;
            } else {
                ResultDomain<Boolean> resultDomain = new ResultDomain<>();
                resultDomain.fail("删除模块失败");
                return resultDomain;
            }
        } catch (Exception e) {
            ResultDomain<Boolean> result = new ResultDomain<>();
            result.fail("删除模块失败：" + e.getMessage());
            return result;
        }
    }

    @Override
    @Transactional
    public ResultDomain<Boolean> batchDeleteModules(List<String> moduleIDs) {
        try {
            int result = moduleMapper.batchDeleteModules(moduleIDs);
            if (result > 0) {
                ResultDomain<Boolean> resultDomain = new ResultDomain<>();
                resultDomain.success("批量删除成功", true);
                return resultDomain;
            } else {
                ResultDomain<Boolean> resultDomain = new ResultDomain<>();
                resultDomain.fail("批量删除模块失败");
                return resultDomain;
            }
        } catch (Exception e) {
            ResultDomain<Boolean> result = new ResultDomain<>();
            result.fail("批量删除模块失败：" + e.getMessage());
            return result;
        }
    }

    @Override
    @Transactional
    public ResultDomain<Boolean> updateModuleStatus(String moduleID, Integer status) {
        try {
            int result = moduleMapper.updateModuleStatus(moduleID, status);
            if (result > 0) {
                ResultDomain<Boolean> resultDomain = new ResultDomain<>();
                resultDomain.success("更新状态成功", true);
                return resultDomain;
            } else {
                ResultDomain<Boolean> resultDomain = new ResultDomain<>();
                resultDomain.fail("更新模块状态失败");
                return resultDomain;
            }
        } catch (Exception e) {
            ResultDomain<Boolean> result = new ResultDomain<>();
            result.fail("更新模块状态失败：" + e.getMessage());
            return result;
        }
    }

    @Override
    @Transactional
    public ResultDomain<Boolean> updateModuleOrder(String moduleID, Integer orderNum) {
        try {
            int result = moduleMapper.updateModuleOrder(moduleID, orderNum);
            if (result > 0) {
                ResultDomain<Boolean> resultDomain = new ResultDomain<>();
                resultDomain.success("更新排序成功", true);
                return resultDomain;
            } else {
                ResultDomain<Boolean> resultDomain = new ResultDomain<>();
                resultDomain.fail("更新模块排序失败");
                return resultDomain;
            }
        } catch (Exception e) {
            ResultDomain<Boolean> result = new ResultDomain<>();
            result.fail("更新模块排序失败：" + e.getMessage());
            return result;
        }
    }

    @Override
    public ResultDomain<TbSysModule> getModuleListPage(TbSysModule filter, PageParam pageParam) {
        try {
            List<TbSysModule> modules = moduleMapper.selectModulesPage(filter, pageParam);
            ResultDomain<TbSysModule> result = new ResultDomain<>();
            result.success("查询成功", modules);
            return result;
        } catch (Exception e) {
            ResultDomain<TbSysModule> result = new ResultDomain<>();
            result.fail("分页查询模块列表失败：" + e.getMessage());
            return result;
        }
    }

    @Override
    public ResultDomain<Long> countModules(TbSysModule filter) {
        try {
            long count = moduleMapper.countModules(filter);
            ResultDomain<Long> result = new ResultDomain<>();
            result.success("统计成功", count);
            return result;
        } catch (Exception e) {
            ResultDomain<Long> result = new ResultDomain<>();
            result.fail("统计模块数量失败：" + e.getMessage());
            return result;
        }
    }

    @Override
    public ResultDomain<Boolean> checkModuleCodeExists(String code, String excludeID) {
        try {
            int count = moduleMapper.countByCode(code, excludeID);
            ResultDomain<Boolean> result = new ResultDomain<>();
            result.success("检查成功", count > 0);
            return result;
        } catch (Exception e) {
            ResultDomain<Boolean> result = new ResultDomain<>();
            result.fail("检查模块代码失败：" + e.getMessage());
            return result;
        }
    }

    @Override
    @Transactional
    public ResultDomain<Boolean> bindPermissionsToModule(String moduleID, List<String> permissionIds) {
        try {
            int result = permissionMapper.bindPermissionsToModule(moduleID, permissionIds);
            if (result > 0) {
                ResultDomain<Boolean> resultDomain = new ResultDomain<>();
                resultDomain.success("绑定成功", true);
                return resultDomain;
            } else {
                ResultDomain<Boolean> resultDomain = new ResultDomain<>();
                resultDomain.fail("绑定权限到模块失败");
                return resultDomain;
            }
        } catch (Exception e) {
            ResultDomain<Boolean> result = new ResultDomain<>();
            result.fail("绑定权限到模块失败：" + e.getMessage());
            return result;
        }
    }

    @Override
    @Transactional
    public ResultDomain<Boolean> unbindPermissionsFromModule(String moduleID, List<String> permissionIds) {
        try {
            int result = permissionMapper.unbindPermissionsFromModule(moduleID, permissionIds);
            if (result > 0) {
                ResultDomain<Boolean> resultDomain = new ResultDomain<>();
                resultDomain.success("解绑成功", true);
                return resultDomain;
            } else {
                ResultDomain<Boolean> resultDomain = new ResultDomain<>();
                resultDomain.fail("解绑模块权限失败");
                return resultDomain;
            }
        } catch (Exception e) {
            ResultDomain<Boolean> result = new ResultDomain<>();
            result.fail("解绑模块权限失败：" + e.getMessage());
            return result;
        }
    }

    @Override
    public ResultDomain<List<String>> getModulePermissions(String moduleID) {
        try {
            List<String> permissionIds = permissionMapper.getModulePermissionIds(moduleID);
            ResultDomain<List<String>> result = new ResultDomain<>();
            result.success("查询成功", permissionIds);
            return result;
        } catch (Exception e) {
            ResultDomain<List<String>> result = new ResultDomain<>();
            result.fail("获取模块权限失败：" + e.getMessage());
            return result;
        }
    }

    @Override
    @Transactional
    public ResultDomain<Boolean> batchBindPermissionsToModule(String moduleID, List<String> permissionIds) {
        try {
            int result = permissionMapper.bindPermissionsToModule(moduleID, permissionIds);
            if (result > 0) {
                ResultDomain<Boolean> resultDomain = new ResultDomain<>();
                resultDomain.success("批量绑定成功", true);
                return resultDomain;
            } else {
                ResultDomain<Boolean> resultDomain = new ResultDomain<>();
                resultDomain.fail("批量绑定权限到模块失败");
                return resultDomain;
            }
        } catch (Exception e) {
            ResultDomain<Boolean> result = new ResultDomain<>();
            result.fail("批量绑定权限到模块失败：" + e.getMessage());
            return result;
        }
    }

    @Override
    @Transactional
    public ResultDomain<Boolean> batchUnbindPermissionsFromModule(String moduleID, List<String> permissionIds) {
        try {
            int result = permissionMapper.unbindPermissionsFromModule(moduleID, permissionIds);
            if (result > 0) {
                ResultDomain<Boolean> resultDomain = new ResultDomain<>();
                resultDomain.success("批量解绑成功", true);
                return resultDomain;
            } else {
                ResultDomain<Boolean> resultDomain = new ResultDomain<>();
                resultDomain.fail("批量解绑模块权限失败");
                return resultDomain;
            }
        } catch (Exception e) {
            ResultDomain<Boolean> result = new ResultDomain<>();
            result.fail("批量解绑模块权限失败：" + e.getMessage());
            return result;
        }
    }
}