DiyProServices.php 8.6 KB

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