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

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.google.common.collect.Lists;
import com.lz.common.utils.PageUtils;
import com.lz.common.utils.Query;
import com.lz.common.utils.StringUtil;
import com.lz.modules.app.dto.DepartmentsDto;
import com.lz.modules.app.dao.DepartmentsDao;
import com.lz.modules.app.dto.StaffDepartmentDto;
import com.lz.modules.app.entity.DepartmentsEntity;
import com.lz.modules.app.service.DepartmentsService;
import com.lz.modules.job.model.responseBo.DepartmentInfosBo;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Service("departmentsService")
public class DepartmentsServiceImpl extends ServiceImpl<DepartmentsDao, DepartmentsEntity> implements DepartmentsService {

    protected final static org.slf4j.Logger logger = LoggerFactory.getLogger(DepartmentsServiceImpl.class);

    @Resource
    DepartmentsDao departmentsDao;

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


    @Override
    public void updateDepartmentInfos(List<DepartmentInfosBo> departmentInfosBos) {
        try {
            departmentsDao.updateDepartmentStatus();
            List<DepartmentsEntity> departmentsBos = filterUpdateDepartmentInfos(departmentInfosBos);
            if (departmentsBos.size() > 0) {
                //departmentsDao.addDepartmentBatch(departmentsBos);
                saveBatch(departmentsBos);
            }
        } catch (Exception e) {
            logger.info("updateDepartmentInfos error : " + e);
        }
    }

    @Override
    public List<DepartmentsDto> getDepartmentTree() {
        List<DepartmentsDto> departmentsParentsList = departmentsDao.getDepartmentsByparentId("0");
        getDepartmentTreeList(departmentsParentsList);
        return departmentsParentsList;
    }

    @Override
    public List<DepartmentsDto> selectByParentDepartmentId(String deparentId) {
        return departmentsDao.getDepartmentsByparentId(deparentId+"");
    }

    @Override
    public List<DepartmentsEntity> selectEntityByParentDepartmentId(String parentId) {
        return departmentsDao.selectEntityByParentDepartmentId(parentId);
    }

    @Override
    public DepartmentsEntity selectByDepartmentId(String departmentId) {
        return departmentsDao.selectByDepartmentId(departmentId);
    }

    @Override
    public StaffDepartmentDto selectStaffAllDepartments(String departmentId) {
        StaffDepartmentDto staffDepartmentDto =  departmentsDao.selectStaffAllDepartments(departmentId);
        if(StringUtil.isEmpty(staffDepartmentDto.getDepartment1())){
            staffDepartmentDto.setDepartment1(staffDepartmentDto.getDepartment2());
            staffDepartmentDto.setDepartment2(staffDepartmentDto.getDepartment3());
            staffDepartmentDto.setDepartment3(staffDepartmentDto.getDepartment4());
        }
        return staffDepartmentDto;
    }

    private List<DepartmentsDto> getDepartmentTreeList(List<DepartmentsDto> departmentList) {
        List<DepartmentsDto> DepartmentsList = Lists.newArrayList();
        for (DepartmentsDto departmentsDto : departmentList) {
            List<DepartmentsDto> departmentsChirdList = departmentsDao.getDepartmentsByparentId(departmentsDto.getDepartmentId());
            if (departmentsChirdList.size() > 0) {
                departmentsDto.setList(getDepartmentTreeList(departmentsChirdList));
            }
            DepartmentsList.add(departmentsDto);
        }
        return DepartmentsList;
    }

    // @Override
    public List<DepartmentsEntity> filterUpdateDepartmentInfos(List<DepartmentInfosBo> departmentInfosBos) {
        List<DepartmentsEntity> departmentsBos = Lists.newArrayList();
        for (DepartmentInfosBo departmentInfosBo : departmentInfosBos) {
            DepartmentsEntity departmentsEntity = convertDepartmentEntity(departmentInfosBo);
            DepartmentsEntity departmentBo = departmentsDao.getDepartmentByDepartmentId(departmentInfosBo.getId());
            if (departmentBo != null) {
                departmentsDao.updateDepartment(departmentsEntity);
            } else {
                departmentsBos.add(departmentsEntity);
            }
        }
        return departmentsBos;
    }

    public DepartmentsEntity convertDepartmentEntity(DepartmentInfosBo departmentInfosBo) {
        DepartmentsEntity departmentsEntity = new DepartmentsEntity();
        departmentsEntity.setDepartmentId(departmentInfosBo.getId());
        departmentsEntity.setDepartmentParentId(departmentInfosBo.getParentId());
        departmentsEntity.setMemberCount(departmentInfosBo.getMemberCount());
        departmentsEntity.setDepartmentName(departmentInfosBo.getName());
        departmentsEntity.setChatId(departmentInfosBo.getChatId());
        departmentsEntity.setLeaderEmployeeId(departmentInfosBo.getLeaderEmployeeId());
        departmentsEntity.setLeaderOpenId(departmentInfosBo.getLeaderOpenId());
        departmentsEntity.setStatus(departmentInfosBo.getStatus()); //部门状态【0 无效，1 有效】
        return departmentsEntity;
    }


}
