<?php /** * 【调研中心-后台】20_已填写人员数据导出(导出选项值) * WriteListBySheetController.class.php * CreateBy:dj * Date:2018-04-20 */ namespace Apicp\Controller\Export; use Com\PythonExcel; use Common\Common\ExportDownload; use Common\Service\AnswerService; use Common\Service\BaseinfoService; use Common\Service\QuestionService; use Common\Service\RecordService; use Org\Util\Emoji; class WriteListBySheetController extends \Apicp\Controller\AbstractController { /** @var BaseinfoService */ protected $_base_info_s; /** @var QuestionService */ protected $_question_s; /** @var RecordService */ protected $_record_s; /** @var AnswerService */ protected $_answer_s; protected $p_type = [ '', '【单项选择】', '【多项选择】', '【下拉选择】', '【单行文本】', '【多行文本】', '【日期时间】', '【数字】', '【金额】', '【评分】', '【上传图片】', '【上传文件】', '【段落说明】', '【姓名】', '【性别】', '【手机号】', '【邮箱】', '【微信号】', '【生日】', '【公司】', '【组织】', '【职位】', '【地址】', ]; // 前置操作 public function before_action($action = '') { if (parent::before_action($action) === false) { return false; } // 调研基本信息表 $this->_base_info_s = new BaseinfoService(); // 调研问题信息表 $this->_question_s = new QuestionService(); // 调研回答详情信息表 $this->_record_s = new RecordService(); // 调研回答用户记录表 $this->_answer_s = new AnswerService(); return true; } public function Index() { // 调研id $qu_id = trim(I('post.qu_id')); // 调研基本信息 $base_info = $this->_base_info_s->get($qu_id); // 查询调研题目信息列表 $question_list = $this->_question_s->list_by_conds(['qu_id' => $qu_id], null, ['q_order' => 'ASC']); // 数据格式化 list($list_order, $list_option) = $this->format_data($base_info, $question_list, $qu_id); $this->export($base_info, $question_list, $list_order, $list_option); return true; } /** * 获取数据列表并格式化 * * @param $base_info 调研基本信息 * @param $question_list 问题列表 * @param $qu_id 调研id * * @return array */ private function format_data($base_info, $question_list, $qu_id) { $list_order = []; // 按选项序号展示 $list_option = [];// 按选项内容展示 // 调研填写记录信息 $answer_list = $this->_answer_s->list_by_conds(['qu_id' => $qu_id]); // 填写用户id集合 $uids = array_unique(array_filter(array_column($answer_list, 'uid'))); sort($uids); // 调研填写详情记录信息列表 $record_list = $this->_record_s->list_by_conds(['qu_id' => $qu_id]); $emoji = Emoji::instance(); // 查询用户列表,将用户列表转换成以用户uid为主键的二维数组 $user_list = $this->_base_info_s->getUser($uids); // 序号 $i = 1; foreach ($answer_list as $k => $v) { // 内部用户 if (!empty($v['uid'])) { // 内部用户 $u_type = '内部用户'; // 实名 if (BaseinfoService::REAL_NAME == $base_info['anonymous']) { // 用户信息 $user_data = $user_list[$v['uid']]; // 组织名称 $dp_name = ''; // 组织名称赋值 if (!empty($user_data['dpName'])) { $dp_name_array = array_column($user_data['dpName'], 'dpName'); $dp_name = implode(',', $dp_name_array); } $name = $user_data['memUsername']; $dpName = !empty($dp_name) ? $dp_name : ''; $jobName = !empty($user_data['memJob']) ? $user_data['memJob'] : ''; $roleName = !empty($user_data['memRole']) ? $user_data['memRole'] : ''; $mobile = !empty($user_data['memMobile']) ? $user_data['memMobile'] : ''; } elseif (BaseinfoService::ANONYMOUS == $base_info['anonymous']) { // 匿名 $name = '匿名用户' . $i; $dpName = ''; $jobName = ''; $roleName = ''; $mobile = ''; } } elseif (!empty($v['openid'])) { // 外部用户 $name = $v['username']; // 微信昵称 $dpName = ''; $jobName = ''; $roleName = ''; $mobile = ''; $u_type = '外部用户'; // 外部用户 } $list_order[$k] = [ $i, // 序号 $name, // 用户姓名 $dpName, // 组织名称 $jobName, // 岗位名称 $roleName, // 角色名称 $mobile, // 用户手机 $u_type, // 用户类型:内部用户/外部用户 rgmdate($v['created'], 'Y-m-d H:i') // 填写时间 ]; $list_option[$k] = [ $i, // 序号 $name, // 用户姓名 $dpName, // 组织名称 $jobName, // 岗位名称 $roleName, // 角色名称 $mobile, // 用户手机 $u_type, // 用户类型:内部用户/外部用户 rgmdate($v['created'], 'Y-m-d H:i') // 填写时间 ]; // 填写详情记录 $answer_data = []; // 填写详情记录赋值 foreach ($record_list as $_v) { if ($_v['a_id'] == $v['a_id']) { $answer_data[] = $_v; } } // 将此数组转换成以问题ID为主键的新数组 $answer_data = array_combine_by_key($answer_data, 'q_id'); // 遍历问题信息 foreach ($question_list as $_val) { // 问题的填写记录 $answer = $answer_data[$_val['qid']]; // 单选、复选、下拉选择、性别选择 if (in_array($_val['q_type'], QuestionService::OPTION_TYPE)) { // 问题内容 $option_arr = unserialize($_val['q_field']); // 回答内容 $answer_arr = unserialize($answer['answer']); $answer_option = array_column($answer_arr, 'option'); $answer_img = array_unique(array_filter(array_column($answer_arr, 'option_img'))); $answer_index = ''; $answer_str = ''; // 循环选项,看选项是否选中 for ($n = 0; $n < count($option_arr); $n++) { if (empty($answer['answer'])) { continue; } // 如果选项中不包含图片,则只用 选项值就可以判断 if (empty($option_arr[$n]['option_img'])) { if (in_array($option_arr[$n]['option'], $answer_option)) { // 选中 if (empty($answer_str)) { $answer_str = $option_arr[$n]['option']; $answer_index = $n + 1; } else { $answer_str .= ',' . $option_arr[$n]['option']; $answer_index .= ',' . ($n + 1); } } } else { if (in_array($option_arr[$n]['option'], $answer_option) && in_array($option_arr[$n]['option_img'], $answer_img) ) { // 选中 if (empty($answer_str)) { $answer_str = $option_arr[$n]['option']; $answer_index = $n + 1; } else { $answer_str .= ',' . $option_arr[$n]['option']; $answer_index .= ',' . ($n + 1); } } } } // 是否包含其他选项(0:不包含,1:包含) if ($_val['q_other']) { // 包含其他选项 if (!empty($answer['other'])) { $other_answer = '【其他】:' . $answer['other']; if (empty($answer_str)) { $answer_str = $other_answer; $answer_index = $other_answer; } else { $answer_str .= ',' . $other_answer; $answer_index .= ',' . $other_answer; } } } $list_order[$k][] = $answer_index; $list_option[$k][] = $answer_str; } else { // 上传图片和上传文件类型 if (in_array($_val['q_type'], QuestionService::FILE_TYPE)) { // 回答内容 $answer_arr = unserialize($answer['answer']); $answer_img = array_column($answer_arr, 'option_img'); $imgs = ''; // 循环选项,看选项是否选中 foreach ($answer_img as $v) { if (!empty($v)) { $imgs .= "\r\n" . imgUrl($v); } } // 图片集合 $list_order[$k][] = $imgs; $list_option[$k][] = $imgs; } else { // 地址 if ($_val['q_type'] == 22) { $answer_res = $answer['answer'] . ' ' . $answer['other']; $list_order[$k][] = $answer_res; $list_option[$k][] = $answer_res; } else { $answer_res = $emoji->filter_emoji($answer['answer']); $list_order[$k][] = $answer_res; $list_option[$k][] = $answer_res; } } } } $i++; } return [$list_order, $list_option]; } /** * 导出模板信息 * * @param array $base_info 问卷基本信息 * @param array $question_list 问题列表 * @param array $list_order 按选项序号展示列表数据 * @param array $list_option 按选项内容展示列表数据 */ private function export($base_info, $question_list, $list_order, $list_option) { // 文件名 $fileName = $base_info['title']; // 组装表头问题选项行 $index_arr = ['序号', '姓名', '组织', '岗位', '角色', '手机号', '用户类型', '填写时间']; foreach ($question_list as $k => $v) { $q_type = $this->p_type[$v['q_type']]; $index_arr[] = $q_type . ($k + 1) . "." . $v['q_title']; } // 组装数组 $data = [ [ 'columns' => $index_arr, // 列名 'rows' => $list_order, // 数据 ], [ 'columns' => $index_arr, // 列名 'rows' => $list_option, // 数据 ], ]; // sheets名数组 $sheets = ["按选项序号展示", "按选项内容展示"]; // Python导出excel $realpath = ExportDownload::get_down_dir($this->_login->user['eaId'] . microtime(true)) . $fileName.'_已填写人员数据' . ".xls"; $ret = PythonExcel::instance()->writeSheet($realpath, $sheets, $data); if ($ret) { $data = [ 'title' => $fileName, 'ea_id' => $this->_login->user['eaId'], 'type' => ExportDownload::EXCEL_TYPE, 'username' => $this->_login->user['eaRealname'], 'url' => $realpath, 'app_dir' => APP_DIR ]; ExportDownload::insert_down_load($data); } return true; } }