DiyProServices.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2026 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\diy;
  12. use app\dao\diy\DiyDao;
  13. use app\services\article\ArticleServices;
  14. use app\services\BaseServices;
  15. use app\services\product\product\StoreCategoryServices;
  16. use app\services\product\product\StoreProductServices;
  17. use crmeb\exceptions\AdminException;
  18. use crmeb\services\CacheService;
  19. class DiyProServices extends BaseServices
  20. {
  21. public function __construct(DiyDao $dao)
  22. {
  23. $this->dao = $dao;
  24. }
  25. public function getDiyProVersion(int $id)
  26. {
  27. if ($id) {
  28. $where = ['id' => $id];
  29. } else {
  30. $where = ['status' => 1, 'is_pro' => 1, 'is_del' => 0];
  31. }
  32. $data = $this->dao->getOne($where, 'version,is_diy');
  33. if (isset($data['version']) && isset($data['is_diy'])) {
  34. return $data;
  35. } else {
  36. return $this->dao->getOne($where, 'version,is_diy');
  37. }
  38. }
  39. public function getList($type = 'list')
  40. {
  41. [$page, $limit] = $this->getPageValue();
  42. if ($type == 'link') $limit = 1000;
  43. $where['is_pro'] = 1;
  44. $list = $this->dao->getDiyList($where, $page, $limit, ['id', 'name', 'type', 'add_time', 'update_time', 'is_diy', 'status', 'is_pro']);
  45. $count = $this->dao->count($where + ['is_del' => 0]);
  46. return compact('list', 'count');
  47. }
  48. public function getInfo($id)
  49. {
  50. if (!(int)$id) throw new AdminException('参数错误');
  51. $info = $this->dao->get((int)$id);
  52. if (!$info) throw new AdminException('模板不存在');
  53. $info = $info->toArray();
  54. $productServices = app()->make(StoreProductServices::class);
  55. $articleServices = app()->make(ArticleServices::class);
  56. $storeCategoryServices = app()->make(StoreCategoryServices::class);
  57. $info['value'] = json_decode($info['value'], true);
  58. if ($info['value']) {
  59. foreach ($info['value'] as &$item) {
  60. switch ($item['name']) {
  61. case 'goodList'://商品列表
  62. $typeConfig = $item['typeConfig']['activeValue'] ?? 0;
  63. $where = [];
  64. $num = $item['numberConfig']['val'] ?? 50;
  65. $sort = $item['goodsSort']['type'] ?? 0;
  66. if ($sort == 1) {
  67. $where['salesOrder'] = 'desc';
  68. } elseif ($sort == 2) {
  69. $where['priceOrder'] = 'desc';
  70. }
  71. $item['goodsList']['list'] = [];
  72. $where['is_show'] = 1;
  73. $where['is_del'] = 0;
  74. switch ($typeConfig) {
  75. case 1://指定商品
  76. $where['ids'] = $item['goodsList']['ids'] ?? [];
  77. $item['goodsList']['list'] = $productServices->getSearchList($where, 0, 0, ['id,store_name,cate_id,image,IFNULL(sales, 0) + IFNULL(ficti, 0) as sales,price,stock,activity,ot_price,spec_type,recommend_image,unit_name,is_vip,vip_price']);
  78. break;
  79. case 3://指定分类
  80. $cateIds = $item['classList']['classVal'] ?? [];
  81. if ($cateIds) $where['cate_id'] = $cateIds;
  82. $item['productList']['list'] = $productServices->getSearchList($where, 0, $num, ['id,store_name,cate_id,image,IFNULL(sales, 0) + IFNULL(ficti, 0) as sales,price,stock,activity,ot_price,spec_type,recommend_image,unit_name,is_vip,vip_price']);
  83. break;
  84. case 4://商品标签
  85. $storeLabelIds = $item['goodsLabel']['activeValue'] ?? [];
  86. if ($storeLabelIds) $where['store_label_id'] = $storeLabelIds;
  87. $item['productList']['list'] = $productServices->getSearchList($where, 0, $num, ['id,store_name,cate_id,image,IFNULL(sales, 0) + IFNULL(ficti, 0) as sales,price,stock,activity,ot_price,spec_type,recommend_image,unit_name,is_vip,vip_price']);
  88. break;
  89. }
  90. break;
  91. case 'articleList'://文章
  92. if ($item['selectConfig']['activeValue'] ?? 0) {
  93. $data = $articleServices->getList(['cid' => $item['selectConfig']['activeValue'] ?? 0], 0, $item['numConfig']['val'] ?? 0);
  94. }
  95. $item['selectList']['list'] = $data['list'] ?? [];
  96. break;
  97. case 'promotionList'://促销列表
  98. if (isset($item['tabConfig']['list']) && $item['tabConfig']['list']) {
  99. $list = $item['tabConfig']['list'];
  100. if ($list) {
  101. foreach ($list as &$tabValue) {
  102. $where = [];
  103. //选择方式
  104. $selectValue = $tabValue['tabVal'] ?? 0;
  105. $num = $tabValue['numConfig']['val'] ?? 50;
  106. $sort = $tabValue['goodsSort'] ?? 0;
  107. if ($sort == 1) {
  108. $where['salesOrder'] = 'desc';
  109. } elseif ($sort == 2) {
  110. $where['priceOrder'] = 'desc';
  111. }
  112. if ($selectValue == 1 && isset($tabValue['goodsList']['ids']) && count($tabValue['goodsList']['ids'])) {//手动选商品
  113. $where['ids'] = $tabValue['goodsList']['ids'];
  114. $tabValue['goodsList']['list'] = $productServices->getSearchList($where, 0, 0, ['id,store_name,cate_id,image,IFNULL(sales, 0) + IFNULL(ficti, 0) as sales,price,stock,activity,ot_price,spec_type,recommend_image,unit_name,is_vip,vip_price']);
  115. } elseif ((isset($tabValue['selectConfig']['activeValue']) && $tabValue['selectConfig']['activeValue']) || (isset($tabValue['goodsLabel']['activeValue']) && $tabValue['goodsLabel']['activeValue'])) {//选分类 、标签
  116. $where['cate_id'] = $tabValue['selectConfig']['activeValue'] ?? 0;
  117. $storeLabelIds = $tabValue['goodsLabel']['activeValue'] ?? [];
  118. if ($storeLabelIds) {
  119. $where['store_label_id'] = $storeLabelIds;
  120. }
  121. $where['is_show'] = 1;
  122. $where['is_del'] = 0;
  123. $tabValue['goodsList']['list'] = $productServices->getSearchList($where, 0, $num, ['id,store_name,cate_id,image,IFNULL(sales, 0) + IFNULL(ficti, 0) as sales,price,stock,activity,ot_price,spec_type,recommend_image,unit_name,is_vip,vip_price']);
  124. }
  125. }
  126. $item['tabConfig']['list'] = $list;
  127. }
  128. }
  129. break;
  130. }
  131. }
  132. }
  133. return compact('info');
  134. }
  135. public function saveInfo(int $id = 0, array $data = [])
  136. {
  137. if ($id) {
  138. $data['update_time'] = time();
  139. $res = $this->dao->update($id, $data);
  140. if (!$res) throw new AdminException('修改失败');
  141. } else {
  142. $data['add_time'] = time();
  143. $data['update_time'] = time();
  144. $data['is_diy'] = 1;
  145. $data['is_pro'] = 1;
  146. $data['type'] = 2;
  147. $res = $this->dao->save($data);
  148. if (!$res) throw new AdminException('保存失败');
  149. $id = $res->id;
  150. }
  151. return $id;
  152. }
  153. public function updateName($id, $name)
  154. {
  155. $this->dao->update($id, ['name' => $name]);
  156. return true;
  157. }
  158. public function exportDIYData($id)
  159. {
  160. $info = $this->dao->get($id);
  161. if (!$info) throw new AdminException('数据不存在');
  162. return $info['value'];
  163. }
  164. public function importDIYData($content)
  165. {
  166. $data = [
  167. 'name' => 'DIY导入数据',
  168. 'version' => uniqid(),
  169. 'value' => $content,
  170. 'add_time' => time(),
  171. 'update_time' => time(),
  172. 'type' => 2,
  173. 'is_show' => 1,
  174. 'is_diy' => 1,
  175. 'is_pro' => 1,
  176. ];
  177. $this->dao->save($data);
  178. return true;
  179. }
  180. }