package cn.com.ctop.common.module.service.impl;

import cn.com.ctop.common.module.entity.TagInfo;
import cn.com.ctop.common.module.mapper.TagInfoMapper;
import cn.com.ctop.common.module.model.TagInfoTreeModel;
import cn.com.ctop.common.module.service.ITagInfoService;
import cn.com.ctop.common.module.utils.FindstagInfosChildrenUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 标签信息
 * @author jeecg-boot
 * @date   2020-08-26
 * @version V1.0
 */
@Service
public class TagInfoServiceImpl extends ServiceImpl<TagInfoMapper, TagInfo> implements ITagInfoService {

    @Autowired
    private TagInfoMapper mapper;
    /**
     * queryTreeList 对应 queryTreeList 查询所有的部门数据,以树结构形式响应给前端
     */
    @Override
    public List<TagInfoTreeModel> queryTreeList(String userId,Long categoryId) {
        QueryWrapper<TagInfo> query = new QueryWrapper<>();
        if(null!=categoryId&&categoryId!=0){
            query.eq("tag_category_id",categoryId);
        }
        query.eq("del_flag", "0").or(wrapper->wrapper.eq("status",0).eq("user_id",userId));
        query.orderByAsc("tag_order");
        List<TagInfo> list = this.list(query);
        // 调用wrapTreeDataToTreeList方法生成树状数据
        List<TagInfoTreeModel> listResult = FindstagInfosChildrenUtil.wrapTreeDataToTreeList(list);
        return listResult;
    }

    @Override
    public TagInfo getByTagCode(String tagCode) {
        QueryWrapper<TagInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tag_code",tagCode);
        queryWrapper.orderByDesc("id");
        queryWrapper.last("limit 1");
        return this.getOne(queryWrapper);
    }

    @Override
    public List<TagInfo> getByParentId(Long parentId) {
        QueryWrapper<TagInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("parent_id",parentId);
        queryWrapper.eq("del_flag",0);
        queryWrapper.orderByAsc("tag_order");
        return this.list(queryWrapper);
    }

    @Override
    public List<TagInfo> getListByCode(JSONArray array) {
        return mapper.getListByCode(array);
    }
}
