package com.fjhx.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.entity.Material;
import com.fjhx.service.ClassifyService;
import com.fjhx.service.MaterialExService;
import com.fjhx.service.MaterialService;
import com.fjhx.utils.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class MaterialServiceExImpl implements MaterialExService {

    @Autowired
    private MaterialService materialService;

    @Autowired
    private ClassifyService classifyService;

    @Override
    public Page<Material> getPage(Map<String, String> condition) {

        LambdaQueryWrapper<Material> wrapper = Wrappers.lambdaQuery();

        wrapper.orderByAsc(Material::getCode);

        String keyword = condition.get("keyword");
        if (ObjectUtil.isNotEmpty(keyword)) {
            wrapper.and(q -> q.like(Material::getCode, keyword).or().like(Material::getName, keyword));
        }

        // 如果传入分类id，查询分类已经分类下级
        String classifyId = condition.get("classifyId");
        if (ObjectUtil.isNotEmpty(classifyId)) {
            List<Long> childrenIdList = classifyService.getChildrenIdList(Long.valueOf(classifyId));
            wrapper.in(Material::getClassifyId, childrenIdList);
        }

        return materialService.page(materialService.createPage(condition), wrapper);
    }

}
