SaveController.class.php 1.61 KB
<?php
/**
 * Created by PhpStorm.
 * User: zhonglei
 * Date: 2018/3/26
 * Time: 11:41
 */

namespace Apicp\Controller\Dict;

use Com\PackageValidate;
use Common\Service\DictService;

class SaveController extends AbstractController
{
    /**
     * Save
     * @author zhonglei
     * @desc 保存字典
     * @param int dict_id:true 字典ID
     * @param String key:true 字典Key(teacher_title=讲师头衔;teacher_task_type=讲师授课任务类型)
     * @param String value:true 字典值
     * @return Array
     * array(
     *     'dict_id' => 1 // 字典ID
     * )
     */
    public function Index_post()
    {
        $rules = [
            'dict_id' => 'require|integer',
            'key' => 'require|max:64',
            'value' => 'require|max:64',
        ];

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

        $dictServ = new DictService();
        $dict_id = $post_data['dict_id'];
        unset($post_data['dict_id']);
        $conds = $post_data;

        // 编辑
        if ($dict_id > 0) {
            if (empty($dictServ->get($dict_id))) {
                E('_ERR_DICT_NOT_FOUND');
            }

            $conds['dict_id != ?'] = $dict_id;
        }

        // 判断重复数据
        if (!empty($dictServ->get_by_conds($conds))) {
            E('_ERR_DICT_EXIST');
        }

        // 保存
        if ($dict_id == 0) {
            $dict_id = $dictServ->insert($post_data);
        } else {
            $dictServ->update($dict_id, $post_data);
        }

        $this->_result['dict_id'] = $dict_id;
    }
}