package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.system.domain.UserTableColumnConfig;
import com.ruoyi.system.mapper.UserTableColumnConfigMapper;
import com.ruoyi.system.service.IUserTableColumnConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户自定义列配置Service业务层处理
 * 
 * @author gyt
 * @date 2025-10-30
 */
@Service
public class UserTableColumnConfigServiceImpl implements IUserTableColumnConfigService 
{
    @Autowired
    private UserTableColumnConfigMapper userTableColumnConfigMapper;

    /**
     * 查询用户自定义列配置
     * 
     * @param id 用户自定义列配置主键
     * @return 用户自定义列配置
     */
    @Override
    public UserTableColumnConfig selectUserTableColumnConfigById(Long id)
    {
        return userTableColumnConfigMapper.selectUserTableColumnConfigById(id);
    }

    /**
     * 查询用户自定义列配置列表
     * 
     * @param userTableColumnConfig 用户自定义列配置
     * @return 用户自定义列配置
     */
    @Override
    public List<UserTableColumnConfig> selectUserTableColumnConfigList(UserTableColumnConfig userTableColumnConfig)
    {
        return userTableColumnConfigMapper.selectUserTableColumnConfigList(userTableColumnConfig);
    }

    /**
     * 新增用户自定义列配置
     * 
     * @param userTableColumnConfig 用户自定义列配置
     * @return 结果
     */
    @Override
    public int insertUserTableColumnConfig(UserTableColumnConfig userTableColumnConfig)
    {
        Long userId = userTableColumnConfig.getUserId();
        if(userId == null){
            System.err.println("用户不存在");
            return 0;
        }
        if(userTableColumnConfig.getPageRoute() == null){
            System.err.println("路由不存在");
            return 0;
        }
        userTableColumnConfig.setCreateTime(DateUtils.getNowDate());
        userTableColumnConfig.setUserId(userId);
        // 查询该用户是否已存在配置
        UserTableColumnConfig configQuery = new UserTableColumnConfig();
        configQuery.setUserId(userId);
        configQuery.setPageRoute(userTableColumnConfig.getPageRoute());
        List<UserTableColumnConfig> exits = userTableColumnConfigMapper.selectUserTableColumnConfigList(configQuery);
        if(exits.isEmpty()){
            userTableColumnConfigMapper.insertUserTableColumnConfig(userTableColumnConfig);
        }else{
            userTableColumnConfigMapper.updateUserTableColumnConfig(userTableColumnConfig);
        }
        return 1;
    }

    /**
     * 修改用户自定义列配置
     * 
     * @param userTableColumnConfig 用户自定义列配置
     * @return 结果
     */
    @Override
    public int updateUserTableColumnConfig(UserTableColumnConfig userTableColumnConfig)
    {
        userTableColumnConfig.setUpdateTime(DateUtils.getNowDate());
        return userTableColumnConfigMapper.updateUserTableColumnConfig(userTableColumnConfig);
    }

    /**
     * 批量删除用户自定义列配置
     * 
     * @param ids 需要删除的用户自定义列配置主键
     * @return 结果
     */
    @Override
    public int deleteUserTableColumnConfigByIds(Long[] ids)
    {
        return userTableColumnConfigMapper.deleteUserTableColumnConfigByIds(ids);
    }

    /**
     * 删除用户自定义列配置信息
     * 
     * @param id 用户自定义列配置主键
     * @return 结果
     */
    @Override
    public int deleteUserTableColumnConfigById(Long id)
    {
        return userTableColumnConfigMapper.deleteUserTableColumnConfigById(id);
    }

    @Override
    public UserTableColumnConfig getColumnConfigByUserId(UserTableColumnConfig userTableColumnConfig) {
        UserTableColumnConfig configQuery = new UserTableColumnConfig();
        configQuery.setUserId(SecurityUtils.getUserId());
        configQuery.setPageRoute(userTableColumnConfig.getPageRoute());
        List<UserTableColumnConfig> userTableColumnConfigList = userTableColumnConfigMapper.selectUserTableColumnConfigList(configQuery);
        if(!userTableColumnConfigList.isEmpty()){
            return userTableColumnConfigList.get(0);
        }
        return new UserTableColumnConfig();
    }
}
