StoreAttrTemplateDao.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\BaseModel;
  14. use app\common\model\store\StoreAttrTemplate;
  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 StoreAttrTemplateDao
  22. * @package app\common\dao\store
  23. * @author xaboy
  24. * @day 2020-05-06
  25. */
  26. class StoreAttrTemplateDao extends BaseDao
  27. {
  28. /**
  29. * @return BaseModel
  30. * @author xaboy
  31. * @day 2020-03-30
  32. */
  33. protected function getModel(): string
  34. {
  35. return StoreAttrTemplate::class;
  36. }
  37. /**
  38. * 根据条件搜索商店属性模板
  39. *
  40. * 本函数用于查询特定商家ID下的属性模板信息。支持通过关键词进行模糊搜索。
  41. *
  42. * @param string $merId 商家ID,用于限定查询的商家范围。
  43. * @param array $where 查询条件数组,可选参数,其中'keyword'键用于指定搜索关键词。
  44. * @return BaseQuery|\think\Paginator
  45. */
  46. public function search($merId, array $where = [])
  47. {
  48. return StoreAttrTemplate::getDB()->when(isset($where['keyword']),function($query) use($where){
  49. $query->whereLike('template_name',"%{$where['keyword']}%");
  50. })->where('mer_id', $merId)->order('attr_template_id DESC');
  51. }
  52. /**
  53. * 检查指定商户是否存在指定的ID
  54. *
  55. * 本函数通过调用merFieldExists方法来判断指定商户ID在特定字段中是否存在。
  56. * 主要用于验证商户ID的有效性,以及在某些情况下排除特定ID的检查。
  57. *
  58. * @param int $merId 商户的ID,用于标识特定的商户。
  59. * @param int $id 需要检查的ID,看它是否属于指定的商户。
  60. * @param mixed $except 可选参数,用于指定需要排除的ID,默认为null。
  61. * @return bool 如果指定的ID存在于商户中则返回true,否则返回false。
  62. */
  63. public function merExists(int $merId, int $id, $except = null)
  64. {
  65. // 调用merFieldExists方法来检查指定的ID是否存在于商户ID字段中
  66. return $this->merFieldExists($merId, $this->getPk(), $id, $except);
  67. }
  68. /**
  69. * 检查指定商家是否存在特定字段的特定值。
  70. *
  71. * 该方法用于查询数据库中是否存在特定条件的记录。具体来说,它首先检查传入的除外条件($except),
  72. * 如果除外条件存在,则在查询时排除这些条件。然后,它查询指定商家($merId)的记录中,
  73. * 指定字段($field)的值是否等于传入的值($value)。如果存在匹配的记录,则返回true,否则返回false。
  74. *
  75. * @param int $merId 商家ID,用于限定查询的商家范围。
  76. * @param string $field 要检查的字段名。
  77. * @param mixed $value 要检查的字段值。
  78. * @param mixed $except 除外条件,即在查询时不应该匹配的值。
  79. * @return bool 如果存在匹配的记录则返回true,否则返回false。
  80. */
  81. public function merFieldExists(int $merId, $field, $value, $except = null)
  82. {
  83. // 获取模型对应的数据库实例,并根据$except参数应用条件。
  84. return ($this->getModel())::getDB()->when($except, function ($query, $except) use ($field) {
  85. // 如果$except存在,则添加不等于($<>_)查询条件。
  86. $query->where($field, '<>', $except);
  87. })->where('mer_id', $merId)->where($field, $value)->count() > 0;
  88. }
  89. /**
  90. * 根据ID和商家ID获取数据
  91. *
  92. * 本函数用于从数据库中检索指定ID和商家ID对应的数据行。
  93. * 它首先通过调用getModel方法来获取模型实例,然后使用该实例的getDB方法来获得数据库操作对象。
  94. * 接着,通过where方法指定查询条件为商家ID为$merId,最后使用find方法根据$id查找数据。
  95. *
  96. * @param int $id 数据行的唯一标识ID
  97. * @param int $merId 商家的唯一标识ID,默认为0,表示系统默认商家
  98. * @return object 返回查询结果的对象,如果未找到则为null
  99. */
  100. public function get( $id, $merId = 0)
  101. {
  102. return ($this->getModel())::getDB()->where('mer_id', $merId)->find($id);
  103. }
  104. /**
  105. * 根据ID和商家ID删除记录
  106. *
  107. * 本函数用于删除指定ID的记录。它可以处理单个ID和多个ID的删除请求。
  108. * 删除操作会根据提供的商家ID对数据进行过滤,确保只删除属于该商家的数据。
  109. *
  110. * @param mixed $id 需要删除的记录的ID,可以是单个ID或ID数组
  111. * @param int $merId 商家ID,用于指定删除哪个商家的数据,默认为0,表示删除所有商家的数据
  112. * @return int 返回删除的记录数
  113. */
  114. public function delete($id, $merId = 0)
  115. {
  116. // 获取数据库实例并根据商家ID建立查询条件
  117. $query = ($this->getModel())::getDB()->where('mer_id', $merId);
  118. // 判断传入的ID类型,如果是数组,则使用whereIn方法删除多个记录;否则,直接使用where方法删除单个记录
  119. if (is_array($id)) {
  120. $query->where($this->getPk(), 'in',$id);
  121. } else {
  122. $query->where($this->getPk(), $id);
  123. }
  124. // 执行删除操作并返回删除的记录数
  125. return $query->delete();
  126. }
  127. /**
  128. * 获取指定商户的属性模板列表
  129. *
  130. * 本函数旨在通过指定的商户ID,从数据库中检索该商户的所有属性模板信息。
  131. * 返回的结果包括属性模板ID、模板名称和模板值。
  132. *
  133. * @param int $merId 商户ID,用于指定要查询的商户。
  134. * @return array 返回一个包含属性模板信息的数组,每个元素包含属性模板ID、名称和值。
  135. */
  136. public function getList($merId)
  137. {
  138. // 使用模型获取数据库实例,并设置查询条件为指定的商户ID,然后指定查询的字段,最后执行查询操作。
  139. return ($this->getModel())::getDB()->where('mer_id',$merId)->field('mer_id,attr_template_id,template_name,template_value')->select()
  140. ->each(function($item) {
  141. $template_value = $item['template_value'];
  142. if ($template_value) {
  143. foreach ($template_value as &$item1) {
  144. $detail = $item1['detail'];
  145. $de = array_map(function($v) use($item1){return ['pic' => '', 'value' => $v];},$detail);
  146. $item1['detail'] = $de;
  147. }
  148. $item['template_value'] = $template_value;
  149. }
  150. });
  151. }
  152. }