package com.lz.modules.job.business;

import com.google.common.collect.Lists;
import com.lz.common.utils.DateUtils;
import com.lz.common.utils.FeishuUtil;
import com.lz.modules.app.entity.StaffEntity;
import com.lz.modules.app.entity.StaffOccupationEntity;
import com.lz.modules.app.service.DepartmentsService;
import com.lz.modules.app.service.DepartmentsStaffRelateService;
import com.lz.modules.app.service.StaffOccupationService;
import com.lz.modules.app.service.StaffService;
import com.lz.modules.job.model.responseBo.DepartmentInfosBo;
import com.lz.modules.job.model.responseBo.DepartmentStaffBo;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * @author fumeiai
 * @date 2019-05-29 17:06
 * @Description 飞书业务
 * @注意 本内容仅限于杭州霖梓网络科技有限公司内部传阅，禁止外泄以及用于其他的商业目的
 */
@Component(value = "feishuBusiness")
public class FeishuBusiness {
    protected final static org.slf4j.Logger logger = LoggerFactory.getLogger(FeishuBusiness.class);

    @Autowired
    FeishuUtil feishuUtil;

    @Resource
    DepartmentsService departmentsService;

    @Resource
    DepartmentsStaffRelateService departmentsStaffRelateService;

    @Resource
    StaffService staffService;

    @Resource
    StaffOccupationService staffOccupationService;

    /**
     * 获取组织架构信息并录入到数据库
     */
//    @DataSource(name = DataSourceNames.FOUR)
    public void getFeishuDepartmentsIntoData() {
        //获取Token
        String token = feishuUtil.getAccessToken();

        //获取通讯录授权范围：获取最高级架构的department_id
        List<String> departmentIds = feishuUtil.getDepartmentIds(token);

        if (departmentIds == null || departmentIds.size() == 0) {
            return;
        }

        //批量获取部门详情，根据详情填充部门信息
        StringBuffer departmentIdSbs = new StringBuffer();
        for (String departmentId : departmentIds) {
            //获取子孙部门id列表
            departmentIdSbs.append("department_ids=" + departmentId + "&");
            List<String> childDepartmentIds = feishuUtil.getChildDepartmentIds(token, departmentId);
            for (String childDepartmentId : childDepartmentIds) {
                departmentIdSbs.append("department_ids=" + childDepartmentId + "&");
            }
        }

        String topDepartmentIds = departmentIdSbs.toString().substring(0, departmentIdSbs.toString().length() - 1);
        //获取所有的部门详情
        List<DepartmentInfosBo> departmentInfosBos = feishuUtil.getDepartmentDetails(token, topDepartmentIds);
        //更新数据库中的部门相关信息
        departmentsService.updateDepartmentInfos(departmentInfosBos);
        departmentsStaffRelateService.deleteAllRelates();
        //未在飞书组织架构里的成员，置为离职(全部置为离职，再把在职的恢复)
        staffOccupationService.updateAllOccupation();
        //获取飞书部门对应的用户详情
        for (DepartmentInfosBo departmentInfo : departmentInfosBos) {
            //获取部门用户详情
            List<DepartmentStaffBo> staffs = feishuUtil.getDepartmentStaffDetails(token, departmentInfo.getId());
            logger.info("=============================" + departmentInfo.getName() + "================================");
//            for (DepartmentStaffBo staff : staffs)
//                logger.info(staff.getName());
            //循环录入到员工信息表中
            if(staffService.updateStaffsInfo(staffs)){
                //加入到部门和员工关系表
                departmentsStaffRelateService.addRelateInfos(departmentInfo.getId(), staffs);
                //录入员工职业信息表
                enterStaffOccupationInfos(staffs);
            }



        }

    }

    public void enterStaffOccupationInfos(List<DepartmentStaffBo> staffs) {
        List<StaffOccupationEntity> StaffOccupations = Lists.newArrayList();
        for (DepartmentStaffBo departmentStaffBo : staffs) {
            StaffEntity staffEntity = staffService.getStaffInfoByOpenId(departmentStaffBo.getOpenId());
            Long staffId = staffEntity.getId();
            StaffOccupationEntity staffOccupationEntity = staffOccupationService.getStaffOccupationByStaffId(staffId);
            if (staffOccupationEntity == null) {
                StaffOccupationEntity staffOccupation = new StaffOccupationEntity();
                staffOccupation.setStaffId(staffId);
                staffOccupation.setEmployeeNo(departmentStaffBo.getEmployeeNo());
                staffOccupation.setStaffType(departmentStaffBo.getEmployeeType());
                staffOccupation.setStaffStatus(departmentStaffBo.getStatus());
                staffOccupation.setEntryTime(DateUtils.getCurrentDate());
                StaffOccupations.add(staffOccupation);
            } else {
                staffOccupationService.updateStatusByStaffId(staffId, departmentStaffBo.getStatus());
            }
        }
        if (StaffOccupations.size() > 0) {
            staffOccupationService.saveBatch(StaffOccupations);
        }
    }


}
