package com.lz.modules.app.service.impl;

import com.google.common.collect.Lists;
import com.lz.common.utils.StringUtil;
import com.lz.modules.app.entity.StaffEntity;
import com.lz.modules.job.model.responseBo.DepartmentStaffBo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lz.common.utils.PageUtils;
import com.lz.common.utils.Query;

import com.lz.modules.app.dao.DepartmentsStaffRelateDao;
import com.lz.modules.app.entity.DepartmentsStaffRelateEntity;
import com.lz.modules.app.service.DepartmentsStaffRelateService;

import javax.annotation.Resource;


@Service("departmentsStaffRelateService")
public class DepartmentsStaffRelateServiceImpl extends ServiceImpl<DepartmentsStaffRelateDao, DepartmentsStaffRelateEntity> implements DepartmentsStaffRelateService {

    @Resource
    DepartmentsStaffRelateDao departmentsStaffRelateDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<DepartmentsStaffRelateEntity> page = this.page(
                new Query<DepartmentsStaffRelateEntity>().getPage(params),
                new QueryWrapper<DepartmentsStaffRelateEntity>()
        );
        return new PageUtils(page);
    }

    @Override
    public void addRelateInfos(String departmentId, List<DepartmentStaffBo> staffs) {
        List<DepartmentsStaffRelateEntity> departStaffRelateList = Lists.newArrayList();
        for (DepartmentStaffBo staffId : staffs) {
            String departId = departmentsStaffRelateDao.getRelateByStaffIdAndDepartmentId(staffId.getId(), departmentId);
            if (StringUtil.isEmpty(departId)) {
                DepartmentsStaffRelateEntity departmentsStaffRelateBo = new DepartmentsStaffRelateEntity();
                departmentsStaffRelateBo.setDepartmentId(departmentId);
                departmentsStaffRelateBo.setStaffId(staffId.getId());
                departmentsStaffRelateBo.setIsLeader(staffId.getIsLeader());
                departStaffRelateList.add(departmentsStaffRelateBo);
            } else /*if (!StringUtil.equals(departmentId, departId))*/ {
                departmentsStaffRelateDao.updateByStaffId(departmentId, staffId);
            }
        }
        //departmentsStaffRelateDao.addDepartmentStaffRelateBatch(departStaffRelateList);
        saveBatch(departStaffRelateList);
    }

    @Override
    public void deleteAllRelates() {
        departmentsStaffRelateDao.deleteAllRelates();
    }

    @Override
    public List<DepartmentsStaffRelateEntity> selectAll() {
        return departmentsStaffRelateDao.selectAll();
    }

    @Override
    public List<DepartmentsStaffRelateEntity> selectByStaffIds(List<Long> staffIds) {
        return departmentsStaffRelateDao.selectByStaffIds(staffIds);
    }

    @Override
    public DepartmentsStaffRelateEntity selectLastDepartmentByStaffId(Long staffId) {
        return departmentsStaffRelateDao.selectLastDepartmentByStaffId(staffId);
    }

    @Override
    public DepartmentsStaffRelateEntity selectLeaderByDepartmentId(String departmentId) {
        return departmentsStaffRelateDao.selectLeaderByDepartmentId(departmentId);
    }

    @Override
    public DepartmentsStaffRelateEntity selectDepartmentByDepartmentId(String departmentId) {
        return departmentsStaffRelateDao.selectDepartmentByDepartmentId(departmentId);
    }


    @Override
    public DepartmentsStaffRelateEntity selectByStaffId(Long staffId) {
        return departmentsStaffRelateDao.selectByStaffId(staffId);
    }

    @Override
    public List<String> selectStaffIdsByDepartments(List<String> deparmentIds) {
        if(CollectionUtils.isEmpty(deparmentIds)){
            return Collections.EMPTY_LIST;
        }
        return departmentsStaffRelateDao.selectStaffIdsByDepartments(deparmentIds);
    }

    @Override
    public List<StaffEntity> selectLeadersByDepartmentId(String depId){
        return departmentsStaffRelateDao.selectLeadersByDepartmentId(depId);
    }

    @Override
    public List<Long> selectLongStaffIdsByDepartments(List<String> departmentIds){
        if(CollectionUtils.isEmpty(departmentIds)){
            return Collections.EMPTY_LIST;
        }
        return departmentsStaffRelateDao.selectLongStaffIdsByDepartments(departmentIds);
    }

    @Override
    public List<Long> selectAllStaffIds(){
        return departmentsStaffRelateDao.selectAllStaffIds();
    }
}
