GroupDataDao.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. namespace app\common\dao\system\groupData;
  3. use app\common\dao\BaseDao;
  4. use app\common\model\BaseModel;
  5. use app\common\model\system\groupData\SystemGroupData;
  6. use think\db\BaseQuery;
  7. use think\db\exception\DataNotFoundException;
  8. use think\db\exception\DbException;
  9. use think\db\exception\ModelNotFoundException;
  10. use think\Model;
  11. /**
  12. * Class GroupDataDao
  13. * @package app\common\dao\system\groupData
  14. * @author zfy
  15. * @day 2020-03-27
  16. */
  17. class GroupDataDao extends BaseDao
  18. {
  19. /**
  20. * @return BaseModel
  21. * @author zfy
  22. * @day 2020-03-30
  23. */
  24. protected function getModel(): string
  25. {
  26. return SystemGroupData::class;
  27. }
  28. /**
  29. * @param $merId
  30. * @param $groupId
  31. * @return BaseQuery
  32. * @author zfy
  33. * @day 2020-03-30
  34. */
  35. public function getGroupDataWhere($merId, $groupId): BaseQuery
  36. {
  37. return SystemGroupData::getDB()->withAttr('value', function ($val) {
  38. return json_decode($val, true);
  39. })->where('mer_id', $merId)->where('group_id', $groupId)->order('sort DESC');
  40. }
  41. /**
  42. * @param $merId
  43. * @param $groupId
  44. * @param int|null $page
  45. * @param int|null $limit
  46. * @return array
  47. * @author zfy
  48. * @day 2020/5/27
  49. */
  50. public function getGroupData($merId, $groupId, ?int $page = null, ?int $limit = 10)
  51. {
  52. $query = SystemGroupData::getDB()
  53. ->where('mer_id', $merId)->where('group_id', $groupId)
  54. ->where('status', 1)
  55. ->where('')
  56. ->order('sort DESC');
  57. if (!is_null($page)) $query->page($page, $limit);
  58. $groupData = [];
  59. foreach ($query->column('value') as $k => $v) {
  60. $groupData[] = json_decode($v, true);
  61. }
  62. return $groupData;
  63. }
  64. public function groupDataCount($merId, $groupId)
  65. {
  66. return SystemGroupData::getDB()->where('mer_id', $merId)->where('group_id', $groupId)->where('status', 1)->count();
  67. }
  68. /**
  69. * @param $merId
  70. * @param $groupId
  71. * @param int|null $page
  72. * @param int|null $limit
  73. * @return array
  74. * @author zfy
  75. * @day 2020/6/3
  76. */
  77. public function getGroupDataId($merId, $groupId, ?int $page = null, ?int $limit = 10)
  78. {
  79. $query = SystemGroupData::getDB()->where('mer_id', $merId)->where('group_id', $groupId)->where('status', 1)->order('sort DESC');
  80. if (!is_null($page)) $query->page($page, $limit);
  81. $groupData = [];
  82. foreach ($query->column('value', 'group_data_id') as $k => $v) {
  83. $groupData[] = ['id' => $k, 'data' => json_decode($v, true)];
  84. }
  85. return $groupData;
  86. }
  87. /**
  88. * @param $merId
  89. * @param $id
  90. * @param $data
  91. * @return int
  92. * @throws DbException
  93. * @author zfy
  94. * @day 2020-03-30
  95. */
  96. public function merUpdate($merId, $id, $data)
  97. {
  98. $data['value'] = json_encode($data['value']);
  99. return SystemGroupData::getDB()->where('group_data_id', $id)->where('mer_id', $merId)->update($data);
  100. }
  101. /**
  102. * @param $merId
  103. * @param $id
  104. * @return int
  105. * @throws DbException
  106. * @author zfy
  107. * @day 2020-03-30
  108. */
  109. public function merDelete($merId, $id)
  110. {
  111. return SystemGroupData::getDB()->where('mer_id', $merId)->where('group_data_id', $id)->delete();
  112. }
  113. /**
  114. * @param int $merId
  115. * @param int $id
  116. * @return bool
  117. * @author zfy
  118. * @day 2020-04-02
  119. */
  120. public function merExists(int $merId, int $id)
  121. {
  122. return ($this->getModel())::getDB()->where('mer_id', $merId)->where($this->getPk(), $id)->count() > 0;
  123. }
  124. /**
  125. * @param int $groupId
  126. * @return int
  127. * @throws DbException
  128. * @author zfy
  129. * @day 2020-05-16
  130. */
  131. public function clearGroup(int $groupId)
  132. {
  133. return SystemGroupData::getDB()->where('group_id', $groupId)->delete();
  134. }
  135. /**
  136. * @param $id
  137. * @param $merId
  138. * @return array|Model|null
  139. * @throws DbException
  140. * @throws DataNotFoundException
  141. * @throws ModelNotFoundException
  142. * @author zfy
  143. * @day 2020/6/2
  144. */
  145. public function merGet($id, $merId)
  146. {
  147. $data = SystemGroupData::getDB()->where('group_data_id', $id)->where('mer_id', $merId)->find();
  148. return $data ? $data['value'] : null;
  149. }
  150. }