LiveOrderController.php 2.6 KB

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