StoreServiceLogDao.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 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\store\service;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\store\service\StoreServiceLog;
  14. use think\db\BaseQuery;
  15. /**
  16. * Class StoreServiceLogDao
  17. * @package app\common\dao\store\service
  18. * @author xaboy
  19. * @day 2020/5/29
  20. */
  21. class StoreServiceLogDao extends BaseDao
  22. {
  23. /**
  24. * @return string
  25. * @author xaboy
  26. * @day 2020/5/29
  27. */
  28. protected function getModel(): string
  29. {
  30. return StoreServiceLog::class;
  31. }
  32. /**
  33. * 更新用户服务记录的状态
  34. *
  35. * 本函数用于将特定用户的特定服务记录的状态从未读改为已读。
  36. * 它通过指定的商家ID和用户ID来定位特定的服务记录,并更新其类型字段。
  37. *
  38. * @param string $merId 商家ID,用于定位服务记录所属的商家。
  39. * @param string $uid 用户ID,用于定位服务记录所属的用户。
  40. * @return int 返回更新操作影响的行数,即被更新的服务记录数量。
  41. * @throws \think\db\exception\DbException
  42. */
  43. public function userRead($merId, $uid)
  44. {
  45. // 使用where子句构建查询条件,定位到特定商家、特定用户且类型不为1的服务记录,
  46. // 然后将这些记录的类型更新为1,表示这些记录已被读取。
  47. return StoreServiceLog::getDB()->where('mer_id', $merId)->where('uid', $uid)->where('type', '<>', 1)->update(['type' => 1]);
  48. }
  49. /**
  50. * 检查用户是否有未发送的日志
  51. *
  52. * 本函数用于查询指定用户($uid)在指定商户($merId)下是否存在未发送类型的日志记录。
  53. * 通过返回布尔值来表示查询结果,如果存在至少一条未发送的日志记录,则返回true,否则返回false。
  54. * 这对于判断是否需要发送日志或者是否已完成所有日志发送等情况非常有用。
  55. *
  56. * @param int $uid 用户ID,用于指定查询哪个用户的日志记录。
  57. * @param int $merId 商户ID,用于指定查询哪个商户的日志记录。
  58. * @return bool 如果查询结果存在至少一条记录则返回true,否则返回false。
  59. * @throws \think\db\exception\DbException
  60. */
  61. public function issetLog($uid, $merId)
  62. {
  63. // 使用数据库查询方法,根据$merId, $uid和send_type为0的条件,限制查询结果为一条,然后检查查询结果的数量是否大于0。
  64. return StoreServiceLog::getDB()->where('mer_id', $merId)->where('uid', $uid)->where('send_type', 0)->limit(1)->count() > 0;
  65. }
  66. /**
  67. * 标记服务为已读
  68. *
  69. * 本函数用于更新特定商户、用户、服务ID的服务日志状态,将其标记为已读。
  70. * 通过查询条件筛选出特定的服务日志记录,确保服务类型不为1的情况下,更新服务类型为1。
  71. * 这样的设计可能是为了区分服务的阅读状态,避免某些操作被错误地标记为已读。
  72. *
  73. * @param string $merId 商户ID,用于限定查询的商户范围
  74. * @param string $uid 用户ID,用于限定查询的用户范围
  75. * @param string $serviceId 服务ID,用于限定查询的具体服务
  76. * @return int 返回更新操作影响的行数,用于确认操作是否成功
  77. * @throws \think\db\exception\DbException
  78. */
  79. public function serviceRead($merId, $uid, $serviceId)
  80. {
  81. // 使用where子句构建查询条件,确保查询到正确的服务日志记录
  82. // 并通过update方法更新这些记录的服务类型为1,表示已读
  83. return StoreServiceLog::getDB()->where('mer_id', $merId)->where('uid', $uid)->where('service_id', $serviceId)->where('service_type', '<>', 1)->update(['service_type' => 1]);
  84. }
  85. /**
  86. * 根据条件搜索商店服务日志。
  87. *
  88. * 该方法用于查询商店服务日志数据库,根据传入的条件进行过滤。支持的条件包括用户ID(uid)、商家ID(mer_id)、
  89. * 最后一条日志ID(last_id)和服务ID(service_id)。每个条件都是可选的,只有当相应条件的值被设置且不为空时,
  90. * 才会应用该条件的查询过滤。
  91. *
  92. * @param array $where 查询条件数组,包含可能的过滤条件:uid、mer_id、last_id和服务_id。
  93. * @return BaseQuery|\think\db\Query
  94. */
  95. public function search(array $where)
  96. {
  97. // 获取数据库查询对象
  98. return StoreServiceLog::getDB()->when(isset($where['uid']) && $where['uid'] !== '', function ($query) use ($where) {
  99. // 如果设置了用户ID,则添加用户ID的查询条件
  100. $query->where('uid', $where['uid']);
  101. })->when(isset($where['mer_id']) && $where['mer_id'] !== '', function ($query) use ($where) {
  102. // 如果设置了商家ID,则添加商家ID的查询条件
  103. $query->where('mer_id', $where['mer_id']);
  104. })->when(isset($where['last_id']) && $where['last_id'] !== '', function ($query) use ($where) {
  105. // 如果设置了最后一条日志ID,则添加服务日志ID小于指定ID的查询条件
  106. $query->where('service_log_id', '<', $where['last_id']);
  107. })->when(isset($where['service_id']) && $where['service_id'] !== '', function ($query) use ($where) {
  108. // 如果设置了服务ID,则添加服务ID的查询条件
  109. $query->where('service_id', $where['service_id']);
  110. });
  111. }
  112. /**
  113. * 获取用户最近一次的服务ID
  114. *
  115. * 本函数用于查询指定用户和商家的最近一次服务记录的ID。
  116. * 通过查询StoreServiceLog表,根据商家ID(merId)和用户ID(uid),
  117. * 以服务日志ID降序的方式获取最新的服务ID。
  118. *
  119. * @param string $merId 商家ID,用于指定查询哪个商家的服务记录
  120. * @param string $uid 用户ID,用于指定查询哪个用户的服务记录
  121. * @return mixed 返回最近一次服务的ID,如果不存在则返回null
  122. */
  123. public function getLastServiceId($merId, $uid)
  124. {
  125. // 使用数据库查询工具,指定查询条件为商家ID和用户ID,按服务日志ID降序排列,返回最新的服务ID
  126. return StoreServiceLog::getDB()->where('mer_id', $merId)->order('service_log_id DESC')->where('uid', $uid)->value('service_id');
  127. }
  128. /**
  129. * 获取商家列表查询
  130. *
  131. * 本函数用于构造查询指定用户所关联的商家列表的数据库查询语句。
  132. * 通过用户ID($uid)筛选出相关数据,并对结果按商家ID(mer_id)进行分组。
  133. * 这样做的目的是为了获取每个商家的相关信息,避免数据重复,便于后续处理和展示商家列表。
  134. *
  135. * @param int $uid 用户ID,用于查询该用户关联的商家信息。
  136. * @return \think\db\Query 返回一个数据库查询对象,该对象包含了根据用户ID筛选并按商家ID分组的查询条件。
  137. */
  138. public function getMerchantListQuery($uid)
  139. {
  140. // 使用StoreServiceLog类中的getDB方法获取数据库操作对象,并链式调用where和group方法构造查询语句
  141. return StoreServiceLog::getDB()->where('uid', $uid)->group('mer_id');
  142. }
  143. /**
  144. * 根据服务ID获取用户列表查询
  145. *
  146. * 本函数用于构建查询特定服务ID的用户列表的数据库查询条件。它不直接执行查询,
  147. * 而是返回一个构建好的查询对象,以便进一步定制查询条件或执行查询。
  148. *
  149. * @param int $serviceId 服务ID,用于指定查询哪个服务的用户列表。
  150. * @return \yii\db\ActiveQuery 返回一个ActiveQuery对象,该对象包含了根据服务ID筛选的查询条件。
  151. */
  152. public function getUserListQuery($serviceId)
  153. {
  154. // 通过StoreServiceLog类的getDB方法获取数据库连接对象,并链式调用where方法指定查询条件
  155. return StoreServiceLog::getDB()->where('service_id', $serviceId);
  156. }
  157. /**
  158. * 根据商家ID获取商家用户列表
  159. *
  160. * 本函数通过查询StoreServiceLog中的数据,来获取指定商家ID下的用户列表。
  161. * 主要用于商家服务记录的查询与管理,以便商家能够查看与其相关的用户服务记录。
  162. *
  163. * @param string $merId 商家ID,用于查询指定商家的服务记录。
  164. * @return \Illuminate\Database\Query\Builder|Collection 返回查询结果,可以是查询构建器对象或集合。
  165. */
  166. public function getMerchantUserList($merId)
  167. {
  168. // 通过StoreServiceLog的getDB方法获取数据库查询构建器,并指定查询条件为mer_id等于$merId
  169. return StoreServiceLog::getDB()->where('mer_id', $merId);
  170. }
  171. /**
  172. * 获取指定商户和用户最后一次服务日志
  173. *
  174. * 本函数通过查询StoreServiceLog模型,找到指定商户(merId)和用户(uid)的最后一次服务日志。
  175. * 它使用了数据库查询语句来过滤结果,并按照service_log_id降序排列,确保返回的是最新的日志记录。
  176. *
  177. * @param string $merId 商户ID,用于指定查询哪个商户的服务日志
  178. * @param string $uid 用户ID,用于指定查询哪个用户的服务日志
  179. * @return array 返回最后一次服务日志的信息,如果不存在则返回空数组
  180. * @throws \think\db\exception\DataNotFoundException
  181. * @throws \think\db\exception\DbException
  182. * @throws \think\db\exception\ModelNotFoundException
  183. */
  184. public function getLastLog($merId, $uid)
  185. {
  186. // 使用where方法指定查询条件,查询指定商户和用户的服务日志
  187. // 并通过order方法按service_log_id降序排列,确保获取最新的日志记录
  188. // 最后使用find方法查找并返回第一条符合条件的数据
  189. return StoreServiceLog::getDB()->where('mer_id', $merId)->where('uid', $uid)->order('service_log_id DESC')->find();
  190. }
  191. /**
  192. * 获取未读消息数量
  193. * 该方法用于查询特定用户、商家和消息类型的未读消息数量。
  194. * @param $merId 商家ID,用于指定查询哪个商家的消息
  195. * @param $uid 用户ID,用于指定查询哪个用户的消息
  196. * @param $sendType 消息发送类型,用于进一步筛选消息
  197. * @return int 返回未读消息的数量
  198. */
  199. public function getUnReadNum($merId, $uid, $sendType)
  200. {
  201. // 根据传入的参数,使用条件查询未读消息数量
  202. // 其中,查询条件包括用户ID、商家ID、消息发送类型以及消息类型(当sendType为真时查询type,否则查询service_type)
  203. return StoreServiceLog::getDB()->where('uid', $uid)->where('mer_id', $merId)->where('send_type', $sendType)->where($sendType ? 'type' : 'service_type', 0)->count();
  204. }
  205. /**
  206. * 计算用户未读消息的数量
  207. *
  208. * 本函数用于查询并返回指定用户ID的未读消息总数。未读消息是指发送类型为1,消息类型为0的那些消息。
  209. * 这种统计方式适用于需要区分用户并关注其未读消息数量的场景,例如在商城应用中,用户可能需要知道有多少未读的系统通知。
  210. *
  211. * @param int $uid 用户的唯一标识符。这个参数用于指定查询哪个用户的未读消息数量。
  212. * @return int 返回未读消息的数量。这个数量是根据查询条件计算得出的,只包括发送类型为1,消息类型为0的消息。
  213. */
  214. public function totalUnReadNum($uid)
  215. {
  216. // 使用数据库查询方法,根据条件统计未读消息的数量
  217. return StoreServiceLog::getDB()->where('uid', $uid)->where('send_type', 1)->where('type', 0)->count();
  218. }
  219. }