StoreProductEnsureServices.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2021 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\services\product\ensure;
  12. use app\dao\product\ensure\StoreProductEnsureDao;
  13. use app\services\BaseServices;
  14. use app\services\product\product\StoreProductServices;
  15. use crmeb\services\FormBuilder as Form;
  16. use FormBuilder\Factory\Iview;
  17. use think\facade\Route as Url;
  18. /**
  19. * 商品保障服务
  20. * Class StoreProductEnsureServices
  21. * @package app\services\product\ensure
  22. * @mixin StoreProductEnsureDao
  23. */
  24. class StoreProductEnsureServices extends BaseServices
  25. {
  26. /**
  27. * StoreProductEnsureServices constructor.
  28. * @param StoreProductEnsureDao $dao
  29. */
  30. public function __construct(StoreProductEnsureDao $dao)
  31. {
  32. $this->dao = $dao;
  33. }
  34. /**
  35. * 获取所有保障服务
  36. * @param int $type
  37. * @param int $relation_id
  38. * @return array
  39. * @throws \think\db\exception\DataNotFoundException
  40. * @throws \think\db\exception\DbException
  41. * @throws \think\db\exception\ModelNotFoundException
  42. */
  43. public function getAllEnsure(int $type = 0, int $relation_id = 0)
  44. {
  45. // $where['relation_id'] = $relation_id;
  46. // $where['type'] = $type;
  47. $where['status'] = 1;
  48. return $this->dao->getList($where, 'id,name');
  49. }
  50. /**
  51. * 获取缓存内的数据
  52. * @param array $ids
  53. * @param array|null $field
  54. * @return array|mixed
  55. * @throws \think\db\exception\DataNotFoundException
  56. * @throws \think\db\exception\DbException
  57. * @throws \think\db\exception\ModelNotFoundException
  58. * @author 等风来
  59. * @email 136327134@qq.com
  60. * @date 2022/11/14
  61. */
  62. public function getEnsurCache(array $ids, array $field = null)
  63. {
  64. if (app()->config->get('cache.is_data')) {
  65. $list = $this->dao->cacheInfoByIds($ids);
  66. } else {
  67. $list = null;
  68. }
  69. if (!$list) {
  70. $list = $this->dao->getList(['ids' => $ids, 'status' => 1]);
  71. foreach ($list as $item) {
  72. $this->dao->cacheUpdate($item);
  73. }
  74. }
  75. if ($field && $list) {
  76. $newList = [];
  77. foreach ($list as $item) {
  78. $data = [];
  79. foreach ($field as $k) {
  80. $data[$k] = $item[$k] ?? null;
  81. }
  82. $newList[] = $data;
  83. }
  84. $list = $newList;
  85. }
  86. return $list;
  87. }
  88. /**
  89. * 获取保障服务列表(带标签)
  90. * @param array $where
  91. * @return array
  92. */
  93. public function getEnsureList(array $where)
  94. {
  95. [$page, $limit] = $this->getPageValue();
  96. $list = $this->dao->getList($where, '*', $page, $limit);
  97. if ($list) {
  98. /** @var StoreProductServices $storeProductServices */
  99. $storeProductServices = app()->make(StoreProductServices::class);
  100. foreach ($list as &$item) {
  101. $item['product_count'] = $storeProductServices->getUseEnsureCount((int)$item['id']);
  102. }
  103. }
  104. $count = $this->dao->count($where);
  105. return compact('list', 'count');
  106. }
  107. /**
  108. * 创建新增表单
  109. * @return array
  110. * @throws \FormBuilder\Exception\FormBuilderException
  111. */
  112. public function createForm()
  113. {
  114. return create_form('添加保障服务', $this->form(), Url::buildUrl('/product/ensure'), 'POST');
  115. }
  116. /**
  117. * 创建编辑表单
  118. * @param $id
  119. * @return array
  120. * @throws \FormBuilder\Exception\FormBuilderException
  121. */
  122. public function editForm(int $id)
  123. {
  124. $info = $this->dao->get($id);
  125. return create_form('编辑保障服务', $this->form($info), $this->url('/product/ensure/' . $id), 'PUT');
  126. }
  127. /**
  128. * 生成表单参数
  129. * @param array $info
  130. * @return array
  131. * @throws \FormBuilder\Exception\FormBuilderException
  132. */
  133. public function form($info = [])
  134. {
  135. $f[] = Form::input('name', '保障服务条款', $info['name'] ?? '')->maxlength(100)->required();
  136. $f[] = Form::textarea('desc', '内容描述', $info['desc'] ?? '')->required();
  137. $f[] = Form::frameImage('image', '图标(建议尺寸:100px*100px)', Url::buildUrl(config('admin.admin_prefix') . '/widget.images/index', array('fodder' => 'image')), $info['image'] ?? '')->icon('ios-add')->width('960px')->appendValidate(Iview::validateStr()->message('请选择图标(建议尺寸:100px*100px)')->required())->height('505px')->modal(['footer-hide' => true]);
  138. $f[] = Form::number('sort', '排序', (int)($info['sort'] ?? 0))->min(0)->min(0);
  139. return $f;
  140. }
  141. }