package com.youlai.boot.system.service.impl;

import com.youlai.boot.system.service.SysFolderService;
import com.youlai.boot.system.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youlai.boot.system.mapper.OMRecordMapper;
import com.youlai.boot.system.service.OMRecordService;
import com.youlai.boot.system.model.entity.OMRecord;
import com.youlai.boot.system.model.form.OMRecordForm;
import com.youlai.boot.system.model.query.OMRecordQuery;
import com.youlai.boot.system.model.vo.OMRecordVO;
import com.youlai.boot.system.converter.OMRecordConverter;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;

/**
 * 运维管理服务实现类
 *
 * @author kangyu
 * @since 2025-07-25 10:18
 */
@Service
@RequiredArgsConstructor
public class OMRecordServiceImpl extends ServiceImpl<OMRecordMapper, OMRecord> implements OMRecordService {

    private final OMRecordConverter oMRecordConverter;

    private final SysFolderService sysFolderService;

    private final UserService userService;

    /**
    * 获取运维管理分页列表
    *
    * @param queryParams 查询参数
    * @return {@link IPage<OMRecordVO>} 运维管理分页列表
    */
    @Override
    public IPage<OMRecordVO> getOMRecordPage(OMRecordQuery queryParams) {
        List<Integer> subIds = sysFolderService.getSubFolderIdsRecursively(queryParams.getFolderId());

        queryParams.setSubIds(subIds);

        String nickname = userService.getCurrentUserInfo().getNickname();

        queryParams.setCurUserName(nickname);

        Page<OMRecordVO> pageVO = this.baseMapper.getOMRecordPage(
                new Page<>(queryParams.getPageNum(), queryParams.getPageSize()),
                queryParams
        );
        return pageVO;
    }
    
    /**
     * 获取运维管理表单数据
     *
     * @param id 运维管理ID
     * @return
     */
    @Override
    public OMRecordForm getOMRecordFormData(Long id) {
        OMRecord entity = this.getById(id);
        return oMRecordConverter.toForm(entity);
    }
    
    /**
     * 新增运维管理
     *
     * @param formData 运维管理表单对象
     * @return
     */
    @Override
    public boolean saveOMRecord(OMRecordForm formData) {
        OMRecord entity = oMRecordConverter.toEntity(formData);
        entity.setCreateTime(LocalDateTime.now());
        return this.save(entity);
    }
    
    /**
     * 更新运维管理
     *
     * @param id   运维管理ID
     * @param formData 运维管理表单对象
     * @return
     */
    @Override
    public boolean updateOMRecord(Long id,OMRecordForm formData) {
        OMRecord entity = oMRecordConverter.toEntity(formData);
        entity.setUpdateTime(LocalDateTime.now());
        return this.updateById(entity);
    }
    
    /**
     * 删除运维管理
     *
     * @param ids 运维管理ID，多个以英文逗号(,)分割
     * @return
     */
    @Override
    public boolean deleteOMRecords(String ids) {
        Assert.isTrue(StrUtil.isNotBlank(ids), "删除的运维管理数据为空");
        // 逻辑删除
        List<Long> idList = Arrays.stream(ids.split(","))
                .map(Long::parseLong)
                .toList();
        return this.removeByIds(idList);
    }

}
