ShippingTemplateDao.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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\shipping;
  12. use app\common\repositories\store\shipping\ShippingTemplateFreeRepository;
  13. use app\common\repositories\store\shipping\ShippingTemplateRegionRepository;
  14. use app\common\repositories\store\shipping\ShippingTemplateUndeliveRepository;
  15. use app\common\repositories\system\merchant\MerchantAdminRepository;
  16. use crmeb\jobs\ClearMerchantStoreJob;
  17. use think\facade\Db;
  18. use app\common\dao\BaseDao;
  19. use app\common\model\store\shipping\ShippingTemplate as model;
  20. use think\facade\Queue;
  21. class ShippingTemplateDao extends BaseDao
  22. {
  23. /**
  24. * @Author:Qinii
  25. * @Date: 2020/5/8
  26. * @return string
  27. */
  28. protected function getModel(): string
  29. {
  30. return model::class;
  31. }
  32. /**
  33. * 根据条件搜索信息。
  34. *
  35. * 本函数用于根据给定的商家ID和额外的搜索条件,从数据库中检索相关信息。
  36. * 它支持根据商家名称和类型进行模糊搜索或精确搜索。
  37. *
  38. * @param int $merId 商家ID,用于限定搜索范围。
  39. * @param array $where 搜索条件数组,包含可选的名称和类型条件。
  40. * @return \think\db\Query 返回一个查询对象,该对象可用于进一步的查询操作或数据检索。
  41. */
  42. public function search(int $merId,array $where)
  43. {
  44. // 初始化查询,指定商家ID并按排序降序排列
  45. $query = ($this->getModel()::getDB())->where('mer_id',$merId)->order('sort desc');
  46. // 如果指定了名称,并且名称不为空,则添加名称模糊搜索条件
  47. if(isset($where['name']) && !empty($where['name'])) {
  48. $query->where('name','like','%'.$where['name'].'%');
  49. }
  50. // 如果指定了类型,并且类型不为空,则添加类型精确搜索条件
  51. if(isset($where['type']) && !empty($where['type'])) {
  52. $query->where('type',$where['type']);
  53. }
  54. // 最终按排序降序排列,如果未指定排序则默认按照创建时间降序排列
  55. return $query->order('sort DESC,create_time DESC');
  56. }
  57. /**
  58. * 查询是否存在
  59. * @Author:Qinii
  60. * @Date: 2020/5/7
  61. * @param int $merId
  62. * @param $field
  63. * @param $value
  64. * @param null $except
  65. * @return bool
  66. */
  67. public function merFieldExists(int $merId, $field, $value, $except = null)
  68. {
  69. return ($this->getModel())::getDB()->when($except, function ($query, $except) use ($field) {
  70. $query->where($field, '<>', $except);
  71. })->where('mer_id', $merId)->where($field, $value)->count() > 0;
  72. }
  73. /**
  74. * 关联删除
  75. * @Author:Qinii
  76. * @Date: 2020/5/7
  77. * @param int $id
  78. * @return int|void
  79. */
  80. public function delete(int $id)
  81. {
  82. $result = $this->getModel()::with(['free','region','undelives'])->find($id);
  83. $result->together(['free','region','undelives'])->delete();
  84. }
  85. /**
  86. * 批量删除
  87. * @Author:Qinii
  88. * @Date: 2020/5/8
  89. * @param int $id
  90. * @return mixed
  91. */
  92. public function batchRemove(int $id)
  93. {
  94. return ($this->getModel())::getDB()->where($this->getPk(),'in',$id)->delete();
  95. }
  96. /**
  97. * 获取商家的物流模板列表
  98. *
  99. * 本函数用于查询指定商家ID对应的物流模板信息。它通过构建数据库查询条件,
  100. * 筛选出商家ID对应的物流模板,并按照排序和创建时间进行降序排列,最终返回查询结果。
  101. *
  102. * @param int $merId 商家ID,用于确定查询的商家范围
  103. * @return array 返回包含物流模板信息的数组,每个元素包含shipping_template_id、name、is_default字段
  104. */
  105. public function getList($merId)
  106. {
  107. // 调用getModel方法获取模型实例,并直接进行数据库查询
  108. // 查询条件为mer_id等于$merId,返回字段包括shipping_template_id、name、is_default
  109. // 按照sort降序和create_time降序进行结果排序
  110. return ($this->getModel())::getDB()->where('mer_id',$merId)->field('shipping_template_id,name,is_default')->order('sort DESC,create_time DESC')->select();
  111. }
  112. /**
  113. * 清理与指定字段和ID相关的运输模板数据。
  114. *
  115. * 此函数用于根据给定的字段ID清理相关的运输模板数据,包括删除模板本身以及与该模板相关的所有区域、未投递和免费运输设置。
  116. * 使用事务确保数据一致性和完整性。
  117. *
  118. * @param int $id 主键ID,用于查询和删除相关数据。
  119. * @param string $field 字段名,用于查询和删除相关数据。
  120. */
  121. public function clear($id,$field)
  122. {
  123. // 查询与指定字段和ID相关的运输模板ID。
  124. $shipping_template_id = $this->getModel()::getDB()->where($field, $id)->column('shipping_template_id');
  125. // 使用事务处理来确保相关操作的原子性。
  126. Db::transaction(function () use ($id,$field,$shipping_template_id) {
  127. // 删除指定字段和ID的运输模板记录。
  128. $this->getModel()::getDB()->where($field, $id)->delete();
  129. // 删除与运输模板ID相关的所有免费运输设置。
  130. app()->make(ShippingTemplateFreeRepository::class)->getSearch([])->whereIn('temp_id',$shipping_template_id)->delete();
  131. // 删除与运输模板ID相关的所有区域设置。
  132. app()->make(ShippingTemplateRegionRepository::class)->getSearch([])->whereIn('temp_id',$shipping_template_id)->delete();
  133. // 删除与运输模板ID相关的所有未投递设置。
  134. app()->make(ShippingTemplateUndeliveRepository::class)->getSearch([])->whereIn('temp_id',$shipping_template_id)->delete();
  135. });
  136. }
  137. /**
  138. * 设置默认模板
  139. * @param $merId
  140. * @param $id
  141. * @return bool
  142. *
  143. * @date 2023/10/07
  144. * @author yyw
  145. */
  146. public function setDefault($merId, $id)
  147. {
  148. ($this->getModel())::getDB()->where('mer_id', $merId)->where('is_default', 1)->update(['is_default' => 0]);
  149. ($this->getModel())::getDB()->where($this->getPk(), $id)->update(['is_default' => 1]);
  150. return true;
  151. }
  152. }