ExportController.class.php 5.07 KB
<?php
/**
 * Created by IntelliJ IDEA.
 * 勋章列表
 * User: zhoutao
 * Reader: zhoutao 2017-05-31 10:07:35
 * Date: 2017-05-24 15:43:49
 */

namespace Apicp\Controller\Medal;

use Com\PackageValidate;
use Com\PythonExcel;
use Common\Common\Constant;
use Common\Common\ExportDownload;
use Common\Common\Integral;
use Common\Common\User;
use Common\Service\MedalService;
use Common\Service\MemberMedalService;

class ExportController extends AbstractController
{
    /**
     * Export
     * @author tangxingguo
     * @desc 导出接口
     * @param Array dp_ids 组织ID
     * @param Array job_ids 岗位ID
     * @param Array role_ids 角色ID
     * @param String username 用户名
     * @return Array
     */
    public function index()
    {
        $rules = [
            'dp_ids' => 'array',
            'job_ids' => 'array',
            'role_ids' => 'array',
            'username' => 'max:64',
        ];

        // 验证请求数据
        $validate = new PackageValidate($rules, [], array_keys($rules));
        $postData = $validate->postData;

        // 条件
        $conds = [];
        if (isset($postData['username'])) {
            $conds['mem_username like ?'] = '%' . $postData['username'] . '%';
        }

        // 组织、岗位、角色转UID
        $condUids = &Integral::instance()->getUids($postData);
        if (!empty($postData['dp_ids']) || !empty($postData['job_ids']) || !empty($postData['role_ids'])) {
            if (empty($condUids)) {
                return true;
            }
        }
        $conds['mem_uid'] = $condUids;

        $medalServ = new MemberMedalService();
        $list = $medalServ->list_by_conds($conds,null);
        $all_uids = array_unique(array_filter(array_column($list, 'mem_uid')));
        // 去重列表
        $unList = [];

        $columns = ['姓名', '组织', '岗位', '角色', '勋章'];
        $rows = [];


        if (!empty($list)) {
            // 用户信息

            $uids = array_unique($all_uids);
            $users = &User::instance()->listAll(['memUids' => $uids]);
            $users = array_combine_by_key($users, 'memUid');

            // 所有勋章
            $medalModel = new MedalService();
            $medalList = $medalModel->list_all();
            $medalList = array_combine_by_key($medalList, 'im_id');

            // 组织、岗位、角色、勋章
            foreach ($list as $v) {
                // 勋章
                if (isset($medalList[$v['im_id']])) {
                    if (isset($unList[$v['mem_uid']])) {
                        $unList[$v['mem_uid']]['madel'][] = [
                            'name' => $medalList[$v['im_id']]['name'],
                            'num' => $v['im_total']
                        ];

                    } else {
                        // 组织、岗位、角色
                        if (isset($users[$v['mem_uid']])) {
                            $dpInfo = $users[$v['mem_uid']]['dpName'];
                            $v['dp_name'] = array_column($dpInfo, 'dpName');
                            $v['job_name'] = $users[$v['mem_uid']]['memJob'];
                            $v['role_name'] = $users[$v['mem_uid']]['memRole'];
                        }

                        $v['madel'][] = [
                            'name' => $medalList[$v['im_id']]['name'],
                            'num' => $v['im_total']
                        ];
                        $unList[$v['mem_uid']] = $v;
                    }
                }
            }

            // 行数据格式化
            foreach ($all_uids as $uid) {
                $madelStr = '';
                foreach ($unList[$uid]['madel'] as $madel) {
                    if ($madel['num'] > 1) {
                        $madelStr .= $madel['name'] . "({$madel['num']})";
                    } else {
                        $madelStr .= $madel['name'];
                    }

                    if (end($unList[$uid]['madel']) != $madel) {
                        $madelStr .= ';';
                    }
                }

                $rows[] = [
                    $unList[$uid]['mem_username'],
                    implode(';', $unList[$uid]['dp_name']),
                    $unList[$uid]['job_name'],
                    $unList[$uid]['role_name'],
                    $madelStr,
                ];
            }
        }

        $title = '员工勋章列表_' . rgmdate(MILLI_TIME, 'YmdHis') . '.xls';
        $filename = ExportDownload::get_down_dir($this->_login->user['eaId']) . $title . '.xls';
        $result = PythonExcel::instance()->write($filename, $columns, $rows);

        if ($result === true) {
            // 写入数据到下载中心
            $conditon = [
                'title' => $title,
                'ea_id' => $this->_login->user['eaId'],
                'type' => ExportDownload::EXCEL_TYPE,
                'size' => filesize($filename),
                'username' => $this->_login->user['eaRealname'],
                'url' => $filename,
                'app_dir' => APP_DIR
            ];

            ExportDownload::insert_down_load($conditon);
        }

        return true;
    }
}