ImportController.class.php 8.11 KB
<?php
/**
 * 用户导入操作
 * User: liyifei2012it
 * Date: 16/9/17
 * Time: 22:45
 */

namespace Apicp\Controller\User;

use Common\Common\Department;
use Common\Model\AttrModel;
use Common\Service\AttrService;
use Common\Service\DepartmentService;
use Common\Service\ImportDataService;
use Common\Service\UserService;
use VcySDK\Member;
use VcySDK\Service;

class ImportController extends AbstractController
{

    /**
     * 【通讯录】人员导入
     * @author liyifei
     */
    public function Index_post()
    {
        try {
            // 保存失败原因
            $errMsg = '';
            $title_id = (int)I('post.title_id');
            $index = (int)I('post.index');

            // 读取 title 和待导入数据
            $importDataService = new ImportDataService();
            $dataList = $importDataService->list_by_pks(array($title_id, $index));
            $dataList = array_combine_by_key($dataList, 'cid_id');
            // 如果数据不存在
            if (empty($dataList[$title_id]) || empty($dataList[$index])) {
                E(L('_ERR_EXCEL_IMPORT', array('msg' => '数据错误')));
            }

            // 获取组织类型
            $titleData = json_decode($dataList[$title_id]['data']);
            $importData = json_decode($dataList[$index]['data']);
            // 新增导入失败原因
            $titleData[] = '失败原因';
            $this->_result['title'] = $titleData;
            $this->_result['data'] = $importData;

            // 判断管理权限
            if (!$this->_managePower()) {
                E(L('_ERR_EXCEL_IMPORT', array('msg' => '没有管理权限')));
            }

            $attrServ = new AttrService();
            $attrs = $attrServ->getAttrList(true, array(), true);
            $attrs = array_combine_by_key($attrs, 'attr_name');

            // 导入数据
            $member = array();
            $ignoreAttrs = array(
                AttrModel::ATTR_TYPE_PICTURE
            );
            foreach ($titleData as $_k => $_title) {
                $attr = $attrs[$_title];
                if (empty($attr) || in_array($attr['type'], $ignoreAttrs)) {
                    continue;
                }

                // 如果是多选
                if (AttrModel::ATTR_TYPE_CHECKBOX == $attr['type']) {
                    $explodes = explode(';', $importData[$_k]);
                    $selected = array();
                    foreach ($attr['option'] as $_attr) {
                        if (in_array($_attr['name'], $explodes)) {
                            $selected[] = $_attr;
                        }
                    }
                    $member[$attr['field_name']] = serialize($selected);
                    continue;
                } elseif (AttrModel::ATTR_TYPE_RADIO == $attr['type'] || AttrModel::ATTR_TYPE_DROPBOX == $attr['type']) {
                    $value = '';
                    foreach ($attr['option'] as $_attr) {
                        if ($_attr['name'] == $importData[$_k]) {
                            $value = $_attr['value'];
                            break;
                        }
                    }
                    $importData[$_k] = $value;
                } elseif (AttrModel::ATTR_TYPE_DATE == $attr['type']) {
                    // 没有提交日期时间时 跳过 不然会变成 1970-01-01
                    if (is_string($importData[$_k]) && empty($importData[$_k])) {
                        continue;
                    }
                    $originalData = $importData[$_k];
                    // 入库需要转换成毫秒
                    $importData[$_k] = rstrtotime($importData[$_k], 1);
                    // 显示需要还原成年月日格式
                    $this->_result['data'][$_k] = $originalData;
                }

                $member[$attr['field_name']] = $importData[$_k];
            }

            // 调用验证接口,验证参数传值是否符合规范
            $errors = $attrServ->checkValue($member);
            if (!empty($errors)) {
                if (count($errors) > 1) {
                    $errMsg = implode(',', $errors);
                } else {
                    $errMsg = $errors[0];
                }
                E($errMsg);
            }

            // 获取部门信息
            $dpIds = $this->_getDpIdByName($member['dpName']);
            if (empty($dpIds)) {
                if (!empty($errMsg)) {
                    $errMsg .= ',';
                }
                $errMsg .= '组织不存在';
                E(L('_ERR_EXCEL_IMPORT', array('msg' => $errMsg)));
                // 判断部门范围是否在管理范围内
            } elseif (!empty($this->_login->role['writeDpIdList'])) {
                $dpServ = new Department();
                $allow_dp_ids = $dpServ->list_childrens_by_cdid($this->_login->role['writeDpIdList'], true);

                if (array_diff($dpIds, $allow_dp_ids)) {
                    E(L('_ERR_EXCEL_IMPORT', array('msg' => '组织不在管理权限内')));
                }
            }

            $member['dpIdList'] = $dpIds;
            unset($member['dpName']);

            // 先搜索用户
            $memberSDK = new Member(Service::instance());
            $memberResult = array();
            if (!empty($member['memMobile'])) {
                $condition = array('memMobile' => $member['memMobile']);
                $memberResult = $memberSDK->listAll($condition);
            }
            if (empty($memberResult) && !empty($member['memWeixin'])) {
                $condition = array('memWeixin' => $member['memWeixin']);
                $memberResult = $memberSDK->listAll($condition);
            }
            if (empty($memberResult) && !empty($member['memEmail'])) {
                $condition = array('memEmail' => $member['memEmail']);
                $memberResult = $memberSDK->listAll($condition);
            }
            // 如果用户存在, 则
            if (!empty($memberResult['list']) && 1 == count($memberResult['list'])) {
                $uid = $memberResult['list'][0]['memUid'];
            } else {
                $uid = 0;
            }

            if (0 == $uid) {
                // 邀请记录信息
                $member['memJoinType'] = UserService::ADMIN_ADD_JOIN;
                if (!empty($this->_login->user['eaRealname'])) {
                    $member['memJoinInviter'] = $this->_login->user['eaRealname'];
                } elseif (!empty($this->_login->user['eaMobile'])) {
                    $member['memJoinInviter'] = $this->_login->user['eaMobile'];
                } elseif (!empty($this->_login->user['eaEmail'])) {
                    $member['memJoinInviter'] = $this->_login->user['eaEmail'];
                } else {
                    $member['memJoinInviter'] = '';
                }
            }

            // 更新用户信息
            $userService = new UserService();
            $this->_result = $userService->saveUser($uid, $member);

            $this->clearUserCache();
        } catch (\Exception $e) {
            // 如果读取数据正常, 则抛错
            if (!empty($this->_result['data'])) {
                if (empty($errMsg)) {
                    $errMsg = $e->getMessage();
                }
                $importDataService = new ImportDataService();
                $importDataService->update($index, array('is_error' => 1, 'fail_message' => $errMsg));
                // 错误列表新增失败原因
                $this->_result['data'][] = $errMsg;
            }

            E($e->getCode() . ":" . $errMsg);
        }

        return true;
    }

    /**
     * 通过部门名称获取部门ID
     * @param $dpName
     * @return array
     */
    protected function _getDpIdByName($dpName)
    {

        $dpIds = array();
        $dpNames = explode(';', $dpName);
        $departmentService = new DepartmentService();
        foreach ($dpNames as $_name) {
            $department = array();
            $departmentService->getDepartmentByPath($department, $_name, '', false);
            if (empty($department['dpId'])) {
                continue;
            }

            $dpIds[] = $department['dpId'];
        }

        return $dpIds;
    }

}