BoxService.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. namespace App\Service\Box;
  3. use App\Model\Box;
  4. use App\Model\BoxDetail;
  5. use App\Model\Header_ext;
  6. use App\Model\SaleOrdersProduct;
  7. use App\Service\Service;
  8. use Illuminate\Support\Facades\DB;
  9. /**
  10. * 包装相关
  11. * @package App\Models
  12. */
  13. class BoxService extends Service
  14. {
  15. protected static $instance;
  16. protected static $box_header;
  17. protected static $box_detail_header;
  18. protected static $box_hook;
  19. public $lock_key = 'box';
  20. public function __construct()
  21. {
  22. self::$box_header = Header_ext::where('type', 'box')->pluck('value', 'key')->toArray();
  23. self::$box_detail_header = Header_ext::where('type', 'box_detail')->pluck('value', 'key')->toArray();
  24. self::$box_hook = BoxHookService::getInstance();
  25. }
  26. /**
  27. * 包装
  28. * @param $data
  29. * @return array
  30. */
  31. public function boxIn($data)
  32. {
  33. if(!isset($data['param'])) return [false,'not found param'];
  34. $param = $data['param'];
  35. // $param = [
  36. // [
  37. // 'id' => 716,
  38. // 'param' => [
  39. // '1',
  40. // '1'
  41. // ],
  42. // ], [
  43. // 'id' => 723,
  44. // 'param' => [
  45. // '40',
  46. // '40'
  47. // ],
  48. // ],
  49. // ];
  50. $ids = [];
  51. $key_list = [];
  52. foreach ($param as $v) {
  53. $ids[] = $v['id'];
  54. $total = 0;
  55. foreach ($v['param'] as $vv) {
  56. $total += $vv;
  57. }
  58. $key_list[$v['id']] = [
  59. 'detail' => $v['param'],
  60. 'total' => $total,
  61. ];
  62. }
  63. try{
  64. DB::beginTransaction();
  65. $product_list = SaleOrdersProduct::wherein('id', $ids)->get()->toArray();
  66. foreach ($product_list as $v) {
  67. $key = $this->lock_key.'_'.$v['id'];
  68. $lock_status = $this->isLock($key);
  69. if(!$lock_status) return [false,'操作过于频繁'];
  70. $num_list = $key_list[$v['id']];
  71. $total = $num_list['total'];
  72. $detail = $num_list['detail'];
  73. $un_box_num = $v['order_quantity'] - $v['box_num'];
  74. if ($total > $un_box_num) return [false, $v['product_title'] . '数量不足'];
  75. $ext_1 = $v['customer_no'];
  76. $ext_2 = $v['customer_name'];
  77. $ext_3 = $v['product_no'];
  78. $ext_4 = $v['product_title'];
  79. $ext_5 = $v['product_size'];
  80. $out_order_no = $v['out_order_no'];
  81. $top_id = $v['id'];
  82. foreach ($detail as $vv){
  83. $box_insert = [
  84. 'out_order_no' => $out_order_no,
  85. 'top_id' => $top_id,
  86. 'ext_1' => $ext_1,
  87. 'ext_2' => $ext_2,
  88. 'ext_3' => $ext_3,
  89. 'ext_4' => $ext_4,
  90. 'ext_5' => $ext_5,
  91. 'num' => $vv,
  92. ];
  93. list($status,$msg) = self::$box_hook->boxInsert($box_insert);
  94. if(!$status) {
  95. $this->delLock($key);
  96. DB::rollBack();
  97. return [false,$msg];
  98. }
  99. }
  100. $this->delLock($key);
  101. SaleOrdersProduct::where('id',$top_id)->update([
  102. 'box_num' => DB::raw('box_num + '.$total),
  103. ]);
  104. }
  105. DB::commit();
  106. return [true,''];
  107. }catch (\Exception $e){
  108. DB::rollBack();
  109. return [false,$e->getLine().':'.$e->getMessage()];
  110. }
  111. }
  112. /**
  113. * 包装详情1
  114. * @param $data
  115. * @return array
  116. */
  117. public function boxDetail($data)
  118. {
  119. list($status, $data) = self::$box_hook->boxDetail($data);
  120. if (!$status) return [false, $data];
  121. return [true, $data];
  122. }
  123. }