Service.class.php 9.79 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
<?php

/**
 * Service.class.php
 * $author$
 */
namespace Com;

abstract class Service
{

    /**
     * 默认数据操作类实例
     *
     * @type null|Model
     */
    protected $_d = null;

    // 构造方法
    public function __construct($name = '')
    {
        // do nothing.
    }

    // 获取错误码
    public function get_errcode()
    {

        return Error::instance()->get_errcode();
    }

    // 获取错误信息
    public function get_errmsg()
    {

        return Error::instance()->get_errmsg();
    }

    /**
     * 设置错误信息
     *
     * @param mixed $message 错误信息
     * @param int $code 错误号
     *
     * @return bool
     */
    protected function _set_error($message, $code = 0)
    {

        Error::instance()->set_error($message, $code);

        return true;
    }

    // 获取表字段前缀
    public function get_prefield()
    {

        static $prefield = null;
        // 如果前缀不为 null, 则直接返回
        if (null != $prefield) {
            return $prefield;
        }
        // 如果 model 未初始化
        if (null == $this->_d) {
            E('_ERR_SERVICE_MODEL_UN_INIT');

            return false;
        }
        $prefield = $this->_d->prefield;

        return $prefield;
    }

    /**
     * 根据主键值获取单条数据
     *
     * @param string $val 值
     * @param bool $withOutDomain 跳过企业标识查询
     * @return array|bool
     */
    public function get($val, $withOutDomain = false)
    {

        try {
            // 设置条件
            return $this->_d->get($val, $withOutDomain);
        } catch (\Exception $e) {
            E($e->getCode() . ":" . $e->getMessage());

            return false;
        }
    }

    /**
     * 获取数据列表
     *
     * @param int|array $page_option 分页参数
     *                                + int => limit $page_option
     *                                + array => limit $page_option[0], $page_option[1]
     * @param array $order_option 排序信息
     *
     * @return array|bool
     */
    public function list_all($page_option = null, $order_option = array())
    {

        try {
            return $this->_d->list_all($page_option, $order_option);
        } catch (\Exception $e) {
            E($e->getCode() . ":" . $e->getMessage());

            return false;
        }
    }

    /**
     * 获取数据列表
     *
     * @param int|array $page_option 分页参数
     *                                + int => limit $page_option
     *                                + array => limit $page_option[0], $page_option[1]
     * @param array $order_option 排序信息
     *
     * @return array|bool
     */
    public function list_all_without_domain($page_option = null, $order_option = array())
    {

        try {
            return $this->_d->list_all_without_domain($page_option, $order_option);
        } catch (\Exception $e) {
            E($e->getCode() . ":" . $e->getMessage());

            return false;
        }
    }

    /**
     * 删除字段
     *
     * @param mixed $vals 主键对应的值
     *
     * @return object
     */
    public function delete($vals)
    {

        try {
            return $this->_d->delete($vals);
        } catch (\Exception $e) {
            E($e->getCode() . ":" . $e->getMessage());

            return false;
        }
    }

    /**
     * 更新数据
     *
     * @param int $val 主键键值
     * @param array $data 待更新数据
     * @param bool $withOutDomain 排除企业标识
     *
     * @return bool
     */
    public function update($val = null, $data = array(), $withOutDomain = false)
    {

        try {
            return $this->_d->update($val, $data, $withOutDomain);
        } catch (\Exception $e) {
            E($e->getCode() . ":" . $e->getMessage());

            return false;
        }
    }

    /**
     * 更新数据 (不查询 企业标识)
     *
     * @param int $val 主键键值
     * @param array $data 待更新数据
     *
     * @return bool
     */
    public function updateWithOutDomain($val = null, $data = array())
    {

        try {
            return $this->_d->updateWithOutDomain($val, $data);
        } catch (\Exception $e) {
            E($e->getCode() . ":" . $e->getMessage());

            return false;
        }
    }

    /**
     * 统计总数
     *
     * @param mixed $page_options 分页选项
     *
     * @return array|bool
     */
    public function count($page_options = 0)
    {

        try {
            return $this->_d->count($page_options);
        } catch (\Exception $e) {
            E($e->getCode() . ":" . $e->getMessage());

            return false;
        }
    }

    /**
     * 根据主键值读取数据
     *
     * @param int|string|array $vals 查询条件
     * @param array $orders 排序
     *
     * @return array|bool
     */
    public function list_by_pks($vals, $orders = array())
    {

        try {
            return $this->_d->list_by_pks($vals, $orders);
        } catch (\Exception $e) {
            E($e->getCode() . ":" . $e->getMessage());

            return false;
        }
    }

    /**
     * 根据条件读取数据
     * @param array $conds 查询条件数组
     * @param array $order_option 排序数组
     * @param bool $withOutDomain 排除企业标识字段
     * @return array|bool
     */
    public function get_by_conds($conds, $order_option = array(), $withOutDomain = false, $withOutStatus = false)
    {

        try {
            return $this->_d->get_by_conds($conds, $order_option, $withOutDomain);
        } catch (\Exception $e) {
            E($e->getCode() . ":" . $e->getMessage());

            return false;
        }
    }

    /**
     * 根据条件读取数据数组
     *
     * @param array $conds 条件数组
     * @param int|array $page_option 分页参数
     * @param array $order_option 排序
     * @param string $field 字段信息
     * @param array $extOption
     *                  withOutDomain true 跳过 domain 字段查询
     *                  withOutStatus true 跳过 domain 字段查询
     * @return array|bool
     */
    public function list_by_conds($conds, $page_option = null, $order_option = array(), $field = '*', $extOption = [])
    {

        try {
            return $this->_d->list_by_conds($conds, $page_option, $order_option, $field, $extOption);
        } catch (\Exception $e) {
            E($e->getCode() . ":" . $e->getMessage());

            return false;
        }
    }

    /**
     * 没有企业标识的查询
     * @param        $conds
     * @param null   $page_option
     * @param array  $order_option
     * @param string $fields
     * @return array|bool
     */
    public function listWithOutDomian($conds, $page_option = null, $order_option = array(), $fields = '*')
    {

        try {
            return $this->_d->listWithOutDomian($conds, $page_option, $order_option, $fields);
        } catch (\Exception $e) {
            E($e->getCode() . ":" . $e->getMessage());

            return false;
        }
    }

    /**
     * 根据条件更新数据
     *
     * @param array $conds 条件数组
     * @param array $data 数据数组
     * @param bool $withOutDomain 不带企业标识
     * @return bool
     */
    public function update_by_conds($conds, $data, $withOutDomain = false)
    {

        try {
            return $this->_d->update_by_conds($conds, $data, $withOutDomain);
        } catch (\Exception $e) {
            E($e->getCode() . ":" . $e->getMessage());

            return false;
        }
    }

    /**
     * 根据条件删除数据
     *
     * @param array $conds 删除条件数组
     *
     * @return bool
     */
    public function delete_by_conds($conds)
    {

        try {
            return $this->_d->delete_by_conds($conds);
        } catch (\Exception $e) {
            E($e->getCode() . ":" . $e->getMessage());

            return false;
        }
    }

    /**
     * 根据条件计算数量
     *
     * @param        $conds
     * @param string $field
     * @param bool   $withOutDomain 排除企业标识字段
     * @param bool   $withOutStatus 排除数据状态字段
     * @return bool|number
     * @throws service_exception
     */
    public function count_by_conds($conds, $field = '*', $withOutDomain = false, $withOutStatus = false)
    {

        try {
            return $this->_d->count_by_conds($conds, $field, $withOutDomain, $withOutStatus);
        } catch (\Exception $e) {
            E($e->getCode() . ":" . $e->getMessage());

            return false;
        }
    }

    /**
     * 插入单条数据
     *
     * @param array $data 待插入数据
     * @param array $options 表达式
     * @param boolean $replace 是否使用 REPLACE INTO
     *
     * @return \Think\mixed
     */
    public function insert($data, $options = array(), $replace = false)
    {

        try {
            return $this->_d->insert($data, $options, $replace);
        } catch (\Exception $e) {
            E($e->getCode() . ":" . $e->getMessage());

            return false;
        }
    }

    /**
     * 插入多条数据
     *
     * @param array $list 待插入数据数组
     * @param array $options 表达式
     * @param boolean $replace 是否使用 REPLACE INTO
     *
     * @return bool|Ambigous
     */
    public function insert_all($list, $options = array(), $replace = false)
    {

        try {
            return $this->_d->insert_all($list, $options, $replace);
        } catch (\Exception $e) {
            E($e->getCode() . ":" . $e->getMessage());

            return false;
        }
    }

    // 开始存储过程
    public function start_trans()
    {

        return $this->_d->start_trans();
    }

    // 回滚
    public function rollback()
    {

        return $this->_d->rollback();
    }

    // 提交
    public function commit()
    {

        return $this->_d->commit();
    }
}