package com.ygtx.gxt.service.impl;

import java.util.*;

import com.ygtx.common.core.domain.entity.SysDept;
import com.ygtx.common.core.domain.entity.SysRole;
import com.ygtx.common.core.domain.entity.SysUser;
import com.ygtx.common.core.domain.model.LoginUser;
import com.ygtx.common.utils.SecurityUtils;
import com.ygtx.common.utils.StringUtils;
import com.ygtx.framework.aspectj.DataScopeAspect;
import com.ygtx.gxt.domain.GxtRepairOrder;
import com.ygtx.gxt.domain.GxtSafeOperationReward;
import com.ygtx.gxt.domain.GxtWorkOrder;
import com.ygtx.gxt.mapper.GxtRepairOrderMapper;
import com.ygtx.gxt.mapper.GxtSafeOperationRewardMapper;
import com.ygtx.gxt.mapper.GxtWorkOrderMapper;
import com.ygtx.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ygtx.gxt.mapper.GxtMonthRuntimeMapper;
import com.ygtx.gxt.domain.GxtMonthRuntime;
import com.ygtx.gxt.service.IGxtMonthRuntimeService;

/**
 * 月度场站运行天数工单Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-11-12
 */
@Service
public class GxtMonthRuntimeServiceImpl implements IGxtMonthRuntimeService 
{
    @Autowired
    private GxtMonthRuntimeMapper gxtMonthRuntimeMapper;

    @Autowired
    private GxtWorkOrderMapper gxtWorkOrderMapper;

    @Autowired
    private GxtSafeOperationRewardMapper safeOperationRewardMapper;

    @Autowired
    private ISysConfigService configService;

    /**
     * 查询月度场站运行天数工单
     * 
     * @param id 月度场站运行天数工单主键
     * @return 月度场站运行天数工单
     */
    @Override
    public GxtMonthRuntime selectGxtMonthRuntimeById(Long id)
    {
        return gxtMonthRuntimeMapper.selectGxtMonthRuntimeById(id);
    }

    /**
     * 查询月度场站运行天数工单列表
     * 
     * @param gxtMonthRuntime 月度场站运行天数工单
     * @return 月度场站运行天数工单
     */
    @Override
    public List<GxtMonthRuntime> selectGxtMonthRuntimeList(GxtMonthRuntime gxtMonthRuntime)
    {
        List<GxtWorkOrder> workOrders = getOrdersByPid(gxtMonthRuntime.getDeptName(),gxtMonthRuntime.getStationName(),null, null);
        List<GxtMonthRuntime> result = new ArrayList<>();
        if (!workOrders.isEmpty()) {
            GxtSafeOperationReward safeOperationReward = new GxtSafeOperationReward();
            safeOperationReward.setStatus(0);
            List<GxtSafeOperationReward> safeList = safeOperationRewardMapper.selectGxtSafeOperationRewardList(safeOperationReward);
            if (!safeList.isEmpty()) {
                Collections.sort(safeList, new Comparator<GxtSafeOperationReward>() {
                    @Override
                    public int compare(GxtSafeOperationReward o1, GxtSafeOperationReward o2) {
                        return o2.getSafeDuration().compareTo(o1.getSafeDuration());
                    }
                });
                for (int i=0;i<safeList.size();i++) {
                    GxtSafeOperationReward safe = safeList.get(i);
                    Integer fromDays = safe.getSafeDuration();
                    Integer toDays = null;
                    if (i>0) {
                        toDays = safeList.get(i-1).getSafeDuration();
                    }
                    for (GxtWorkOrder workOrder:workOrders) {
                        // 停运时长计算
                        if (workOrder.getStopDays()>=fromDays && (toDays==null||toDays==0||workOrder.getStopDays()<toDays)) {
                            boolean isAdd = true;
                            String runtimeRange = "≥" + fromDays + "天";
                            if (!result.isEmpty()) {
                                for (GxtMonthRuntime runtime:result) {
                                    if (Objects.equals(runtime.getStationId(), workOrder.getPcsStationId()) && runtime.getRuntimeRange().equals(runtimeRange)) {
                                        isAdd = false;
                                        runtime.setStopNum(runtime.getStopNum()+1);
                                        break;
                                    }
                                }
                            }
                            if (isAdd) {
                                GxtMonthRuntime addRuntime = new GxtMonthRuntime();
                                addRuntime.setDeptName(workOrder.getDeptName());
                                addRuntime.setDeptId(workOrder.getPcsStationPid());
                                addRuntime.setStationId(workOrder.getPcsStationId());
                                addRuntime.setStationName(workOrder.getPcsStationName());
                                addRuntime.setFromDays(fromDays);
                                addRuntime.setToDays(toDays);
                                addRuntime.setRuntimeRange(runtimeRange);
                                addRuntime.setStopNum(1);
                                addRuntime.setRunningNum(0);
                                result.add(addRuntime);
                            }
                        }
                        // 持续运行时长计算
                        if (workOrder.getRunningDays()>=fromDays && (toDays==null||toDays==0||workOrder.getRunningDays()<toDays)) {
                            boolean isAdd = true;
                            String runtimeRange = "≥" + fromDays + "天";
                            if (!result.isEmpty()) {
                                for (GxtMonthRuntime runtime:result) {
                                    if (Objects.equals(runtime.getStationId(), workOrder.getPcsStationId()) && runtime.getRuntimeRange().equals(runtimeRange)) {
                                        isAdd = false;
                                        runtime.setRunningNum(runtime.getRunningNum()+1);
                                        break;
                                    }
                                }
                            }
                            if (isAdd) {
                                GxtMonthRuntime addRuntime = new GxtMonthRuntime();
                                addRuntime.setDeptName(workOrder.getDeptName());
                                addRuntime.setDeptId(workOrder.getPcsStationPid());
                                addRuntime.setStationId(workOrder.getPcsStationId());
                                addRuntime.setStationName(workOrder.getPcsStationName());
                                addRuntime.setFromDays(fromDays);
                                addRuntime.setToDays(toDays);
                                addRuntime.setRuntimeRange(runtimeRange);
                                addRuntime.setStopNum(0);
                                addRuntime.setRunningNum(1);
                                result.add(addRuntime);
                            }
                        }
                    }
                }
            }
            Collections.sort(result, new Comparator<GxtMonthRuntime>() {
                @Override
                public int compare(GxtMonthRuntime o1, GxtMonthRuntime o2) {
                    int deptCompare = o1.getDeptName().compareTo(o2.getDeptName());
                    if (deptCompare != 0) {
                        return deptCompare;
                    }
                    return o1.getFromDays().compareTo(o2.getFromDays());
                }
            });
        }
        return result;
    }

    public List<GxtWorkOrder> getOrdersByPid(String deptName, String stationName,Long pid, Long stationId) {
        GxtWorkOrder searchOrder = new GxtWorkOrder();
        searchOrder.setPcsStationPid(pid);
        searchOrder.setPcsStationId(stationId);
        searchOrder.setDeptName(deptName);
        searchOrder.setPcsStationName(stationName);
        addBusinessDataScopeFilter(searchOrder);
        GxtWorkOrder searchRepairOrder = new GxtWorkOrder();
        searchRepairOrder.setPcsStationPid(pid);
        searchRepairOrder.setPcsStationId(stationId);
        searchRepairOrder.setDeptName(deptName);
        searchRepairOrder.setPcsStationName(stationName);
        addBusinessDataScopeFilterRepair(searchRepairOrder);
        List<GxtWorkOrder> workOrders = new ArrayList<>();
//        List<GxtWorkOrder> workOrderList = gxtWorkOrderMapper.selectGxtWorkOrderRunningDaysInfo(searchOrder);
        List<GxtWorkOrder> workOrderList = new ArrayList<>();
        List<GxtWorkOrder> repairOrderList = gxtWorkOrderMapper.selectGxtRepairOrderRunningDaysInfo(searchRepairOrder);
        if (!workOrderList.isEmpty() && !repairOrderList.isEmpty()) {
            workOrders.addAll(repairOrderList);
            for (GxtWorkOrder workOrder:workOrderList) {
                boolean isAdd = true;
                GxtWorkOrder repairAdd = null;
                for (GxtWorkOrder repairOrder:repairOrderList) {
                    if (workOrder.getPcsDeviceId()==repairOrder.getPcsDeviceId()) {
                        isAdd = false;
                        if (workOrder.getPauseTime().compareTo(repairOrder.getPauseTime())>=0) {
                            repairAdd = repairOrder;
                        }
                        break;
                    }
                }
                if (isAdd) {
                    workOrders.add(workOrder);
                } else if (repairAdd!=null) {
                    workOrders.remove(repairAdd);
                    workOrders.add(workOrder);
                }
            }
        } else if (!workOrderList.isEmpty()) {
            workOrders.addAll(workOrderList);
        } else if (!repairOrderList.isEmpty()) {
            workOrders.addAll(repairOrderList);
        }
        if (!workOrders.isEmpty()) {
            Collections.sort(workOrders, new Comparator<GxtWorkOrder>() {
                @Override
                public int compare(GxtWorkOrder o1, GxtWorkOrder o2) {
                    int deptCompare = o1.getDeptName().compareTo(o2.getDeptName());
                    if (deptCompare != 0) {
                        return deptCompare;
                    }
                    return o1.getWorkOrderProjectNo().compareTo(o2.getWorkOrderProjectNo());
                }
            });
        }
        return workOrders;
    }

    @Override
    public List<GxtWorkOrder> getOrders(Long pid, Long stationId,Integer fromDays,Integer toDays,boolean isStop) {
        List<GxtWorkOrder> workOrders = getOrdersByPid(null, null, pid, stationId);
        List<GxtWorkOrder> result = new ArrayList<>();
        if (!workOrders.isEmpty()) {
            for (GxtWorkOrder order:workOrders) {
                if (isStop) {
                    if (order.getStopDays()>=fromDays && (toDays==null || toDays==0 || order.getStopDays()<toDays)) {
                        result.add(order);
                    }
                }else {
                    if (order.getRunningDays()>=fromDays && (toDays==null || toDays==0 || order.getRunningDays()<toDays)) {
                        result.add(order);
                    }
                }
            }
        }
        if (!result.isEmpty()) {
            Collections.sort(result, new Comparator<GxtWorkOrder>() {
                @Override
                public int compare(GxtWorkOrder o1, GxtWorkOrder o2) {
                    int deptCompare = o1.getDeptName().compareTo(o2.getDeptName());
                    if (deptCompare != 0) {
                        return deptCompare;
                    }
                    return o1.getWorkOrderProjectNo().compareTo(o2.getWorkOrderProjectNo());
                }
            });
        }
        return result;
    }

    /**
     * 添加业务特定的数据权限过滤条件
     * 基于角色的数据范围进行自定义权限控制
     *
     * @param gxtWorkOrder 保工单查询条件
     */
    private void addBusinessDataScopeFilter(GxtWorkOrder gxtWorkOrder) {
        // 获取当前登录用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return;
        }

        SysUser currentUser = loginUser.getUser();
        if (currentUser == null) {
            return;
        }

        // 如果是超级管理员，不过滤数据
        if (currentUser.isAdmin()) {
            return;
        }

        // 获取用户角色
        List<SysRole> roles = currentUser.getRoles();
        if (roles == null || roles.isEmpty()) {
            return;
        }

        // 检查是否有"全部数据"权限的角色，如果有则不过滤
        boolean hasAllDataPermission = roles.stream()
                .anyMatch(role -> DataScopeAspect.DATA_SCOPE_ALL.equals(role.getDataScope()));

        // 如果有任何一个角色有全部数据权限，则不过滤
        if (!hasAllDataPermission) {
            // 构建业务特定的数据权限SQL
            StringBuilder businessSql = new StringBuilder();

            // 遍历用户角色，根据角色的数据范围添加业务特定的权限控制
            for (SysRole role : roles) {
                String dataScope = role.getDataScope();

                // 自定义数据权限 - 根据角色的数据范围值来控制查询条件
                if (DataScopeAspect.DATA_SCOPE_CUSTOM.equals(dataScope)) {
                    // 获取具有自定义数据权限的角色ID列表
                    List<String> customRoleIds = new ArrayList<>();
                    for (SysRole r : roles) {
                        if (DataScopeAspect.DATA_SCOPE_CUSTOM.equals(r.getDataScope())) {
                            customRoleIds.add(String.valueOf(r.getRoleId()));
                        }
                    }

                    // 构建自定义数据权限查询条件
                    if (businessSql.length() > 0) {
                        businessSql.append(" OR ");
                    }

                    if (customRoleIds.size() > 1) {
                        // 多个自定义数据权限角色使用IN查询
                        businessSql.append("(t.pcs_station_pid IN (SELECT dept_id FROM sys_role_dept WHERE role_id IN (")
                                .append(String.join(",", customRoleIds))
                                .append("))")
                                .append(" OR t.pcs_station_id IN (SELECT dept_id FROM sys_role_dept WHERE role_id IN (")
                                .append(String.join(",", customRoleIds))
                                .append("))")
                                .append(" OR t.gxt_center_id IN (SELECT dept_id FROM sys_role_dept WHERE role_id IN (")
                                .append(String.join(",", customRoleIds))
                                .append(")))");
                    } else {
                        // 单个自定义数据权限角色查询
                        businessSql.append("(t.pcs_station_pid IN (SELECT dept_id FROM sys_role_dept WHERE role_id = ")
                                .append(role.getRoleId())
                                .append(")")
                                .append(" OR t.pcs_station_id IN (SELECT dept_id FROM sys_role_dept WHERE role_id = ")
                                .append(role.getRoleId())
                                .append(")")
                                .append(" OR t.gxt_center_id IN (SELECT dept_id FROM sys_role_dept WHERE role_id = ")
                                .append(role.getRoleId())
                                .append("))");
                    }
                    continue;
                }

                // 部门数据权限 - 适用于部门负责人角色
                if (DataScopeAspect.DATA_SCOPE_DEPT.equals(dataScope)) {
                    if (businessSql.length() > 0) {
                        businessSql.append(" OR ");
                    }
                    businessSql.append("(t.pcs_station_id = ")
                            .append(currentUser.getDeptId())
                            .append(" OR t.pcs_station_pid = ")
                            .append(currentUser.getDeptId())
                            .append(" OR t.gxt_center_id = ")
                            .append(currentUser.getDeptId())
                            .append(")");
                    continue;
                }

                // 部门及以下数据权限
                if (DataScopeAspect.DATA_SCOPE_DEPT_AND_CHILD.equals(dataScope)) {
                    if (businessSql.length() > 0) {
                        businessSql.append(" OR ");
                    }
                    businessSql.append("(t.pcs_station_id IN ")
                            .append("(SELECT dept_id FROM sys_dept WHERE dept_id = ")
                            .append(currentUser.getDeptId())
                            .append(" OR FIND_IN_SET(")
                            .append(currentUser.getDeptId())
                            .append(", ancestors))")
                            .append(" OR t.pcs_station_pid IN ")
                            .append("(SELECT dept_id FROM sys_dept WHERE dept_id = ")
                            .append(currentUser.getDeptId())
                            .append(" OR FIND_IN_SET(")
                            .append(currentUser.getDeptId())
                            .append(", ancestors))")
                            .append(" OR t.gxt_center_id IN ")
                            .append("(SELECT dept_id FROM sys_dept WHERE dept_id = ")
                            .append(currentUser.getDeptId())
                            .append(" OR FIND_IN_SET(")
                            .append(currentUser.getDeptId())
                            .append(", ancestors)))");
                    continue;
                }

                // 仅本人数据权限 - 适用于主检修员角色
                if (DataScopeAspect.DATA_SCOPE_SELF.equals(dataScope)) {
                    if (businessSql.length() > 0) {
                        businessSql.append(" OR ");
                    }
                    businessSql.append("(t.team_leader_id = ")
                            .append(currentUser.getUserId())
                            .append(" OR t.assign_user_id = ")
                            .append(currentUser.getUserId())
                            .append(" OR t.accept_user_id = ")
                            .append(currentUser.getUserId())
                            .append(" OR t.id IN (SELECT order_id FROM gxt_work_order_person WHERE user_id = ")
                            .append(currentUser.getUserId())
                            .append(") ")
                            .append(" OR t.create_by = '")
                            .append(currentUser.getUserName())
                            .append("')");
                    continue;
                }
            }
            String selfData = configService.selectConfigByKey("gxt.oder.selfData");
            if (StringUtils.isNotEmpty(selfData) && "1".equals(selfData)) {
                if (businessSql.length() > 0) {
                    businessSql.append(" OR ");
                }
                businessSql.append("(t.team_leader_id = ")
                        .append(currentUser.getUserId())
                        .append(" OR t.assign_user_id = ")
                        .append(currentUser.getUserId())
                        .append(" OR t.accept_user_id = ")
                        .append(currentUser.getUserId())
                        .append(" OR t.id IN (SELECT order_id FROM gxt_work_order_person WHERE user_id = ")
                        .append(currentUser.getUserId())
                        .append(") ")
                        .append(" OR t.create_by = '")
                        .append(currentUser.getUserName())
                        .append("')");
            }
            // 如果构建了业务特定的过滤条件，则添加到查询参数中
            if (businessSql.length() > 0) {
                String businessDataScopeSql = " AND (" + businessSql.toString() + ")";
                gxtWorkOrder.getParams().put("businessDataScope", businessDataScopeSql);
            }
        }
        // 对于已作废(work_order_status = 'invalid')的工单，只有admin用户才能查看
        if (!SecurityUtils.isAdmin(currentUser.getUserId())) {
            String invalidFilterSql = " AND (t.work_order_status != 'invalid')";
            if (gxtWorkOrder.getParams().get("businessDataScope") != null) {
                String existingSql = gxtWorkOrder.getParams().get("businessDataScope").toString();
                gxtWorkOrder.getParams().put("businessDataScope", existingSql + invalidFilterSql);
            } else {
                gxtWorkOrder.getParams().put("businessDataScope", invalidFilterSql);
            }
        }
    }


    /**
     * 添加业务特定的数据权限过滤条件
     * 基于角色的数据范围进行自定义权限控制
     *
     * @param repairOrder 保工单查询条件
     */
    private void addBusinessDataScopeFilterRepair(GxtWorkOrder repairOrder) {
        // 获取当前登录用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        if (loginUser == null) {
            return;
        }

        SysUser currentUser = loginUser.getUser();
        if (currentUser == null) {
            return;
        }

        // 如果是超级管理员，不过滤数据
        if (currentUser.isAdmin()) {
            return;
        }

        // 获取用户角色
        List<SysRole> roles = currentUser.getRoles();
        if (roles == null || roles.isEmpty()) {
            return;
        }

        // 检查是否有"全部数据"权限的角色，如果有则不过滤
        boolean hasAllDataPermission = roles.stream()
                .anyMatch(role -> DataScopeAspect.DATA_SCOPE_ALL.equals(role.getDataScope()));

        // 如果有任何一个角色有全部数据权限，则不过滤
        if (!hasAllDataPermission) {
            // 构建业务特定的数据权限SQL
            StringBuilder businessSql = new StringBuilder();

            // 遍历用户角色，根据角色的数据范围添加业务特定的权限控制
            for (SysRole role : roles) {
                String dataScope = role.getDataScope();

                // 自定义数据权限 - 根据角色的数据范围值来控制查询条件
                if (DataScopeAspect.DATA_SCOPE_CUSTOM.equals(dataScope)) {
                    // 获取具有自定义数据权限的角色ID列表
                    List<String> customRoleIds = new ArrayList<>();
                    for (SysRole r : roles) {
                        if (DataScopeAspect.DATA_SCOPE_CUSTOM.equals(r.getDataScope())) {
                            customRoleIds.add(String.valueOf(r.getRoleId()));
                        }
                    }

                    // 构建自定义数据权限查询条件
                    if (businessSql.length() > 0) {
                        businessSql.append(" OR ");
                    }

                    if (customRoleIds.size() > 1) {
                        // 多个自定义数据权限角色使用IN查询
                        businessSql.append("(t.pcs_station_pid IN (SELECT dept_id FROM sys_role_dept WHERE role_id IN (")
                                .append(String.join(",", customRoleIds))
                                .append("))")
                                .append(" OR t.pcs_station_id IN (SELECT dept_id FROM sys_role_dept WHERE role_id IN (")
                                .append(String.join(",", customRoleIds))
                                .append("))")
                                .append(" OR t.gxt_center_id IN (SELECT dept_id FROM sys_role_dept WHERE role_id IN (")
                                .append(String.join(",", customRoleIds))
                                .append(")))");
                    } else {
                        // 单个自定义数据权限角色查询
                        businessSql.append("(t.pcs_station_pid IN (SELECT dept_id FROM sys_role_dept WHERE role_id = ")
                                .append(role.getRoleId())
                                .append(")")
                                .append(" OR t.pcs_station_id IN (SELECT dept_id FROM sys_role_dept WHERE role_id = ")
                                .append(role.getRoleId())
                                .append(")")
                                .append(" OR t.gxt_center_id IN (SELECT dept_id FROM sys_role_dept WHERE role_id = ")
                                .append(role.getRoleId())
                                .append("))");
                    }
                    continue;
                }

                // 部门数据权限 - 适用于部门负责人角色
                if (DataScopeAspect.DATA_SCOPE_DEPT.equals(dataScope)) {
                    if (businessSql.length() > 0) {
                        businessSql.append(" OR ");
                    }
                    businessSql.append("(t.pcs_station_id = ")
                            .append(currentUser.getDeptId())
                            .append(" OR t.pcs_station_pid = ")
                            .append(currentUser.getDeptId())
                            .append(" OR t.gxt_center_id = ")
                            .append(currentUser.getDeptId())
                            .append(")");
                    continue;
                }

                // 部门及以下数据权限
                if (DataScopeAspect.DATA_SCOPE_DEPT_AND_CHILD.equals(dataScope)) {
                    if (businessSql.length() > 0) {
                        businessSql.append(" OR ");
                    }
                    businessSql.append("(t.pcs_station_id IN ")
                            .append("(SELECT dept_id FROM sys_dept WHERE dept_id = ")
                            .append(currentUser.getDeptId())
                            .append(" OR FIND_IN_SET(")
                            .append(currentUser.getDeptId())
                            .append(", ancestors))")
                            .append(" OR t.pcs_station_pid IN ")
                            .append("(SELECT dept_id FROM sys_dept WHERE dept_id = ")
                            .append(currentUser.getDeptId())
                            .append(" OR FIND_IN_SET(")
                            .append(currentUser.getDeptId())
                            .append(", ancestors))")
                            .append(" OR t.gxt_center_id IN ")
                            .append("(SELECT dept_id FROM sys_dept WHERE dept_id = ")
                            .append(currentUser.getDeptId())
                            .append(" OR FIND_IN_SET(")
                            .append(currentUser.getDeptId())
                            .append(", ancestors)))");
                    continue;
                }

                // 仅本人数据权限 - 适用于主检修员角色
                if (DataScopeAspect.DATA_SCOPE_SELF.equals(dataScope)) {
                    if (businessSql.length() > 0) {
                        businessSql.append(" OR ");
                    }
                    businessSql.append("(t.team_leader_id = ")
                            .append(currentUser.getUserId())
                            .append(" OR t.assign_user_id = ")
                            .append(currentUser.getUserId())
                            .append(" OR t.accept_user_id = ")
                            .append(currentUser.getUserId())
                            .append(" OR t.id IN (SELECT order_id FROM gxt_repair_order_person WHERE user_id = ")
                            .append(currentUser.getUserId())
                            .append(") ")
                            .append(" OR t.create_by = '")
                            .append(currentUser.getUserName())
                            .append("')");
                    continue;
                }
            }
            String selfData = configService.selectConfigByKey("gxt.oder.selfData");
            if (StringUtils.isNotEmpty(selfData) && "1".equals(selfData)) {
                if (businessSql.length() > 0) {
                    businessSql.append(" OR ");
                }
                businessSql.append("(t.team_leader_id = ")
                        .append(currentUser.getUserId())
                        .append(" OR t.assign_user_id = ")
                        .append(currentUser.getUserId())
                        .append(" OR t.accept_user_id = ")
                        .append(currentUser.getUserId())
                        .append(" OR t.id IN (SELECT order_id FROM gxt_repair_order_person WHERE user_id = ")
                        .append(currentUser.getUserId())
                        .append(") ")
                        .append(" OR t.create_by = '")
                        .append(currentUser.getUserName())
                        .append("')");
            }
            // 如果构建了业务特定的过滤条件，则添加到查询参数中
            if (businessSql.length() > 0) {
                String businessDataScopeSql = " AND (" + businessSql.toString() + ")";
                repairOrder.getParams().put("businessDataScope", businessDataScopeSql);
            }
        }
        // 对于已作废(work_order_status = 'invalid')的工单，只有admin用户才能查看
        if (!SecurityUtils.isAdmin(currentUser.getUserId())) {
            String invalidFilterSql = " AND (t.work_order_status != 'invalid')";
            if (repairOrder.getParams().get("businessDataScope") != null) {
                String existingSql = repairOrder.getParams().get("businessDataScope").toString();
                repairOrder.getParams().put("businessDataScope", existingSql + invalidFilterSql);
            } else {
                repairOrder.getParams().put("businessDataScope", invalidFilterSql);
            }
        }
    }

    /**
     * 新增月度场站运行天数工单
     * 
     * @param gxtMonthRuntime 月度场站运行天数工单
     * @return 结果
     */
    @Override
    public int insertGxtMonthRuntime(GxtMonthRuntime gxtMonthRuntime)
    {
        return gxtMonthRuntimeMapper.insertGxtMonthRuntime(gxtMonthRuntime);
    }

    /**
     * 修改月度场站运行天数工单
     * 
     * @param gxtMonthRuntime 月度场站运行天数工单
     * @return 结果
     */
    @Override
    public int updateGxtMonthRuntime(GxtMonthRuntime gxtMonthRuntime)
    {
        return gxtMonthRuntimeMapper.updateGxtMonthRuntime(gxtMonthRuntime);
    }

    /**
     * 批量删除月度场站运行天数工单
     * 
     * @param ids 需要删除的月度场站运行天数工单主键
     * @return 结果
     */
    @Override
    public int deleteGxtMonthRuntimeByIds(Long[] ids)
    {
        return gxtMonthRuntimeMapper.deleteGxtMonthRuntimeByIds(ids);
    }

    /**
     * 删除月度场站运行天数工单信息
     * 
     * @param id 月度场站运行天数工单主键
     * @return 结果
     */
    @Override
    public int deleteGxtMonthRuntimeById(Long id)
    {
        return gxtMonthRuntimeMapper.deleteGxtMonthRuntimeById(id);
    }
}