PrizeAddController.class.php 5.04 KB
<?php
/**
 * Created by IntelliJ IDEA.
 * 奖品添加
 * User: zhoutao
 * Date: 2016-12-06
 * Time: 16:48:56
 */

namespace Apicp\Controller\Mall;

use Common\Common\AttachOperation;
use Common\Model\PrizeModel;
use Common\Service\PrizeService;

class PrizeAddController extends AbstractController
{
    public function before_action($action = '')
    {
        // 开启自动获取
        $this->autoGetData = true;
        // 获取的数据规则
        $this->field = [
            'ia_id' => [
                'require' => false,
                'verify' => 'intval',
                'cn' => '要编辑的奖品ID',
            ],
            'name' => [
                'require' => true,
                'verify' => 'strval',
                'cn' => '名称',
                'maxLength' => PrizeModel::MAX_NAME_COUNT
            ],
            'sequence' => [
                'require' => true,
                'verify' => 'intval',
                'cn' => '序号',
            ],
            'on_sale' => [
                'require' => false,
                'verify' => 'intval',
                'cn' => '状态',
            ],
            'reserve' => [
                'require' => true,
                'verify' => 'intval',
                'cn' => '库存',
            ],
            'integral' => [
                'require' => true,
                'verify' => 'intval',
                'cn' => '所需积分',
                'maxLength' => PrizeModel::MAX_INTEGRAL_LEN
            ],
            'times' => [
                'require' => false,
                'verify' => 'intval',
                'cn' => '每人限兑次数',
            ],
            'picture' => [
                'require' => true,
                'cn' => '图片',
                'maxLength' => PrizeModel::MAX_PICTURE_NUMBER
            ],
            'desc' => [
                'require' => true,
                'verify' => 'strval',
                'cn' => '介绍',
            ],
            'range_mem' => [
                'require' => false,
                'cn' => '人员范围',
            ],
            'range_dep' => [
                'require' => false,
                'cn' => '部门范围',
            ],
        ];

        return parent::before_action($action);
    }

    public function Index()
    {
        // 处理
        $this->dealData();

        if (!empty($this->data['ia_id'])) {
            // 更新
            $prizeServ = new PrizeService();
            $iaId = $this->data['ia_id'];
            unset($this->data['ia_id']);
            $prizeServ->update($iaId, $this->data);

            // 附件操作
            $this->attach_operation($iaId, $this->data, 'edit');
        } else {
            // 排序兼容
            $this->data['updated'] = MILLI_TIME;
            // 入库
            $prizeServ = new PrizeService();
            $iaId = $prizeServ->insert($this->data);

            // 附件操作
            $this->attach_operation($iaId, $this->data);
        }

        return true;
    }

    /**
     * 处理数据
     * @return bool
     */
    protected function dealData()
    {
        $this->data['picture'] = implode(',', $this->data['picture']);
        $this->data['range_mem'] = implode(',', $this->data['range_mem']);
        $this->data['range_dep'] = implode(',', $this->data['range_dep']);
        // 如果没有设置范围 则全公司
        if (empty($this->data['range_mem']) && empty($this->data['range_dep'])) {
            $this->data['range_mem'] = '';
            $this->data['range_dep'] = '';
            $this->data['is_all'] = self::MEAN_TRUE;
        } else {
            $this->data['is_all'] = self::MEAN_FALSE;
        }

        return true;
    }

    /**
     * 附件操作
     *
     * @param int $prize_id 奖品id
     * @param array $data 奖品数据
     * @param string $type 操作类型(add:添加,edit:编辑)
     *
     * @return bool
     */
    private function attach_operation($prize_id, $data, $type = 'add')
    {
        /**
         * 附件操作处理 start
         */
        $attach_serv = new AttachOperation();
        $attach_ids = [];

        if (!empty($data['picture'])) {
            // 奖品图片
            $attach_ids = explode(',', $data['picture']);
        }

        if (!empty($data['desc'])) {
            // 内容中的附件
            $content_at_ids = $attach_serv->match_at_ids($data['desc']);
            $attach_ids = array_merge($attach_ids, $content_at_ids);
        }

        if (!empty($attach_ids)) {
            // 添加
            if ($type == 'add') {
                $attach_serv->insert_attach(
                    APP_DIR,
                    'prize',
                    $prize_id,
                    ['attach_ids' => $attach_ids]
                );
            }
            // 编辑
            if ($type == 'edit') {
                $attach_serv->update_attach(
                    APP_DIR,
                    'prize',
                    $prize_id,
                    ['attach_ids' => $attach_ids]
                );
            }
        }

        return true;
    }
}