package com.xunmei.core.edu.service.impl;

import java.util.Collections;
import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xunmei.common.core.domain.IdName;
import com.xunmei.common.core.utils.DateUtils;
import com.xunmei.common.core.utils.StringUtils;
import com.xunmei.common.core.domain.edu.domain.SysKnowledge;
import com.xunmei.core.edu.mapper.SysKnowledgeMapper;
import com.xunmei.core.edu.service.ISysKnowledgeService;
import com.xunmei.system.api.RemoteOrgService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xunmei.common.core.web.page.TableDataInfo;

/**
 * 知识库标签Service业务层处理
 *
 * @author xunmei
 * @date 2023-08-21
 */
@Service
public class SysKnowledgeServiceImpl extends ServiceImpl<SysKnowledgeMapper, SysKnowledge> implements ISysKnowledgeService {
    @Autowired
    private SysKnowledgeMapper sysKnowledgeMapper;
    @Autowired
    private RemoteOrgService orgService;

    @Override
    public TableDataInfo selectPage(SysKnowledge sysKnowledge) {

        //未删除
        sysKnowledge.setDeleted(0L);
        Page<SysKnowledge> page;
        //分页
        if (sysKnowledge.getPageNum() != null && sysKnowledge.getPageSize() != null) {
            page = new Page<>(sysKnowledge.getPageNum(), sysKnowledge.getPageSize());
        } else {
            page = new Page<>();
        }
        //查询条件
        LambdaQueryWrapper<SysKnowledge> wrapper = new LambdaQueryWrapper<>(sysKnowledge);
        //模糊查询
        if (StringUtils.isNotNull(sysKnowledge.getName())) {
            wrapper.like(SysKnowledge::getName, sysKnowledge.getName());
            sysKnowledge.setName(null);
        }
        //获取数据
        page = sysKnowledgeMapper.selectPage(page, wrapper);
        //抓换为TableDataInfo适配前端
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setMsg("操作成功");
        tableDataInfo.setCode(200);
        tableDataInfo.setTotal(page.getTotal());
        tableDataInfo.setRows(page.getRecords());
        return tableDataInfo;


    }


    /**
     * 查询知识库标签
     *
     * @param id 知识库标签主键
     * @return 知识库标签
     */
    @Override
    public SysKnowledge selectSysKnowledgeById(Long id) {
        return sysKnowledgeMapper.selectById(id);
    }

    /**
     * 查询知识库标签列表
     *
     * @param sysKnowledge 知识库标签
     * @return 知识库标签
     */
    @Override
    public List<SysKnowledge> selectSysKnowledgeList(SysKnowledge sysKnowledge) {
        return sysKnowledgeMapper.selectList(new QueryWrapper<>(sysKnowledge));
    }

    /**
     * 新增知识库标签
     *
     * @param sysKnowledge 知识库标签
     * @return 结果
     */
    @Override
    public int insertSysKnowledge(SysKnowledge sysKnowledge) {
        sysKnowledge.setCreateTime(DateUtils.getNowDate());
        return sysKnowledgeMapper.insert(sysKnowledge);
    }

    /**
     * 修改知识库标签
     *
     * @param sysKnowledge 知识库标签
     * @return 结果
     */
    @Override
    public int updateSysKnowledge(SysKnowledge sysKnowledge) {
        sysKnowledge.setUpdateTime(DateUtils.getNowDate());
        return sysKnowledgeMapper.updateById(sysKnowledge);
    }

    /**
     * 批量删除知识库标签
     *
     * @param ids 需要删除的知识库标签主键
     * @return 结果
     */
    @Override
    public int deleteSysKnowledgeByIds(Long[] ids) {
        return sysKnowledgeMapper.deleteBatchIds(Arrays.asList((ids)));
    }

    /**
     * 删除知识库标签信息
     *
     * @param id 知识库标签主键
     * @return 结果
     */
    @Override
    public int deleteSysKnowledgeById(Long id) {
        return sysKnowledgeMapper.deleteById(id);
    }

    /**
     * 查询知识库标签列表数据
     *
     * @param
     * @return List
     */
    @Override
    public List<IdName<String, String>> dataList() {
        final List<SysKnowledge> sysKnowledge = sysKnowledgeMapper.dataList();
        if (sysKnowledge == null || sysKnowledge.isEmpty()) {
            return Collections.emptyList();
        }
        return sysKnowledge.stream().map(e -> new IdName<>(e.getId().toString(), e.getName())).collect(Collectors.toList());

    }
}
