PriceDifferenceProfitService.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <?php
  2. namespace addons\CloudStock\common\services;
  3. use addons\CloudStock\common\enums\CloudStockEnum;
  4. use addons\CloudStock\common\models\CloudStockAgent;
  5. use addons\CloudStock\common\models\CloudStockBuyingOrder;
  6. use addons\CloudStock\common\models\CloudStockDeductionDetail;
  7. use addons\CloudStock\common\models\CloudStockGoods;
  8. use addons\CloudStock\common\models\CloudStockGoodsStockLog;
  9. use addons\CloudStock\common\models\CloudStockLossAmount;
  10. use addons\CloudStock\common\models\CloudStockPaymentLog;
  11. use addons\CloudStock\common\models\CloudStockPriceDifferenceProfit;
  12. use common\components\export\CsvTool;
  13. use common\enums\DownloadEnum;
  14. use common\enums\StatusEnum;
  15. use common\helpers\DateHelper;
  16. use common\models\common\Download;
  17. use common\models\goods\Goods;
  18. use common\models\user\User;
  19. class PriceDifferenceProfitService extends BaseService
  20. {
  21. public $agent_price_arr;
  22. public $order_no;
  23. public function typeMap()
  24. {
  25. //1:上级配货,2:下级补货;3:会员买货
  26. return [
  27. 1 => '上级配货',
  28. 2 => '下级补货',
  29. 3 => '会员买货'
  30. ];
  31. }
  32. public function index($params)
  33. {
  34. $where = [];
  35. $this->getWhere($params, $where);
  36. $params['where'] = $where;
  37. $params['order'] = 'id desc';
  38. $page_data = CloudStockPriceDifferenceProfit::listPage($params, false, true);
  39. if (!empty($page_data['list'])) {
  40. $user_ids = array_column($page_data['list'], 'user_id');
  41. $child_ids = array_column($page_data['list'], 'child_id');
  42. $goods_ids = array_column($page_data['list'], 'goods_id');
  43. $goods_list = Goods::lists(['where' => [
  44. ['id' => $goods_ids],
  45. ['mall_id' => $this->mall_id]
  46. ],
  47. 'index' => 'id',
  48. ]);
  49. $user_list = User::lists(['where' => [
  50. ['id' => array_merge($user_ids, $child_ids)],
  51. ['mall_id' => $this->mall_id]
  52. ],
  53. 'index' => 'id',
  54. ]);
  55. foreach ($page_data['list'] as &$item) {
  56. $item['type_text'] = $this->typeMap()[$item['type']] ?? '';
  57. $item['user'] = $item['from_user'] = [];
  58. $item['cover_pic'] = $item['goods_name'] = '';
  59. if (isset($user_list[$item['user_id']])) {
  60. $item['user'] = [
  61. 'id' => $user_list[$item['user_id']]['id'],
  62. 'nickname' => $user_list[$item['user_id']]['nickname'],
  63. 'mobile' => $user_list[$item['user_id']]['mobile'],
  64. 'avatar_url' => $user_list[$item['user_id']]['avatar_url'],
  65. ];
  66. }
  67. if (isset($goods_list[$item['goods_id']])) {
  68. $item['cover_pic'] = $goods_list[$item['goods_id']]['cover_pic'];
  69. $item['goods_name'] = $goods_list[$item['goods_id']]['goods_name'];
  70. }
  71. if (isset($user_list[$item['child_id']])) {
  72. $item['from_user'] = [
  73. 'id' => $user_list[$item['child_id']]['id'],
  74. 'nickname' => $user_list[$item['child_id']]['nickname'],
  75. 'mobile' => $user_list[$item['child_id']]['mobile'],
  76. 'avatar_url' => $user_list[$item['child_id']]['avatar_url'],
  77. ];
  78. }
  79. }
  80. }
  81. return $page_data;
  82. }
  83. public function priceDifferenceProfitList($params)
  84. {
  85. if (empty($params['month'])) {
  86. $month = date('Y-m');
  87. } else {
  88. $month = $params['month'];
  89. }
  90. $start_time = strtotime($month . '-01 00:00:00');
  91. $end_time = DateHelper::getMonthLastDayTime($start_time);
  92. $params['date'][0] = date('Y-m-d', $start_time);
  93. $params['date'][1] = date('Y-m-d', $end_time);
  94. $params['user_id'] = $this->user_id;
  95. $this->getWhere($params, $where);
  96. $params['where'] = $where;
  97. $params['order'] = 'id desc';
  98. $page_data = CloudStockPriceDifferenceProfit::listPage($params, false, true);
  99. if (!empty($page_data['list'])) {
  100. $user_ids = array_column($page_data['list'], 'user_id');
  101. $child_ids = array_column($page_data['list'], 'child_id');
  102. $goods_ids = array_column($page_data['list'], 'goods_id');
  103. $goods_list = Goods::lists(['where' => [
  104. ['id' => $goods_ids],
  105. ['mall_id' => $this->mall_id]
  106. ],
  107. 'index' => 'id',
  108. ]);
  109. $user_list = User::lists(['where' => [
  110. ['id' => array_merge($user_ids, $child_ids)],
  111. ['mall_id' => $this->mall_id]
  112. ],
  113. 'index' => 'id',
  114. ]);
  115. foreach ($page_data['list'] as &$item) {
  116. $item['type_text'] = $this->typeMap()[$item['type']] ?? '';
  117. $item['from_user_id'] = $item['from_user_mobile'] = '';
  118. $item['goods_name'] = '';
  119. if (isset($goods_list[$item['goods_id']])) {
  120. $item['goods_name'] = $goods_list[$item['goods_id']]['goods_name'];
  121. }
  122. if (isset($user_list[$item['child_id']])) {
  123. $item['from_user_id'] = $user_list[$item['child_id']]['id'];
  124. $item['from_user_mobile'] = $user_list[$item['child_id']]['mobile'];
  125. }
  126. }
  127. }
  128. return $page_data;
  129. }
  130. protected function getWhere($params, &$where)
  131. {
  132. $where[] = ['mall_id' => $this->mall_id, 'status' => StatusEnum::ENABLED];
  133. if (!empty($params['keyword'])) {
  134. $user_list = User::lists(['where' => [
  135. ['mall_id' => $this->mall_id],
  136. ['or',
  137. ['like', 'nickname', $params['keyword']],
  138. ['like', 'mobile', $params['keyword']]]],
  139. 'select' => 'id']);
  140. if (!empty($user_list)) {
  141. $user_ids = array_column($user_list, 'id');
  142. $where[] = ['user_id' => $user_ids];
  143. } else {
  144. $where[] = ['user_id' => []];
  145. }
  146. }
  147. if (!empty($params['user_id'])) {
  148. $where[] = ['user_id' => $params['user_id']];
  149. }
  150. if (!empty($params['order_no'])) {
  151. $where[] = ['order_no' => trim($params['order_no'])];
  152. }
  153. if (!empty($params['type'])) {
  154. $where[] = ['type' => trim($params['type'])];
  155. }
  156. if (!empty($params['date'][0])) {
  157. $where[] = ['>=', 'created_at', strtotime($params['date'][0] . ' 00:00:00')];
  158. }
  159. if (!empty($params['date'][1])) {
  160. $where[] = ['<=', 'created_at', strtotime($params['date'][1] . ' 23:59:59')];
  161. }
  162. }
  163. public function getListExportFieldList()
  164. {
  165. return $this->exportFieldsList();
  166. }
  167. public function exportFieldsList()
  168. {
  169. return [
  170. 'user_id' => '用户ID',
  171. 'nickname' => '用户昵称',
  172. 'mobile' => '手机号',
  173. 'type_text' => '业务类型',
  174. 'money' => '差价利润',
  175. 'order_no' => '订单编号',
  176. 'created_at' => '创建时间',
  177. ];
  178. }
  179. public static function exportData($params)
  180. {
  181. (new PriceDifferenceProfitService(['mall_id' => $params['mall_id']]))->export($params);
  182. }
  183. public function add($params)
  184. {
  185. $res = CloudStockPaymentLog::add($params);
  186. if (empty($res)) {
  187. throw new \Exception(CloudStockPaymentLog::$static_error);
  188. }
  189. if (empty($params['status'])) {
  190. $params['is_frozen'] = true;
  191. } else {
  192. $params['is_frozen'] = false;
  193. }
  194. WithdrawService::handle($params);
  195. return $res;
  196. }
  197. public function export($data)
  198. {
  199. ini_set('memory_limit', '512M');
  200. $download_id = $data['download_id'] ?? 0;
  201. $download = Download::findOne(['id' => $download_id]);
  202. if ($download) {
  203. try {
  204. $params = json_decode($download->params, true);
  205. $filename = $download->name;
  206. $type = $download->type;
  207. $where = [];
  208. $this->getWhere($params, $where);
  209. $params['where'] = $where;
  210. $params['order'] = 'id desc';
  211. $list = CloudStockPriceDifferenceProfit::lists($params);
  212. $fields = $params['fields'];
  213. $fields_arr = $this->exportFieldsList();
  214. $have_select_field_arr = [];
  215. foreach ($fields as $value) {
  216. $have_select_field_arr[] = $fields_arr[$value];
  217. }
  218. $csv = new CsvTool();
  219. $csv->filename = $filename;
  220. $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
  221. $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
  222. $csv->header = $have_select_field_arr;
  223. $user_id_arr = array_column($list, 'user_id');
  224. $user_arr = User::lists([
  225. 'where' => [
  226. ['id' => $user_id_arr]
  227. ],
  228. 'index' => 'id',
  229. ]);
  230. $csv->data = array_map(function ($item) use ($fields, $user_arr) {
  231. $row = [];
  232. foreach ($fields as $field) {
  233. switch (true) {
  234. case $field == 'created_at':
  235. $row[] = !empty($item[$field]) ? date('Y-m-d H:i:s', $item[$field]) : '';
  236. break;
  237. case $field == 'mobile':
  238. $row[] = $user_arr[$item['user_id']]['mobile'] ?? '';
  239. break;
  240. case $field == 'type_text':
  241. $row[] = $this->typeMap()[$item['type']] ?? '';
  242. break;
  243. case $field == 'nickname':
  244. $row[] = $user_arr[$item['user_id']]['nickname'] ?? '';
  245. break;
  246. default:
  247. $row[] = !empty($item[$field]) ? $item[$field] : '';
  248. }
  249. }
  250. return $row;
  251. }, $list);
  252. $csv->export();
  253. $csv->updateDown($download, $params['mall_id']);
  254. return true;
  255. } catch (\Exception $e) {
  256. $download->status = 3;
  257. $res = $download->save();
  258. return $e->getMessage();
  259. }
  260. }
  261. }
  262. /**
  263. * 会员买货订单,货款结算,计算差价利润
  264. * @param $type
  265. * @param $order_no
  266. * @return void
  267. */
  268. public function computeProfitByDelayBuy($type, $order_no)
  269. {
  270. $order = \common\models\order\Order::findOne(['order_no' => $order_no]);
  271. if ($order) {
  272. $this->order_no = $order['order_no'];
  273. $goods_ids = array_column($order['detail'], 'goods_id');
  274. $order_detail_ids = array_column($order['detail'], 'id');
  275. $buy_order_list = CloudStockBuyingOrder::lists([
  276. 'where' => [
  277. ['order_detail_id' => $order_detail_ids]
  278. ],
  279. 'index' => 'goods_id'
  280. ], false);
  281. $goods_stock_logs = CloudStockGoodsStockLog::lists([
  282. 'where' => [
  283. ['goods_id' => $goods_ids],
  284. ['order_id' => array_column($buy_order_list, 'id')],
  285. ['order_type' => CloudStockEnum::STOCK_ORDER_TYPE_USER],
  286. ],
  287. ]);
  288. $stock_log_ids = array_column($goods_stock_logs, 'id');
  289. $deduction_detail_list = CloudStockDeductionDetail::lists([
  290. 'where' => [
  291. ['stock_log_id' => $stock_log_ids],
  292. ['>', 'user_id', 0]
  293. ],
  294. ]);
  295. if (!empty($deduction_detail_list)) {
  296. $user_ids = array_column($deduction_detail_list, 'user_id');
  297. $cloud_stock_agent_list = CloudStockAgent::lists(['where' => [['user_id' => $user_ids], ['status' => StatusEnum::ENABLED]], 'index' => 'user_id']);
  298. foreach ($deduction_detail_list as $value) {
  299. if (empty($buy_order_list[$value['goods_id']])) {
  300. continue;
  301. }
  302. if (empty($cloud_stock_agent_list[$value['user_id']])) {
  303. continue;
  304. }
  305. $buy_order = $buy_order_list[$value['goods_id']];
  306. $parent_agent = $cloud_stock_agent_list[$value['user_id']];
  307. $this->compute($type, $value['num'], $buy_order, $parent_agent);//配货触发差价利润金额
  308. }
  309. }
  310. }
  311. }
  312. /**
  313. * 计算价差利润
  314. * @param $type
  315. * @param $num
  316. * @param $order
  317. * @param $parent_agent
  318. * @return true|void
  319. */
  320. public function compute($type, $num, $order, $parent_agent = null)
  321. {
  322. $cloud_stock_goods = CloudStockGoods::getOne([
  323. ['goods_id' => $order['goods_id']],
  324. ['mall_id' => $this->mall_id],
  325. ['status' => StatusEnum::ENABLED],
  326. ]);
  327. if (empty($cloud_stock_goods)) {
  328. return true;
  329. }
  330. $agent_price_arr = json_decode($cloud_stock_goods['agent_price'], true);
  331. if (empty($agent_price_arr)) {
  332. return true;
  333. }
  334. $agent_price_arr = array_column($agent_price_arr, null, 'level');
  335. $this->agent_price_arr = $agent_price_arr;
  336. switch ($type) {
  337. case 1:
  338. $user_id = $order['user_id'];//配货者
  339. $child_id = $order['child_id'];//配货下级
  340. $this->computeByFill($type, $num, $user_id, $child_id, $order);
  341. break;
  342. case 2:
  343. $user_id = $parent_agent['user_id'];
  344. $child_id = $order['user_id'];
  345. $this->computeByFill($type, $num, $user_id, $child_id, $order);
  346. break;
  347. case 3:
  348. $user_id = $parent_agent['user_id'];
  349. $child_id = $order['user_id'];
  350. $this->computeByBuy($num, $user_id, $child_id, $order);
  351. break;
  352. }
  353. }
  354. /**
  355. * 会员买货
  356. * @param $num
  357. * @param $user_id
  358. * @param $child_id
  359. * @param $order
  360. * @return true|void
  361. */
  362. public function computeByBuy($num, $user_id, $child_id, $order)
  363. {
  364. $mall_id = $order['mall_id'];
  365. $goods_id = $order['goods_id'];
  366. $money = $order['after_distribution_price'];
  367. $order_no = $this->order_no;
  368. $cloud_stock_agent = CloudStockAgent::getOne([
  369. ['user_id' => $user_id],
  370. ['mall_id' => $mall_id],
  371. ['status' => StatusEnum::ENABLED],
  372. ]);
  373. $child_agent_price = $order['after_distribution_price'] / $order['num'];
  374. $agent_price = $this->agent_price_arr[$cloud_stock_agent['level']]['price'] ?? 0;
  375. $price_diff = $child_agent_price - $agent_price;
  376. if ($price_diff <= 0) {
  377. return true;
  378. }
  379. $add = [
  380. 'mall_id' => $mall_id,
  381. 'user_id' => $cloud_stock_agent['user_id'],
  382. 'order_no' => $order_no,
  383. 'num' => $num,
  384. 'money' => $price_diff * $num,
  385. 'goods_id' => $goods_id,
  386. 'child_id' => $child_id,
  387. 'type' => 3,
  388. ];
  389. CloudStockPriceDifferenceProfit::add($add);
  390. $cloud_stock_agent->difference_profit += $add['money'];
  391. $cloud_stock_agent->save();
  392. }
  393. /**
  394. * 补货/配货
  395. * @param $type
  396. * @param $num
  397. * @param $user_id
  398. * @param $child_id
  399. * @param $order
  400. * @return true|void
  401. */
  402. public function computeByFill($type, $num, $user_id, $child_id, $order)
  403. {
  404. $mall_id = $order['mall_id'];
  405. $goods_id = $order['goods_id'];
  406. $order_no = $order['order_no'];
  407. $cloud_stock_agent = CloudStockAgent::getOne([
  408. ['user_id' => $user_id],
  409. ['mall_id' => $mall_id],
  410. ['status' => StatusEnum::ENABLED],
  411. ]);
  412. $child_cloud_stock_agent = CloudStockAgent::getOne([
  413. ['user_id' => $child_id],
  414. ['mall_id' => $mall_id],
  415. ['status' => StatusEnum::ENABLED],
  416. ]);
  417. $agent_price = $this->agent_price_arr[$cloud_stock_agent['level']]['price'] ?? 0;
  418. $child_agent_price = $this->agent_price_arr[$child_cloud_stock_agent['level']]['price'] ?? 0;
  419. $price_diff = $child_agent_price - $agent_price;
  420. if ($price_diff <= 0) {
  421. return true;
  422. }
  423. $add = [
  424. 'mall_id' => $mall_id,
  425. 'user_id' => $cloud_stock_agent['user_id'],
  426. 'order_no' => $order_no,
  427. 'num' => $num,
  428. 'money' => $price_diff * $num,
  429. 'goods_id' => $goods_id,
  430. 'child_id' => $child_id,
  431. 'type' => $type,
  432. ];
  433. CloudStockPriceDifferenceProfit::add($add);
  434. $cloud_stock_agent->difference_profit += $add['money'];
  435. $cloud_stock_agent->save();
  436. }
  437. }