PlanPicService.class.php 2.14 KB
<?php
/**
 * 照片墙照片表Service
 * @author: houyingcai
 * @email:     594609175@qq.com
 * @date :  2017-08-29 16:40:17
 * @version $Id$
 */

namespace Common\Service;

use Common\Model\PlanPicModel;

class PlanPicService extends AbstractService
{

    // 照片上传类型标识:手机端用户上传
    const PIC_UPLOAD_TYPE_USER = 0;
    // 照片上传类型标识:后台管理员上传
    const PIC_UPLOAD_TYPE_ADMIN = 1;

    // 图片名称最大长度
    const PIC_NAME_MAX_LENGTH = 120;

    /** @var PlanPicModel */
    protected $_d;

    public function __construct()
    {
        // 实例化照片model
        $this->_d = new PlanPicModel();

        parent::__construct();
    }

    /**
     * 更新点赞量
     * @author wanghuan
     *
     * @param int $pic_id 照片id
     * @param int $type 更新类型:0=加1,1=减1
     *
     * @return bool
     */
    public function update_likes($pic_id = 0, $type = 0)
    {

        return $this->_d->update_likes($pic_id, $type);
    }

    /**
     * 获取照片墙详情接口
     *
     * @author 蔡建华
     * @param array $params 查询条件
     *
     * @return array|bool
     */
    public function get_pic_list($params = [])
    {
        if (empty($params['plan_id'])) {

            E('_EMPTY_PLAN_PIC_ID');
        }

        // 倒序排列
        $order_option = ['pic_id' => 'asc'];

        $conds = ['plan_id' => $params['plan_id']];
        // 列表总数
        $total = $this->_d->count_by_conds($conds);

        if ($total > 0) {

            $fields = 'pic_name,pic_id,pic_at_id';
            $list = $this->_d->list_by_conds($conds, null, $order_option, $fields);
        }
        $this->format_pic_list($list);

        // 组装返回数
        return [
            'total' => intval($total),
            'list' => !empty($list) ? $list : [],
        ];
    }

    /**
     * 格式化图片
     *
     * @author 蔡建华
     * @param array $list 照片列表
     */
    protected function format_pic_list(&$list)
    {
        foreach ($list as &$val) {
            $val['pic_url'] = empty($val['pic_at_id']) ? '' : imgUrlReal($val['pic_at_id']);
        }
    }
}