PrizeListController.class.php 4.09 KB
<?php
/**
 * 导出中奖列表
 * User: daijun
 * Date: 18/3/24
 * Time: 下午3:44
 */

namespace Apicp\Controller\Export;

use Common\Common\Constant;
use Com\PythonExcel;
use Common\Service\ActivityService;
use Common\Common\ExportDownload;
use Common\Service\RecordService;

class PrizeListController extends \Apicp\Controller\AbstractController
{

    public function Index()
    {
        $params = I('post.');

        if (empty($params['ac_id'])) {
            // 活动ID不能为空
            E('_EMPTY_ACTIVITY_ID');
        }

        $activity_serv = new ActivityService();

        $detail = $activity_serv->get($params['ac_id']);

        if (empty($detail)) {
            // 活动信息不存在
            E('_ERR_ACTIVITY_DATA');
        }

        if (!isset($params['type']) || !in_array($params['type'], [Constant::HAVE_PRIZE, Constant::NO_PRIZE])) {
            // 如果type为空或者不是特定的数值【未中奖,已中奖】
            return true;
        }

        $record_serv = new RecordService();
        // 组织查询条件
        $where = $record_serv->get_where($params);

        // 时间倒序
        $order_option = ['lr_id' => 'DESC'];

        // 定义查询字段
        $fields = 'lr_id,lp_id,lp_name,is_prize,username,dp_name,mobile,role,job,remark,created';

        // 查询列表数据
        $list = $record_serv->list_by_conds($where, [], $order_option, $fields);

        // 生成下载Excel
        $this->_download($list, $params['type'], $detail['title']);

        return true;
    }


    /**
     * 导出模板
     *
     * @param array $list 导出数据
     * @param int $type 是否中奖(0:中奖,1:未中奖)
     * @param string $ac_title 活动标题
     *
     * @return bool
     */
    protected function _download($list = [], $type = 0, $ac_title = '')
    {

        $rows = [];

        $rand_str = substr(md5(QY_DOMAIN), 0, 1) . substr(md5(QY_DOMAIN), -1);

        if ($type == Constant::NO_PRIZE) {
            // 未中奖
            $file_name = $ac_title . '未中奖记录_' . rgmdate(strval(MILLI_TIME), 'YmdHis') . $rand_str; // 导出文件名称

            // Excel 表头字段
            $title = [
                '抽奖时间',
                '姓名',
                '组织',
                '手机号',
                '抽奖结果',
            ];

            // 回帖数据处理
            foreach ($list as $v) {

                $rows[] = [
                    rgmdate(strval($v['created']), 'Y/m/d H:i'),
                    $v['username'],
                    $v['dp_name'],
                    " " . $v['mobile'],
                    '谢谢参与',
                ];
            }

        } else {
            // 中奖

            $file_name = $ac_title . '中奖记录_' . rgmdate(strval(MILLI_TIME), 'YmdHis') . $rand_str; // 导出文件名称
            // Excel 表头字段
            $title = [
                '抽奖时间',
                '姓名',
                '组织',
                '手机号',
                '抽奖结果',
                '备注信息',
            ];
            // 回帖数据处理
            foreach ($list as $v) {

                $rows[] = [
                    rgmdate(strval($v['created']), 'Y/m/d H:i'),
                    $v['username'],
                    $v['dp_name'],
                    " " . $v['mobile'],
                    $v['lp_name'],
                    $v['remark'],
                ];
            }
        }

        unset($list);

        $dir = DATA_PATH;

        // 生成Excel 下载
        $real_path = $dir . D_S . $file_name . '.xls';
        $ret = PythonExcel::instance()->write($real_path, $title, $rows);

        if ($ret) {

            $data = [
                'title' => $file_name,
                'ea_id' => $this->_login->user['eaId'],
                'type' => ExportDownload::EXCEL_TYPE,
                'username' => $this->_login->user['eaRealname'],
                'url' => $real_path,
                'app_dir' => APP_DIR
            ];

            ExportDownload::insert_down_load($data);
        }

        return true;
    }

}