package com.xueyi.system.digitalmans.manager.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xueyi.system.digitalmans.domain.po.DmQAndAPo;
import com.xueyi.system.digitalmans.domain.po.DmQuestionsPo;
import com.xueyi.system.digitalmans.domain.dto.DmQuestionsDto;
import com.xueyi.system.digitalmans.domain.query.DmQuestionsQuery;
import com.xueyi.system.digitalmans.domain.model.DmQuestionsConverter;
import com.xueyi.system.digitalmans.mapper.DmQAndAMapper;
import com.xueyi.system.digitalmans.mapper.DmQuestionsMapper;
import com.xueyi.common.web.entity.manager.impl.BaseManagerImpl;
import com.xueyi.system.digitalmans.manager.IDmQuestionsManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * 企业知识库问题管理 数据封装层处理
 *
 * @author yinruoxi
 */
@Component
public class DmQuestionsManager extends BaseManagerImpl<DmQuestionsQuery, DmQuestionsDto, DmQuestionsPo, DmQuestionsMapper, DmQuestionsConverter> implements IDmQuestionsManager {
    @Autowired
    DmQAndAMapper dmQAndAMapper;

    @Override
    public List<DmQuestionsDto> selectList(DmQuestionsQuery query) {
        List<DmQuestionsDto> dmQuestionsDtos = super.selectList(query);
        dmQuestionsDtos.forEach(item->{
            item.setCount(dmQAndAMapper.selectCount(Wrappers.<DmQAndAPo>query().lambda().eq(DmQAndAPo::getKnowledgeId,item.getId())));
        });
        return dmQuestionsDtos;
    }

    @Override
    public int deleteByIds(Collection<? extends Serializable> idList) {
        // 根据知识库id删除对应问题集
        idList.forEach(item->{
            dmQAndAMapper.delete(Wrappers.<DmQAndAPo>query().lambda().eq(DmQAndAPo::getKnowledgeId,item));
        });
        return super.deleteByIds(idList);
    }

    @Override
    public int updateStatus(DmQuestionsDto dto) {
        // 根据知识库id更新对应问题集状态
//        dmQAndAMapper.selectList(Wrappers.<DmQAndAPo>query().lambda().eq(DmQAndAPo::getKnowledgeId,dto.getId())).forEach(item->{
//            item.setStatus(dto.getStatus());
//            dmQAndAMapper.update(item);
//        };
        return super.updateStatus(dto);
    }
}