GroupDataDao.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\dao\system\groupData;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\BaseModel;
  14. use app\common\model\system\groupData\SystemGroupData;
  15. use think\db\BaseQuery;
  16. use think\db\exception\DataNotFoundException;
  17. use think\db\exception\DbException;
  18. use think\db\exception\ModelNotFoundException;
  19. use think\Model;
  20. /**
  21. * Class GroupDataDao
  22. * @package app\common\dao\system\groupData
  23. * @author xaboy
  24. * @day 2020-03-27
  25. */
  26. class GroupDataDao extends BaseDao
  27. {
  28. /**
  29. * @return BaseModel
  30. * @author xaboy
  31. * @day 2020-03-30
  32. */
  33. protected function getModel(): string
  34. {
  35. return SystemGroupData::class;
  36. }
  37. /**
  38. * @param $merId
  39. * @param $groupId
  40. * @return BaseQuery
  41. * @author xaboy
  42. * @day 2020-03-30
  43. */
  44. public function getGroupDataWhere($merId, $groupId): BaseQuery
  45. {
  46. return SystemGroupData::getDB()->withAttr('value', function ($val) {
  47. return json_decode($val, true);
  48. })->where('mer_id', $merId)->where('group_id', $groupId)->order('sort DESC');
  49. }
  50. /**
  51. * @param $merId
  52. * @param $groupId
  53. * @param int|null $page
  54. * @param int|null $limit
  55. * @return array
  56. * @author xaboy
  57. * @day 2020/5/27
  58. */
  59. public function getGroupData($merId, $groupId, ?int $page = null, ?int $limit = 10)
  60. {
  61. $query = SystemGroupData::getDB()
  62. ->where('mer_id', $merId)->where('group_id', $groupId)
  63. ->where('status', 1)
  64. ->where('')
  65. ->order('sort DESC');
  66. if (!is_null($page)) $query->page($page, $limit);
  67. $groupData = [];
  68. foreach ($query->column('value,mer_id', 'group_data_id') as $k => $v) {
  69. $groupData[] = json_decode($v['value'], true) + ['group_data_id' => $k, 'group_mer_id' => $v['mer_id']];
  70. }
  71. return $groupData;
  72. }
  73. public function getData($id)
  74. {
  75. $res = SystemGroupData::getDB()->where('group_data_id', (int)$id)->find();
  76. if (!$res) return null;
  77. return $res['value'] + ['group_data_id' => $res['group_data_id'], 'group_mer_id' => $res['mer_id']];
  78. }
  79. public function groupDataCount($merId, $groupId)
  80. {
  81. return SystemGroupData::getDB()->where('mer_id', $merId)->where('group_id', $groupId)->where('status', 1)->count();
  82. }
  83. /**
  84. * @param $merId
  85. * @param $groupId
  86. * @param int|null $page
  87. * @param int|null $limit
  88. * @return array
  89. * @author xaboy
  90. * @day 2020/6/3
  91. */
  92. public function getGroupDataId($merId, $groupId, ?int $page = null, ?int $limit = 10)
  93. {
  94. $query = SystemGroupData::getDB()->where('mer_id', $merId)->where('group_id', $groupId)->where('status', 1)->order('sort DESC');
  95. if (!is_null($page)) $query->page($page, $limit);
  96. $groupData = [];
  97. foreach ($query->column('value', 'group_data_id') as $k => $v) {
  98. $groupData[] = ['id' => $k, 'data' => json_decode($v, true)];
  99. }
  100. return $groupData;
  101. }
  102. /**
  103. * @param $merId
  104. * @param $id
  105. * @param $data
  106. * @return int
  107. * @throws DbException
  108. * @author xaboy
  109. * @day 2020-03-30
  110. */
  111. public function merUpdate($merId, $id, $data)
  112. {
  113. $data['value'] = json_encode($data['value']);
  114. return SystemGroupData::getDB()->where('group_data_id', $id)->where('mer_id', $merId)->update($data);
  115. }
  116. /**
  117. * @param $merId
  118. * @param $id
  119. * @return int
  120. * @throws DbException
  121. * @author xaboy
  122. * @day 2020-03-30
  123. */
  124. public function merDelete($merId, $id)
  125. {
  126. return SystemGroupData::getDB()->where('mer_id', $merId)->where('group_data_id', $id)->delete();
  127. }
  128. /**
  129. * @param int $merId
  130. * @param int $id
  131. * @return bool
  132. * @author xaboy
  133. * @day 2020-04-02
  134. */
  135. public function merExists(int $merId, int $id)
  136. {
  137. return ($this->getModel())::getDB()->where('mer_id', $merId)->where($this->getPk(), $id)->count() > 0;
  138. }
  139. /**
  140. * @param int $groupId
  141. * @return int
  142. * @throws DbException
  143. * @author xaboy
  144. * @day 2020-05-16
  145. */
  146. public function clearGroup(int $groupId)
  147. {
  148. return SystemGroupData::getDB()->where('group_id', $groupId)->delete();
  149. }
  150. /**
  151. * @param $id
  152. * @param $merId
  153. * @return array|Model|null
  154. * @throws DbException
  155. * @throws DataNotFoundException
  156. * @throws ModelNotFoundException
  157. * @author xaboy
  158. * @day 2020/6/2
  159. */
  160. public function merGet($id, $merId)
  161. {
  162. $data = SystemGroupData::getDB()->where('group_data_id', $id)->where('mer_id', $merId)->find();
  163. return $data ? $data['value'] : null;
  164. }
  165. }