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

import com.backendsys.modules.material.dao.MaterialDao;
import com.backendsys.modules.material.dao.MaterialTagDao;
import com.backendsys.modules.material.entity.Material;
import com.backendsys.modules.material.entity.MaterialTag;
import com.backendsys.modules.material.service.MaterialTagService;
import com.backendsys.utils.response.PageEntity;
import com.backendsys.utils.response.PageInfoResult;
import com.backendsys.utils.v2.PageUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MaterialTagServiceImpl implements MaterialTagService {

    @Autowired
    private MaterialDao materialDao;

    @Autowired
    private MaterialTagDao materialTagDao;

    /**
     * 获取素材标签列表
     */
    @Override
    public PageEntity selectMaterialTagList(MaterialTag materialTag) {
        PageUtils.startPage();  // 分页
        List<MaterialTag> list = materialTagDao.selectMaterialTagList(materialTag);
        return new PageInfoResult(list).toEntity();
    }

    /**
     * 获取素材标签列表 (下拉)
     */
    @Override
    public List<MaterialTag> selectMaterialTagPopover(MaterialTag materialTag) {
        List<MaterialTag> list = materialTagDao.selectMaterialTagList(materialTag);

        // [DB] 查询全部素材的数量 (该分类下的)
        LambdaQueryWrapper<Material> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Material::getCategory_id, materialTag.getCategory_id());
        Long count = materialDao.selectCount(wrapper);

//        // 筛选出列表中，所有 material_count 的总和
//        Long count = list.stream().mapToLong(MaterialTag::getMaterial_count).sum();

        MaterialTag all = new MaterialTag();
        all.setTag_name("全部");
        all.setTag_id(null);
        all.setMaterial_count(count);
        list.add(0, all);

        return list;
    }

}
