ExcListController.class.php 5.66 KB
<?php
/**
 * Created by IntelliJ IDEA.
 * 兑奖列表
 * User: gaoyaqiu
 * Date: 2016-12-09
 */

namespace Apicp\Controller\Mall;

use Common\Common\User;
use Common\Model\ConvertModel;
use Common\Service\ConvertService;
use Common\Service\PrizeService;
use VcySDK\Attach;
use VcySDK\Service;

class ExcListController extends AbstractController
{
    public function before_action($action = '')
    {
        // 开启自动获取
        $this->autoGetData = true;
        $this->field = [
            'ia_id' => [
                'require' => false,
                'verify' => 'intval',
                'cn' => '奖品ID',
            ],
            'name' => [
                'require' => false,
                'verify' => 'strval',
                'cn' => '奖品名称',
            ],
            'memUserName' => [
                'require' => false,
                'verify' => 'strval',
                'cn' => '姓名',
            ],
            'convert_status' => [
                'require' => false,
                'verify' => 'intval',
                'cn' => '兑奖状态',
                'area' => [
                    ConvertModel::CONVERT_STATUS_ING,
                    ConvertModel::CONVERT_STATUS_AGREE,
                    ConvertModel::CONVERT_STATUS_DEFUSE,
                    ConvertModel::CONVERT_STATUS_CANCEL,
                ],
            ],
            'dep' => [
                'require' => false,
                'verify' => 'strval',
                'cn' => '部门ID',
            ],
            'number' => [
                'require' => false,
                'verify' => 'strval',
                'cn' => '兑换编码',
            ],
            'applicant_phone' => [
                'require' => false,
                'verify' => 'strval',
                'cn' => '手机号',
            ],
            'startTime' => [
                'require' => false,
                'verify' => 'intval',
                'cn' => '申请开始时间',
            ],
            'endTime' => [
                'require' => false,
                'verify' => 'intval',
                'cn' => '申请结束时间',
            ],
            'page' => [
                'require' => false,
                'default' => 1,
                'verify' => 'intval',
            ],
            'limit' => [
                'require' => false,
                'default' => 15,
                'verify' => 'intval',
            ],
        ];

        return parent::before_action($action);
    }

    public function Index()
    {

        // 分页参数
        list($start, $perpage, $this->_result['page']) = $this->getPageOption('page', 'limit');
        $convertServ = new ConvertService();

        // 过滤用户
        $uidArray = array();
        // 根据用户名查询
        $memUserName = $this->data['memUserName'];
        // 按部门查询
        $dpId = $this->data['dep'];
        $innserParams = array();

        if (!empty($memUserName) || !empty($dpId)) {
            $memServ = new User();
            $innserParams['dpId'] = $dpId;
            $innserParams['memUsername'] = $memUserName;
            $userData = $memServ->listByConds($innserParams, 1, 1);
            if (!empty($userData) && $userData['total'] > 0) {
                $userData = $memServ->listByConds($innserParams, 1, $userData['total']);
                $uidArray = array_column($userData['list'], 'memUid');
            }

            if (empty($uidArray)) {
                $this->_result['list'] = array();
                $this->_result['total'] = 0;

                return true;
            }
            $this->data['uids'] = $uidArray;
        }

        // 兑奖列表
        $this->_result['list'] = $convertServ->getPrizeConvertPageList($this->data, [$start, $perpage]);
        $this->_result['total'] = $convertServ->countPrizeConvert($this->data);
        // 处理返回值
        $this->dealResult();

        return true;
    }

    /**
     * 处理返回值
     * @return bool
     */
    protected function dealResult()
    {
        // 所有奖品 ID
        $aiIds = array_column($this->_result['list'], 'ia_id');
        $prizeList = [];
        $attList = [];
        if (!empty($aiIds)) {
            $prizeServ = new PrizeService();
            // 获取奖品
            $prizeList = $prizeServ->list_by_pks($aiIds);
            $prizeList = array_combine_by_key($prizeList, 'ia_id');
            $allAtId = [];
            foreach ($prizeList as &$prize) {
                $prize['picture'] = explode(',', $prize['picture']);
                // 获取所有的奖品图片
                $allAtId = array_merge($allAtId, $prize['picture']);
            }
            // 获取奖品图片地址
            if (!empty($allAtId)) {
                $attSdk = new Attach(Service::instance());
                $attList = $attSdk->listAll(['atIds' => array_unique($allAtId)]);
                $attList = array_combine_by_key($attList['list'], 'atId');
            }
        }

        $memServ = new User();
        foreach ($this->_result['list'] as &$item) {
            // 获取用户
            $member = $memServ->getByUid($item['uid']);
            if (!empty($member)) {
                $item['memUserName'] = $member['memUsername'];
                // 获取部门名称
                $depName = array_column($member['dpName'], 'dpName');
                $item['dpName'] = implode(';', $depName);
            }

            // 获取奖品图片
            $atId = empty($prizeList[$item['ia_id']]['picture'][0]) ? '' : $prizeList[$item['ia_id']]['picture'][0];
            $item['picture'] = empty($attList[$atId]['atAttachment']) ? '' : $attList[$atId]['atAttachment'];
        }

        return true;
    }
}