CloudStockPriceDifferenceGiftPackageService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. namespace addons\CloudStock\common\services;
  3. use addons\CloudStock\common\models\CloudStockAgent;
  4. use addons\CloudStock\common\models\CloudStockGoods;
  5. use addons\CloudStock\common\models\CloudStockLevel;
  6. use addons\CloudStock\common\models\CloudStockPriceDifferenceGiftPackage;
  7. use common\enums\StatusEnum;
  8. use common\models\goods\Goods;
  9. class CloudStockPriceDifferenceGiftPackageService extends BaseService
  10. {
  11. public function index($params)
  12. {
  13. $mall_id = $this->mall_id;
  14. $where[] = ['mall_id' => $mall_id];
  15. $where[] = ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]];
  16. if (!empty($params['name'])) {
  17. $where[] = ['like', 'name', trim($params['name'])];
  18. }
  19. if (!empty($params['level'])) {
  20. $where[] = ['level' => $params['level']];
  21. }
  22. $params['where'] = $where;
  23. $params['order'] = 'id desc';
  24. $page_data = CloudStockPriceDifferenceGiftPackage::listPage($params, false);
  25. if (!empty($page_data['list'])) {
  26. $goods_ids = [];
  27. $level_arr = [];
  28. foreach ($page_data['list'] as &$item) {
  29. $item['goods_id'] = explode(',', $item['goods_id']);
  30. foreach ($item['goods_id'] as $g_id) {
  31. $goods_ids[] = $g_id;
  32. }
  33. $level_arr[] = $item['level'];
  34. }
  35. $level_list = CloudStockLevel::lists([
  36. 'where' => [
  37. ['mall_id' => $params['mall_id']],
  38. ['level' => $level_arr],
  39. ['status' => [StatusEnum::ENABLED, StatusEnum::DISABLED]]
  40. ],
  41. 'index' => 'level',
  42. 'select' => 'level,name'
  43. ]);
  44. $goods_list = Goods::lists(['where' => [['id' => $goods_ids]], 'index' => 'id', 'select' => 'id,goods_name,cover_pic']);
  45. foreach ($page_data['list'] as &$item) {
  46. $temp_goods_list = [];
  47. foreach ($item['goods_id'] as $g_id) {
  48. if (isset($goods_list[$g_id])) {
  49. $temp_goods_list[] = $goods_list[$g_id];
  50. }
  51. }
  52. $item['goods_list'] = $temp_goods_list;
  53. $item['level_name'] = $level_list[$item['level']]['name'] ?? '';
  54. }
  55. }
  56. return $page_data;
  57. }
  58. public function edit($params)
  59. {
  60. $params['goods_id'] = implode(',', $params['goods_id']);
  61. if (empty($params['recommend_reward'])) {
  62. $params['recommend_reward'] = [];
  63. }
  64. $params['recommend_reward'] = json_encode($params['recommend_reward']);
  65. if (empty($params['pic_type'])) {
  66. $params['pic'] = \Yii::getAlias('@attachurl') . '/static/addons/CloudStock/price_difference_gift_package.png';
  67. }
  68. $res = CloudStockPriceDifferenceGiftPackage::setData($params);
  69. if (empty($res)) {
  70. throw new \Exception(CloudStockPriceDifferenceGiftPackage::$static_error);
  71. }
  72. return $res;
  73. }
  74. public function switchStatus($params)
  75. {
  76. if ($params['status'] == StatusEnum::ENABLED) {
  77. $model = CloudStockPriceDifferenceGiftPackage::findOne(['id' => $params['id'], 'mall_id' => $this->mall_id]);
  78. if (empty($model['level'])) {
  79. throw new \Exception('请先设置升级等级');
  80. }
  81. }
  82. $res = CloudStockPriceDifferenceGiftPackage::updateAll(['status' => $params['status']], ['id' => $params['id'], 'mall_id' => $this->mall_id]);
  83. if ($res === false) {
  84. throw new \Exception(CloudStockPriceDifferenceGiftPackage::$static_error);
  85. }
  86. return true;
  87. }
  88. public function del($params)
  89. {
  90. $mall_id = $this->mall_id;
  91. $model = CloudStockPriceDifferenceGiftPackage::findOne(['id' => $params['id'], 'mall_id' => $mall_id]);
  92. if (!empty($model)) {
  93. $model->status = StatusEnum::DELETE;
  94. $res = $model->save();
  95. if ($res == false) {
  96. throw new \Exception($model->getErrorMessage());
  97. }
  98. }
  99. return true;
  100. }
  101. public function detail($params)
  102. {
  103. if (!empty($params['id'])) {
  104. $id = $params['id'];
  105. $data = CloudStockPriceDifferenceGiftPackage::getOne([['id' => $id]], true);
  106. $data['goods_id'] = explode(',', $data['goods_id']);
  107. $goods_ids = [];
  108. foreach ($data['goods_id'] as $g_id) {
  109. $goods_ids[] = $g_id;
  110. }
  111. $main_goods_list = Goods::lists(['where' => [['id' => $goods_ids]], 'index' => 'id', 'select' => 'id,goods_name,cover_pic']);
  112. $goods_list = [];
  113. foreach ($data['goods_id'] as $id) {
  114. if (isset($main_goods_list[$id])) {
  115. $goods_list[] = $main_goods_list[$id];
  116. }
  117. }
  118. $data['goods_list'] = $goods_list;
  119. $recommend_reward = json_decode($data['recommend_reward'], true);
  120. $recommend_reward = array_column($recommend_reward, null, 'level');
  121. }
  122. $level_list = CloudStockLevel::lists(['where' => [['mall_id' => $this->mall_id], ['status' => StatusEnum::ENABLED]], 'order' => 'level asc', 'select' => 'level,name']);
  123. foreach ($level_list as &$item) {
  124. if (isset($recommend_reward[$item['level']])) {
  125. $item['reward_fee'] = !empty($recommend_reward[$item['level']]['reward_fee']) ? $recommend_reward[$item['level']]['reward_fee'] : 0;
  126. } else {
  127. $item['reward_fee'] = 0;
  128. }
  129. }
  130. $data['recommend_reward'] = $level_list;
  131. return $data;
  132. }
  133. public function getLevelList($params)
  134. {
  135. $mall_id = $this->mall_id;
  136. $where[] = ['mall_id' => $mall_id];
  137. $where[] = ['status' => [StatusEnum::DISABLED, StatusEnum::ENABLED]];
  138. $params['where'] = $where;
  139. $params['select'] = 'level as value,name as label';
  140. $params['order'] = 'value asc';
  141. $list = CloudStockLevel::lists($params);
  142. return $list;
  143. }
  144. public function list($params)
  145. {
  146. $cloud_stock_agent = CloudStockAgent::findOne(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'status' => StatusEnum::ENABLED]);
  147. $mall_id = $this->mall_id;
  148. $where[] = ['mall_id' => $mall_id];
  149. $where[] = ['status' => StatusEnum::ENABLED];
  150. $where[] = ['>', 'level', $cloud_stock_agent['level'] ?? 0];
  151. $params['where'] = $where;
  152. $params['order'] = 'id desc';
  153. $params['select'] = 'id,name,price,pic_type,pic,status';
  154. $page_data = CloudStockPriceDifferenceGiftPackage::listPage($params, false);
  155. return $page_data;
  156. }
  157. public function info($params)
  158. {
  159. $id = $params['id'];
  160. $package = CloudStockPriceDifferenceGiftPackage::getOne([['id' => $id]], true);
  161. $goods_ids = explode(',', $package['goods_id']);
  162. $main_goods_list = Goods::lists(['where' => [['id' => $goods_ids]], 'index' => 'id', 'select' => 'id,goods_name,cover_pic,price']);
  163. $list = [];
  164. $obj = new CloudStockPriceDifferenceGiftPackageOrderService(['mall_id' => $this->mall_id, 'user_id' => $this->user_id]);
  165. foreach ($main_goods_list as $item) {
  166. $price = $item['price'];
  167. $self_pickup_num = $cloud_warehouse_num = 0;
  168. $this->getPrice($item['id'], $goods_ids, $price, $package['level']);
  169. $key = $obj->getCacheKey($id);
  170. $field = $obj->getCacheField(CloudStockPriceDifferenceGiftPackageOrderService::$self_pickup, $item['id']);
  171. $cache_data = \Yii::$app->redis->hget($key, $field);
  172. if (!empty($cache_data)) {
  173. $cache_data = json_decode($cache_data, true);
  174. $self_pickup_num = $cache_data['num'];
  175. }
  176. $field = $obj->getCacheField(CloudStockPriceDifferenceGiftPackageOrderService::$cloud_warehouse, $item['id']);
  177. $cache_data = \Yii::$app->redis->hget($key, $field);
  178. if (!empty($cache_data)) {
  179. $cache_data = json_decode($cache_data, true);
  180. $cloud_warehouse_num = $cache_data['num'];
  181. }
  182. $list[] = [
  183. 'goods_id' => $item['id'],
  184. 'goods_name' => $item['goods_name'],
  185. 'price' => $price,
  186. 'cover_pic' => $item['cover_pic'],
  187. 'self_pickup_num' => $self_pickup_num,
  188. 'cloud_warehouse_num' => $cloud_warehouse_num,
  189. ];
  190. }
  191. $cloud_stock_agent_level = 0;
  192. $cloud_stock_agent = CloudStockAgent::findOne(['mall_id' => $this->mall_id, 'user_id' => $this->user_id, 'status' => StatusEnum::ENABLED]);
  193. if (!empty($cloud_stock_agent)) {
  194. $cloud_stock_agent_level = $cloud_stock_agent['level'];
  195. }
  196. $price_diff = $obj->priceDiff($cloud_stock_agent_level, $package['price']);
  197. return ['list' => $list, 'price' => $price_diff];
  198. }
  199. public function getCloudStockGoodsList($goods_ids)
  200. {
  201. $cloud_stock_goods_list = CloudStockGoods::lists([
  202. 'where' => [
  203. ['goods_id' => $goods_ids],
  204. ['status' => StatusEnum::ENABLED]
  205. ],
  206. 'index' => 'goods_id',
  207. 'select' => 'goods_id,price_difference'
  208. ]);
  209. foreach ($cloud_stock_goods_list as &$val) {
  210. $agent_price = json_decode($val['price_difference'], true);
  211. $val['price_difference'] = array_column($agent_price ?? [], null, 'level');
  212. }
  213. return $cloud_stock_goods_list;
  214. }
  215. public function getPrice($goods_id, $goods_ids, &$price, $level)
  216. {
  217. $cloud_stock_goods_list = $this->getCloudStockGoodsList($goods_ids);
  218. if (isset($cloud_stock_goods_list[$goods_id])) {
  219. $cloud_stock_goods = $cloud_stock_goods_list[$goods_id];
  220. if (isset($cloud_stock_goods['price_difference'][$level]) && $cloud_stock_goods['price_difference'][$level]['price'] !== '') {
  221. $price = $cloud_stock_goods['price_difference'][$level]['price'];
  222. }
  223. }
  224. }
  225. }