GuaranteeRepository.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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\repositories\store;
  12. use app\common\dao\store\GuaranteeDao;
  13. use app\common\repositories\BaseRepository;
  14. use app\common\repositories\store\product\ProductRepository;
  15. use FormBuilder\Factory\Elm;
  16. use think\facade\Route;
  17. /**
  18. * 保障服务
  19. */
  20. class GuaranteeRepository extends BaseRepository
  21. {
  22. /**
  23. * @var GuaranteeDao
  24. */
  25. protected $dao;
  26. /**
  27. * GuaranteeRepository constructor.
  28. * @param GuaranteeDao $dao
  29. */
  30. public function __construct(GuaranteeDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. /**
  35. * 平台列表
  36. * @param $where
  37. * @param $page
  38. * @param $limit
  39. * @return array
  40. * @author Qinii
  41. * @day 5/17/21
  42. */
  43. public function getList($where, $page, $limit)
  44. {
  45. $query = $this->dao->getSearch($where)->order('sort DESC');
  46. $count = $query->count();
  47. $list = $query->page($page, $limit)->select();
  48. return compact('count', 'list');
  49. }
  50. /**
  51. * 根据条件查询保证信息列表
  52. *
  53. * 本函数通过调用DAO层的方法,根据传入的条件数组查询保证信息的相关数据。
  54. * 查询的字段包括保证ID、保证名称、保证信息和保证图片,结果按排序降序返回。
  55. *
  56. * @param array $where 查询条件数组
  57. * @return array 查询到的保证信息列表
  58. */
  59. public function select(array $where)
  60. {
  61. // 调用DAO方法进行查询,指定查询字段,排序方式,并返回查询结果
  62. $list = $this->dao->getSearch($where)
  63. ->field('guarantee_id,guarantee_name,guarantee_info,image')
  64. ->order('sort DESC')
  65. ->select();
  66. return $list;
  67. }
  68. /**
  69. * 添加form
  70. * @param int|null $id
  71. * @param array $formData
  72. * @return \FormBuilder\Form
  73. * @author Qinii
  74. * @day 5/17/21
  75. */
  76. public function form(?int $id, array $formData = [])
  77. {
  78. $form = Elm::createForm(is_null($id) ? Route::buildUrl('systemGuaranteeCreate')->build() : Route::buildUrl('systemGuaranteeUpdate', ['id' => $id])->build());
  79. $form->setRule([
  80. Elm::input('guarantee_name', '服务条款:')->placeholder('请输入服务条款')->required(),
  81. Elm::textarea('guarantee_info', '内容描述:')->autosize([
  82. 'minRows' => 1000,
  83. ])->placeholder('请输入内容描述')->required(),
  84. Elm::frameImage('image', '条款图标:', '/' . config('admin.admin_prefix') . '/setting/uploadPicture?field=image&type=1')->value($formData['image'] ?? '')->icon('el-icon-camera')->modal(['modal' => false])->width('1000px')->height('600px')->required()->appendRule('suffix', [
  85. 'type' => 'div',
  86. 'style' => ['color' => '#999999'],
  87. 'domProps' => [
  88. 'innerHTML' => '建议尺寸:100*100px',
  89. ],
  90. ]),
  91. Elm::switches('status', '是否显示:', 1)->activeValue(1)->inactiveValue(0)->inactiveText('关')->activeText('开'),
  92. Elm::number('sort', '排序:', 0)->precision(0)->max(99999),
  93. ]);
  94. return $form->setTitle(is_null($id) ? '添加服务条款' : '编辑服务条款')->formData($formData);
  95. }
  96. /**
  97. * 编辑form
  98. * @param $id
  99. * @return \FormBuilder\Form
  100. * @author Qinii
  101. * @day 5/17/21
  102. */
  103. public function updateForm($id)
  104. {
  105. $ret = $this->dao->get($id);
  106. return $this->form($id, $ret->toArray());
  107. }
  108. /**
  109. * 获取详情
  110. * @param $id
  111. * @return array|\think\Model|null
  112. * @author Qinii
  113. * @day 5/17/21
  114. */
  115. public function get($id)
  116. {
  117. $where = [
  118. $this->dao->getPk() => $id,
  119. 'is_del' => 0,
  120. ];
  121. $ret = $this->dao->getWhere($where);
  122. return $ret;
  123. }
  124. /**
  125. * 统计并更新保障服务的商家和商品数量。
  126. * 此方法用于查询所有有效的保障服务,然后分别统计每个保障服务所覆盖的商家数量和商品数量。
  127. * 最后,它将更新这些统计信息到对应的保障服务记录中。
  128. *
  129. * @return void 无返回值
  130. */
  131. public function countGuarantee()
  132. {
  133. /**
  134. * 查询所有状态为启用且未被删除的保障服务ID。
  135. * 这里的注释解释了查询的目的和条件,即只考虑启用中且未被删除的保障服务。
  136. */
  137. /**
  138. * 获取所有条款
  139. * 计算商户数量
  140. * 计算商品数量
  141. */
  142. $ret = $this->dao->getSearch(['status' => 1, 'is_del' => 0])->column($this->dao->getPk());
  143. // 实例化保障服务值 repository 和商品 repository,用于后续的查询和统计。
  144. $make = app()->make(GuaranteeValueRepository::class);
  145. $makeProduct = app()->make(ProductRepository::class);
  146. // 如果查询到保障服务ID,则遍历每个保障服务进行统计更新。
  147. if ($ret) {
  148. foreach ($ret as $k => $v) {
  149. $item = [];
  150. // 统计每个保障服务覆盖的商家数量。
  151. $item['mer_count'] = $make->getSearch(['guarantee_id' => $v])->group('mer_id')->count('*');
  152. // 统计每个保障服务使用的模板ID,用于后续的商品数量统计。
  153. $template = $make->getSearch(['guarantee_id' => $v])->group('guarantee_template_id')->column('guarantee_template_id');
  154. // 统计每个保障服务覆盖的商品数量。
  155. $item['product_cout'] = $makeProduct->getSearch(['guarantee_template_id' => $template])->count('*');
  156. // 更新统计信息的时间戳。
  157. $item['update_time'] = date('Y-m-d H:i:s', time());
  158. // 更新保障服务记录的统计信息。
  159. $this->dao->update($v, $item);
  160. }
  161. }
  162. return;
  163. }
  164. }