MElement.Class.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * 元素模型
  4. * Created by PhpStorm.
  5. * User: QianNiao-C
  6. * Date: 2019/10/25
  7. * Time: 15:02
  8. */
  9. namespace JinDouYun\Model\BlockManage;
  10. use Mall\Framework\Core\ErrorCode;
  11. use Mall\Framework\Core\ResultWrapper;
  12. use Mall\Framework\Core\StatusCode;
  13. use JinDouYun\Dao\BlockManage\DElement;
  14. class MElement
  15. {
  16. public $objDElement;
  17. public function __construct()
  18. {
  19. $this->objDElement = new DElement('default');
  20. }
  21. /**
  22. * 添加区块元素
  23. *
  24. * @param array $params 数据
  25. *
  26. * @return ResultWrapper
  27. */
  28. public function addElement($params)
  29. {
  30. $id = $this->objDElement->insert($params);
  31. if ($id === false) {
  32. return ResultWrapper::fail($this->objDElement->error(), ErrorCode::$dberror);
  33. }
  34. return ResultWrapper::success($id);
  35. }
  36. /**
  37. * 获取指定元素信息
  38. * @param $id
  39. * @return ResultWrapper
  40. */
  41. public function getElementInfoById($id)
  42. {
  43. $dbResult = $this->objDElement->get_by('id', $id);
  44. if ($dbResult === false) {
  45. return ResultWrapper::fail($this->objDElement->error(), ErrorCode::$dberror);
  46. }
  47. return ResultWrapper::success($dbResult);
  48. }
  49. /**
  50. * 编辑指定元素
  51. *
  52. * @param int|array $params 修改元素的信息
  53. *
  54. * @return ResultWrapper
  55. */
  56. public function editElement($params)
  57. {
  58. if (empty($params['id'])) {
  59. return ResultWrapper::fail('没有指定要修改的元素字段id', ErrorCode::$paramError);
  60. }
  61. $id = $params['id'];
  62. unset($params['id']);
  63. $dbResult = $this->objDElement->update($params, ['id' => $id]);
  64. if ($dbResult === false) {
  65. return ResultWrapper::fail($this->objDElement->error(), ErrorCode::$dberror);
  66. }
  67. return ResultWrapper::success($dbResult);
  68. }
  69. /**
  70. * 删除指定元素
  71. *
  72. * @param array $ids 要删除元素字段id
  73. *
  74. * @return ResultWrapper
  75. */
  76. public function delElement($ids)
  77. {
  78. if (empty($ids)) {
  79. return ResultWrapper::fail('参数错误', ErrorCode::$paramError);
  80. }
  81. $dbResult = $this->objDElement->update(['deleteStatus' => StatusCode::$delete], $ids);
  82. if ($dbResult === false) {
  83. return ResultWrapper::fail($this->objDElement->error(), ErrorCode::$dberror);
  84. }
  85. return ResultWrapper::success($dbResult);
  86. }
  87. /**
  88. * 获取指定区块下的所有元素
  89. * @param $selectParams
  90. * @return ResultWrapper
  91. */
  92. public function getElementByZoneId($selectParams)
  93. {
  94. $time = time();//当前时间
  95. $limit = $selectParams['limit'];
  96. unset($selectParams['limit']);
  97. $offset = $selectParams['offset'];
  98. unset($selectParams['offset']);
  99. $sql = "`zoneId` = " . $selectParams['zoneId'] . " AND `deleteStatus` = " . StatusCode::$standard;
  100. if (!isset($selectParams['type'])) {
  101. $sql .= " AND `startTime` <= " . $time . " AND `endTime` >= " . $time;//后台用
  102. }
  103. //$selectParams['deleteStatus'] = StatusCode::$standard;
  104. $dbResult = $this->objDElement->select($sql, '*', 'sort DESC', $limit, $offset);
  105. if ($dbResult === false) {
  106. return ResultWrapper::fail($this->objDElement->error(), ErrorCode::$dberror);
  107. }
  108. $total = $this->objDElement->count($sql);
  109. $return = [
  110. 'data' => self::format($dbResult),
  111. 'total' => ($total) ? intval($total) : 0,
  112. ];
  113. return ResultWrapper::success($return);
  114. }
  115. /**
  116. * 格式化返回数据
  117. * @param $data
  118. * @return array
  119. */
  120. private function format($data)
  121. {
  122. foreach ($data as &$v) {
  123. if (isset($v['elementData'])) {
  124. $v['elementData'] = json_decode($v['elementData'], true);
  125. if (!empty($v['elementData'])) {
  126. if (!is_array($v['elementData'])) {
  127. return $data;
  128. }
  129. foreach ($v['elementData'] as &$val) {
  130. if (in_array($val['key'], ['image', 'img'])) {
  131. $val['val'] = URL_UPLOAD . $val['val'];
  132. }
  133. }
  134. }
  135. }
  136. }
  137. return $data;
  138. }
  139. }