SupplysService.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. namespace addons\QiJuhe\common\service;
  3. use addons\QiJuhe\common\models\SettingModel;
  4. use addons\QiJuhe\common\models\StorageModel;
  5. use addons\QiJuhe\common\models\SupplysModel;
  6. use common\enums\RabbitMqEnum;
  7. use Yii;
  8. use yii\base\BaseObject;
  9. use yii\data\Pagination;
  10. use yii\helpers\Json;
  11. /**
  12. * 中台服务类
  13. */
  14. class SupplysService extends BaseObject
  15. {
  16. /**
  17. * mall—id
  18. *
  19. * @var integer
  20. */
  21. public $mall_id;
  22. /**
  23. * error
  24. *
  25. * @var string
  26. */
  27. public $error;
  28. /**
  29. * 保存供应链设置
  30. *
  31. * @param integer $id
  32. * @param array $data
  33. * @return SupplysModel|boolean
  34. */
  35. public function save($id, $data)
  36. {
  37. $base_setting = SettingModel::findOne(['mall_id' => $this->mall_id, 'is_delete' => 0]);
  38. if (empty($base_setting)) {
  39. $this->error = '请先保存基础设置';
  40. return false;
  41. }
  42. $model = SupplysModel::findOne(['mall_id' => $this->mall_id, 'id' => $id]);
  43. empty($model) && $model = new SupplysModel();
  44. // 旧配置
  45. $old = json_encode([$model->sell, $model->cost, $model->original]);
  46. foreach ($data as $k => $v) {
  47. is_array($v) && $data[$k] = Json::encode($v);
  48. }
  49. $array['mall_id'] = $this->mall_id;
  50. $array['updated_at'] = time();
  51. empty($model->id) && $array['created_at'] = $array['updated_at'];
  52. $data = array_merge($data, $array);
  53. $model->load($data, '');
  54. // 新配置
  55. $new = json_encode([$model->sell, $model->cost, $model->original]);
  56. if ($old != $new) {
  57. $this->addChangeDelay($model->id);
  58. }
  59. return $model->save();
  60. }
  61. /**
  62. * 获取中台列表
  63. *
  64. * @param array $params 查询条件
  65. * @return array
  66. */
  67. public function getSupplysList($params)
  68. {
  69. $limit = $params['limit'] ?? 30;
  70. $page = $params['page'] ?? 1;
  71. $model = SupplysModel::find();
  72. $model->where(['mall_id' => $this->mall_id]);
  73. !empty($params['name']) && $model->andWhere(['like', 'app_name', $params['name']]);
  74. $pagination = new Pagination(['totalCount' => $model->count(), 'pageSize' => $limit]);
  75. $model->limit($pagination->limit)->offset($pagination->offset);
  76. $list = $model->orderBy('id desc')
  77. ->asArray()
  78. ->all();
  79. $service = new SettingService(['mall_id' => $this->mall_id]);
  80. foreach ($list as $k => $v) {
  81. $list[$k]['callback_url'] = $service->getCallbackUrl() . "?supplys=" . $v['id'];
  82. }
  83. $data['list'] = $list;
  84. $data['page_count'] = $pagination->pageCount;
  85. $data['current_page'] = $page + 1;
  86. return $data;
  87. }
  88. /**
  89. * 获取实时详情
  90. *
  91. * @param integer $id
  92. * @return array
  93. */
  94. public function getInfo($id)
  95. {
  96. $model = SupplysModel::find();
  97. $info = $model
  98. ->where(['id' => $id, 'mall_id' => $this->mall_id])
  99. ->asArray()
  100. ->one();
  101. isset($info['original']) && $info['original'] = Json::decode($info['original']);
  102. isset($info['sell']) && $info['sell'] = Json::decode($info['sell']);
  103. isset($info['cost']) && $info['cost'] = Json::decode($info['cost']);
  104. return $info;
  105. }
  106. /**
  107. * 获取供应链销售策略
  108. *
  109. * @param integer $mall_id
  110. * @param integer $supply_id
  111. * @return array
  112. */
  113. public static function getStrategySet($mall_id, $supply_id)
  114. {
  115. $where = ['id' => $supply_id, 'mall_id' => $mall_id];
  116. $supply_set = SupplysModel::find()->where($where)->select('sell, cost, original')->asArray()->one();
  117. isset($supply_set['original']) && $supply_set['original'] = Json::decode($supply_set['original']);
  118. isset($supply_set['sell']) && $supply_set['sell'] = Json::decode($supply_set['sell']);
  119. isset($supply_set['cost']) && $supply_set['cost'] = Json::decode($supply_set['cost']);
  120. $set = [];
  121. foreach ($supply_set as $setting) {
  122. $set = array_merge($set, $setting);
  123. }
  124. // 销售价
  125. $set['sell_value'] = 100;
  126. if (isset($set['sell_strategy'])) {
  127. switch ($set['sell_strategy']) {
  128. // 按指导价
  129. case '1':
  130. $set['sell_value'] = $set['sell_guide'];
  131. break;
  132. // 按协议价
  133. case '2':
  134. $set['sell_value'] = $set['sell_agreement'];
  135. break;
  136. // 按营销价
  137. case '3':
  138. $set['sell_value'] = $set['sell_sale'];
  139. break;
  140. // 默认100%
  141. default:
  142. $set['sell_value'] = 100;
  143. break;
  144. }
  145. }
  146. // 成本价
  147. $set['cost_value'] = 100;
  148. if (isset($set['cost_strategy'])) {
  149. switch ($set['cost_strategy']) {
  150. // 按指导价
  151. case '1':
  152. $set['cost_value'] = $set['cost_guide'];
  153. break;
  154. // 按协议价
  155. case '2':
  156. $set['cost_value'] = $set['cost_agreement'];
  157. break;
  158. // 按营销价
  159. case '3':
  160. $set['cost_value'] = $set['cost_sale'];
  161. break;
  162. // 默认100%
  163. default:
  164. $set['cost_value'] = 100;
  165. break;
  166. }
  167. }
  168. // 原价
  169. $set['original_value'] = 100;
  170. if (isset($set['original_strategy'])) {
  171. switch ($set['original_strategy']) {
  172. // 按指导价
  173. case '1':
  174. $set['original_value'] = $set['original_guide'];
  175. break;
  176. // 按协议价
  177. case '2':
  178. $set['original_value'] = $set['original_agreement'];
  179. break;
  180. // 按营销价
  181. case '3':
  182. $set['original_value'] = $set['original_sale'];
  183. break;
  184. // 默认100%
  185. default:
  186. $set['original_value'] = 100;
  187. break;
  188. }
  189. }
  190. return $set;
  191. }
  192. /**
  193. * 获取设置
  194. *
  195. * @param integer $mall_id
  196. * @param integer $supply_id
  197. * @return array
  198. */
  199. public static function getSet($mall_id, $supply_id)
  200. {
  201. $base_setting = SettingService::getSet($mall_id);
  202. $set = self::getStrategySet($mall_id, $supply_id);
  203. return array_merge($base_setting, $set);
  204. }
  205. /**
  206. * 添加修改延迟任务
  207. *
  208. * @param int $supply_id
  209. * @return void
  210. */
  211. public function addChangeDelay($supply_id = null)
  212. {
  213. // batch
  214. /* $batch = StorageModel::find()
  215. ->select('id, mall_id, goods_id')
  216. ->where(['mall_id' => $this->mall_id, 'supply_id' => $supply_id, 'is_import' => 2, 'is_delete' => 0]);
  217. foreach ($batch->batch(20) as $items) {
  218. // ID
  219. $ids = array_column($items, 'id');
  220. // 添加延迟任务
  221. Yii::$app->services->rabbitMq->delay(5, [
  222. 'mall_id' => $this->mall_id,
  223. 'ids' => $ids,
  224. ], self::class, 'addChangeTask');
  225. } */
  226. Yii::$app->services->rabbitMq->delay(5, [
  227. 'mall_id' => $this->mall_id,
  228. 'supply_id' => $supply_id,
  229. 'page' => 0,
  230. ], self::class, 'addDelayTask');
  231. }
  232. /**
  233. * 添加延迟队列
  234. *
  235. * @param array $params
  236. * @return void
  237. */
  238. public static function addDelayTask(array $params)
  239. {
  240. $page = $params['page'] ?? 0;
  241. $limit = 10000;
  242. $offset = bcmul($limit, $page);
  243. $batch = StorageModel::find()
  244. ->select('id, mall_id, goods_id')
  245. ->where(['mall_id' => $params['mall_id'], 'supply_id' => $params['supply_id'], 'is_import' => 2, 'is_delete' => 0])
  246. ->orderBy('id asc')
  247. ->limit($limit)
  248. ->offset($offset);
  249. $count = 0;
  250. foreach ($batch->batch(20) as $items) {
  251. // ID
  252. $ids = array_column($items, 'id');
  253. // 添加延迟任务
  254. Yii::$app->services->rabbitMq->delay(5, [
  255. 'mall_id' => $params['mall_id'],
  256. 'ids' => $ids,
  257. ], self::class, 'addChangeTask');
  258. $count = bcadd($count, count($items));
  259. }
  260. Yii::error('当前数量: ' . $count);
  261. if ($count == $limit) {
  262. $page++;
  263. Yii::$app->services->rabbitMq->delay(5, array_merge($params, ['page' => $page]), self::class, 'addDelayTask');
  264. }
  265. }
  266. /**
  267. * 添加修改任务
  268. *
  269. * @param array $params
  270. * @return void
  271. */
  272. public static function addChangeTask(array $params)
  273. {
  274. QiGoodsService::handleSync($params);
  275. }
  276. }