package com.sky.ioc.service.person.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.entity.domain.person.Person;
import com.sky.ioc.mapper.person.PersonMapper;
import com.sky.ioc.service.person.PersonService;
import com.sky.ioc.tool.ReturnMsg;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class PersonServiceImpl implements PersonService {

    @Autowired
    PersonMapper personMapper;


    @Override
    public ReturnMsg pageList(String name,String companyName,String deptName,Integer type,Integer status,Integer page,Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        if(StringUtils.isNotBlank(name)){
            name ="%"+name+"%";
        }
        if(StringUtils.isNotBlank(companyName)){
            companyName ="%"+companyName+"%";
        }
        if(StringUtils.isNotBlank(deptName)){
            deptName ="%"+deptName+"%";
        }
        List<Person> lists = personMapper.pageList(name,companyName,deptName,type,status);
        return ReturnMsg.ok(new PageInfo<>(lists));
    }

    @Override
    public ReturnMsg addPerson(Person person) {
        person.setCreateTime(new Date()+"");
        personMapper.insert(person);
        return ReturnMsg.ok();
    }

    @Override
    public ReturnMsg editPerson(Person person) {
        if(person.getId()==null){
            return ReturnMsg.fail("参数错误");
        }else{
            person.setUpdateTime(new Date()+"");
            personMapper.updateById(person);
            return ReturnMsg.ok();
        }
    }

    @Override
    public ReturnMsg delPersonById(Integer id) {
        if(id==null){
            return ReturnMsg.fail("参数错误");
        }else{
            Person person = personMapper.selectById(id);
            person.setIsDel(1);
            person.setUpdateTime(new Date()+"");
            personMapper.updateById(person);
            return ReturnMsg.ok();
        }
    }

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