123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- <?php
- /**
- * 元素模型
- * Created by PhpStorm.
- * User: QianNiao-C
- * Date: 2019/10/25
- * Time: 15:02
- */
- namespace JinDouYun\Model\BlockManage;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\ResultWrapper;
- use Mall\Framework\Core\StatusCode;
- use JinDouYun\Dao\BlockManage\DElement;
- class MElement
- {
- public $objDElement;
- public function __construct()
- {
- $this->objDElement = new DElement('default');
- }
- /**
- * 添加区块元素
- *
- * @param array $params 数据
- *
- * @return ResultWrapper
- */
- public function addElement($params)
- {
- $id = $this->objDElement->insert($params);
- if ($id === false) {
- return ResultWrapper::fail($this->objDElement->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($id);
- }
- /**
- * 获取指定元素信息
- * @param $id
- * @return ResultWrapper
- */
- public function getElementInfoById($id)
- {
- $dbResult = $this->objDElement->get_by('id', $id);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDElement->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 编辑指定元素
- *
- * @param int|array $params 修改元素的信息
- *
- * @return ResultWrapper
- */
- public function editElement($params)
- {
- if (empty($params['id'])) {
- return ResultWrapper::fail('没有指定要修改的元素字段id', ErrorCode::$paramError);
- }
- $id = $params['id'];
- unset($params['id']);
- $dbResult = $this->objDElement->update($params, ['id' => $id]);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDElement->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 删除指定元素
- *
- * @param array $ids 要删除元素字段id
- *
- * @return ResultWrapper
- */
- public function delElement($ids)
- {
- if (empty($ids)) {
- return ResultWrapper::fail('参数错误', ErrorCode::$paramError);
- }
- $dbResult = $this->objDElement->update(['deleteStatus' => StatusCode::$delete], $ids);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDElement->error(), ErrorCode::$dberror);
- }
- return ResultWrapper::success($dbResult);
- }
- /**
- * 获取指定区块下的所有元素
- * @param $selectParams
- * @return ResultWrapper
- */
- public function getElementByZoneId($selectParams)
- {
- $time = time();//当前时间
- $limit = $selectParams['limit'];
- unset($selectParams['limit']);
- $offset = $selectParams['offset'];
- unset($selectParams['offset']);
- $sql = "`zoneId` = " . $selectParams['zoneId'] . " AND `deleteStatus` = " . StatusCode::$standard;
- if (!isset($selectParams['type'])) {
- $sql .= " AND `startTime` <= " . $time . " AND `endTime` >= " . $time;//后台用
- }
- //$selectParams['deleteStatus'] = StatusCode::$standard;
- $dbResult = $this->objDElement->select($sql, '*', 'sort DESC', $limit, $offset);
- if ($dbResult === false) {
- return ResultWrapper::fail($this->objDElement->error(), ErrorCode::$dberror);
- }
- $total = $this->objDElement->count($sql);
- $return = [
- 'data' => self::format($dbResult),
- 'total' => ($total) ? intval($total) : 0,
- ];
- return ResultWrapper::success($return);
- }
- /**
- * 格式化返回数据
- * @param $data
- * @return array
- */
- private function format($data)
- {
- foreach ($data as &$v) {
- if (isset($v['elementData'])) {
- $v['elementData'] = json_decode($v['elementData'], true);
- if (!empty($v['elementData'])) {
- if (!is_array($v['elementData'])) {
- return $data;
- }
- foreach ($v['elementData'] as &$val) {
- if (in_array($val['key'], ['image', 'img'])) {
- $val['val'] = URL_UPLOAD . $val['val'];
- }
- }
- }
- }
- }
- return $data;
- }
- }
|