LiveOrderController.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. <?php
  2. namespace ghs\controllers;
  3. use bizGhs\live\classes\LiveOrderClass;
  4. use bizGhs\product\classes\ProductClass;
  5. use common\components\util;
  6. use Yii;
  7. class LiveOrderController extends BaseController
  8. {
  9. //走播单列表 ssh 20241003
  10. public function actionList()
  11. {
  12. $get = Yii::$app->request->get();
  13. $staffId = $get['staffId'] ?? 0;
  14. $where = [];
  15. $where['mainId'] = $this->mainId;
  16. $where['staffId'] = $staffId;
  17. $respond = LiveOrderClass::getOrderList($where);
  18. util::success($respond);
  19. }
  20. //添加订单 ssh 20241003
  21. public function actionAddOrder()
  22. {
  23. $post = Yii::$app->request->post();
  24. $itemId = $post['itemId'] ?? 0;
  25. if (empty($itemId)) {
  26. util::fail('请选花材');
  27. }
  28. $product = ProductClass::getById($itemId, true);
  29. if (empty($product)) {
  30. util::fail('没有找到花材');
  31. }
  32. if ($product->mainId != $this->mainId) {
  33. util::fail('不是你的花材');
  34. }
  35. $selectCustom = $post['selectCustom'] ?? '';
  36. if (empty($selectCustom)) {
  37. util::fail('没有客户');
  38. }
  39. $arr = json_decode($selectCustom, true);
  40. if (empty($arr) || is_array($arr) == false) {
  41. util::fail('没有客户信息');
  42. }
  43. $mainId = $this->mainId;
  44. $hasItem = LiveOrderClass::getByCondition(['mainId' => $mainId, 'itemId' => $itemId, 'switch' => 0], true);
  45. if (!empty($hasItem)) {
  46. util::fail('这个花材已添加过');
  47. }
  48. $has = [];
  49. $customList = [];
  50. foreach ($arr as $key => $info) {
  51. $customId = $info['customId'] ?? 0;
  52. if (isset($has[$customId])) {
  53. util::fail('重复的客户');
  54. }
  55. $has[$customId] = 1;
  56. $customName = $info['customName'] ?? '';
  57. $num = $info['num'] ?? 0;
  58. $price = $info['price'] ?? 0;
  59. $customList[] = ['customId' => $customId, 'customName' => $customName, 'num' => $num, 'price' => $price];
  60. }
  61. $staff = $this->shopAdmin;
  62. $staffId = $staff->id ?? 0;
  63. $staffName = $staff->name ?? '';
  64. $params = ['product' => $product, 'customList' => $customList, 'staffId' => $staffId, 'staffName' => $staffName];
  65. $connection = Yii::$app->db;
  66. $transaction = $connection->beginTransaction();
  67. try {
  68. LiveOrderClass::addOrder($params, $mainId);
  69. $transaction->commit();
  70. util::complete('创建成功');
  71. } catch (\Exception $e) {
  72. Yii::info("操作失败原因:" . $e->getMessage());
  73. $transaction->rollBack();
  74. util::fail('操作失败');
  75. }
  76. }
  77. }