GroupDataDao.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 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()->where('mer_id', $merId)->where('group_id', $groupId)->where('status', 1)->order('sort DESC');
  62. if (!is_null($page)) $query->page($page, $limit);
  63. $groupData = [];
  64. foreach ($query->column('value') as $k => $v) {
  65. $groupData[] = json_decode($v, true);
  66. }
  67. return $groupData;
  68. }
  69. public function groupDataCount($merId, $groupId)
  70. {
  71. return SystemGroupData::getDB()->where('mer_id', $merId)->where('group_id', $groupId)->where('status', 1)->count();
  72. }
  73. /**
  74. * @param $merId
  75. * @param $groupId
  76. * @param int|null $page
  77. * @param int|null $limit
  78. * @return array
  79. * @author xaboy
  80. * @day 2020/6/3
  81. */
  82. public function getGroupDataId($merId, $groupId, ?int $page = null, ?int $limit = 10)
  83. {
  84. $query = SystemGroupData::getDB()->where('mer_id', $merId)->where('group_id', $groupId)->where('status', 1)->order('sort DESC');
  85. if (!is_null($page)) $query->page($page, $limit);
  86. $groupData = [];
  87. foreach ($query->column('value', 'group_data_id') as $k => $v) {
  88. $groupData[] = ['id' => $k, 'data' => json_decode($v, true)];
  89. }
  90. return $groupData;
  91. }
  92. /**
  93. * @param $merId
  94. * @param $id
  95. * @param $data
  96. * @return int
  97. * @throws DbException
  98. * @author xaboy
  99. * @day 2020-03-30
  100. */
  101. public function merUpdate($merId, $id, $data)
  102. {
  103. $data['value'] = json_encode($data['value']);
  104. return SystemGroupData::getDB()->where('group_data_id', $id)->where('mer_id', $merId)->update($data);
  105. }
  106. /**
  107. * @param $merId
  108. * @param $id
  109. * @return int
  110. * @throws DbException
  111. * @author xaboy
  112. * @day 2020-03-30
  113. */
  114. public function merDelete($merId, $id)
  115. {
  116. return SystemGroupData::getDB()->where('mer_id', $merId)->where('group_data_id', $id)->delete();
  117. }
  118. /**
  119. * @param int $merId
  120. * @param int $id
  121. * @return bool
  122. * @author xaboy
  123. * @day 2020-04-02
  124. */
  125. public function merExists(int $merId, int $id)
  126. {
  127. return ($this->getModel())::getDB()->where('mer_id', $merId)->where($this->getPk(), $id)->count() > 0;
  128. }
  129. /**
  130. * @param int $groupId
  131. * @return int
  132. * @throws DbException
  133. * @author xaboy
  134. * @day 2020-05-16
  135. */
  136. public function clearGroup(int $groupId)
  137. {
  138. return SystemGroupData::getDB()->where('group_id', $groupId)->delete();
  139. }
  140. /**
  141. * @param $id
  142. * @param $merId
  143. * @return array|Model|null
  144. * @throws DbException
  145. * @throws DataNotFoundException
  146. * @throws ModelNotFoundException
  147. * @author xaboy
  148. * @day 2020/6/2
  149. */
  150. public function merGet($id, $merId)
  151. {
  152. $data = SystemGroupData::getDB()->where('group_data_id', $id)->where('mer_id', $merId)->find();
  153. return $data ? $data['value'] : null;
  154. }
  155. }