package com.sky.ioc.service.system.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.ioc.entity.Index;
import com.sky.ioc.mapper.IndexMapper;
import com.sky.ioc.service.system.IndexService;
import com.sky.ioc.tool.ReturnMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class IndexServiceImpl implements IndexService {

    @Autowired
    IndexMapper indexMapper;

    @Override
    public ReturnMsg pageList(Integer parentId, Integer page, Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<Index> lists = indexMapper.selectList(new LambdaQueryWrapper<Index>().eq(Index::getParentId,parentId).eq(Index::getIsDel,0));
        return ReturnMsg.ok(new PageInfo<>(lists));
    }

    @Override
    public ReturnMsg addIndex(Index index) {
        indexMapper.insert(index);
        return ReturnMsg.ok();
    }

    @Override
    public ReturnMsg editIndex(Index index) {
        if(index.getId()==null){
            return ReturnMsg.fail("参数错误");
        }else{
            indexMapper.updateById(index);
            return ReturnMsg.ok();
        }

    }

    @Override
    public ReturnMsg delIndexById(Integer id) {
        if(id==null){
            return ReturnMsg.fail("参数错误");
        }else{
            Index index = indexMapper.selectById(id);
            index.setIsDel(1);
            indexMapper.updateById(index);
            return ReturnMsg.ok();
        }
    }

    @Override
    public ReturnMsg delBatchById(List<Integer> ids) {
        if(ids.size()==0||ids==null){
            return ReturnMsg.fail("参数错误");
        }else{
            indexMapper.batchDelByIds(ids);
            return ReturnMsg.ok();
        }
    }
}
