package com.backendsys.modules.material.service.impl;

import com.backendsys.modules.material.dao.MaterialCategoryDao;
import com.backendsys.modules.material.entity.MaterialCategory;
import com.backendsys.modules.material.service.MaterialCategoryService;
import com.backendsys.utils.response.PageEntity;
import com.backendsys.utils.response.PageInfoResult;
import com.backendsys.utils.v2.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MaterialCategoryServiceImpl implements MaterialCategoryService {

    @Autowired
    private MaterialCategoryDao materialCategoryDao;

    /**
     * 获取素材分类列表 (全部/分页)
     */
    @Override
    public PageEntity selectMaterialCategoryList(MaterialCategory materialCategory) {
        PageUtils.startPage();  // 分页
        List<MaterialCategory> list = materialCategoryDao.selectMaterialCategoryList(materialCategory);
        return new PageInfoResult(list).toEntity();
    }

    /**
     * 获取素材分类列表 (下拉)(将 { is_share } 的记录独立出来)
     */
    @Override
    public List<MaterialCategory> selectMaterialCategoryPopover(MaterialCategory materialCategory) {

        // [DB] 查询非共享的素材分类 { is_share = -1 }
        materialCategory.setIs_share(-1);
        List<MaterialCategory> list = materialCategoryDao.selectMaterialCategoryList(materialCategory);

        // [DB] 查询共享的素材分类 { is_share = 1, lora_id = null }
        materialCategory.setLora_id(null);
        materialCategory.setIs_share(1);
        List<MaterialCategory> list_of_share = materialCategoryDao.selectMaterialCategoryList(materialCategory);

        // 将共享分类追加到非共享分类后面
        list.addAll(list_of_share);

        return list;
    }

}
