CommandService.php 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. namespace App\Service;
  3. use App\Model\CommandList;
  4. use App\Model\OrdersProduct;
  5. use App\Model\OrdersProductBom;
  6. use App\Model\OrdersProductMain;
  7. use App\Model\OrdersProductProcess;
  8. use Illuminate\Support\Facades\DB;
  9. class CommandService extends Service
  10. {
  11. public function getForSettle(){
  12. $command = CommandList::where('del_time',0)
  13. ->where('is_use',0)
  14. ->first();
  15. if(! $command) return;
  16. $command->is_use = 1;
  17. $command->save();
  18. if($command->function == "productionAdd"){
  19. $args = json_decode($command->data, true);
  20. $this->productionAdd($args['data'], $args['user'],$command->id);
  21. }
  22. // if($command->function == "productionDel"){
  23. // $args = json_decode($command->data, true);
  24. // $this->productionDel($args['data'],$command->id);
  25. // }
  26. }
  27. public function productionAdd($data,$user,$id){
  28. $service = new ProductionOrderService();
  29. list($status,$msg) = $service->orderRule($data);
  30. if(! $status) {
  31. CommandList::where('id', $id)
  32. ->update([
  33. 'mark' => $msg,
  34. 'del_time' => 1,
  35. ]);
  36. return;
  37. }
  38. $production_no = $service->setOrderNO();
  39. if(empty($production_no)) {
  40. CommandList::where('id', $id)
  41. ->update([
  42. 'mark' => '单据号生成失败',
  43. 'del_time' => 1,
  44. ]);
  45. return;
  46. }
  47. //工序
  48. $process_arr = $data['process_id'];
  49. try{
  50. DB::beginTransaction();
  51. $time = time();
  52. //主表数据写入
  53. OrdersProductMain::insert(['production_no' => $production_no,'crt_time' => $time,'crt_id' => $user['id'], 'process_id' => implode(',',$process_arr)]);
  54. //生产数据的源数据
  55. $result = $msg[0];
  56. $quantity_map = $msg[1];
  57. $boom = $process = [];
  58. foreach ($result as $key => $value){
  59. $result[$key]['process_id'] = implode(',', $process_arr);
  60. $result[$key]['production_no'] = $production_no;
  61. $result[$key]['production_quantity'] = $quantity_map[$value['sale_orders_product_id']];
  62. $result[$key]['production_time'] = $time;
  63. $result[$key]['crt_id'] = $user['id'];
  64. }
  65. OrdersProduct::insert($result);
  66. //反写销售订单中 已经生产数量
  67. $service->writeProductionQuantity($data['id']);
  68. //获取上一次插入订单的所有id
  69. $last_insert_id = OrdersProduct::where('production_no',$production_no)->select('id')->get()->toArray();
  70. $last_insert_id = array_column($last_insert_id,'id');
  71. $size = 0;
  72. //组织process bom的数据 写入与主表的关联id
  73. $time_arr = [];
  74. foreach ($result as $key => $value){
  75. $quantity_tmp = $quantity_map[$value['sale_orders_product_id']];
  76. $quantity_tmp = $quantity_tmp * 1000;
  77. $time_tmp = date("Ymd", $value['out_order_no_time']);
  78. if(! in_array($time_tmp,$time_arr)) $time_arr[] = $time_tmp;
  79. for ($i = 1; $i <= $quantity_tmp; $i++) {
  80. if($size == 500){
  81. $this->batchInsertData($boom,$process,$time_arr);
  82. unset($boom);unset($process);
  83. $size = 0;
  84. echo "500条写入成功";
  85. }
  86. $boom[$time_tmp][] = [
  87. 'order_product_id' => $last_insert_id[$key],
  88. 'production_no' => $production_no,
  89. 'order_no' => $value['order_no'],
  90. 'out_order_no' => $value['out_order_no'],
  91. 'product_no' => $value['product_no'],
  92. 'product_title' => $value['product_title'],
  93. 'crt_time' => $time
  94. ];
  95. foreach ($process_arr as $k => $v){
  96. $process[$time_tmp][] = [
  97. 'order_product_id' => $last_insert_id[$key],
  98. 'production_no' => $production_no,
  99. 'process_id' => $v,
  100. 'order_no' => $value['order_no'],
  101. 'out_order_no' => $value['out_order_no'],
  102. 'product_no' => $value['product_no'],
  103. 'product_title' => $value['product_title'],
  104. 'crt_time' => $time,
  105. 'sort' => $k,
  106. ];
  107. }
  108. $size ++;
  109. }
  110. }
  111. if($size > 0){
  112. $this->batchInsertData($boom, $process, $time_arr);
  113. echo "剩余 " . $size . " 条写入成功\n";
  114. }
  115. foreach ($result as $value){
  116. $key = "productionAdd" . $value['sale_orders_product_id'];
  117. $this->dellimitingSendRequestBackgNeed($key);
  118. }
  119. CommandList::where('id', $id)
  120. ->update([
  121. 'mark' => '',
  122. 'del_time' => time(),
  123. 'is_use' => 2
  124. ]);
  125. DB::commit();
  126. }catch (\Exception $e){
  127. DB::rollBack();
  128. CommandList::where('id', $id)
  129. ->update([
  130. 'mark' => $e->getLine().':'.$e->getMessage(),
  131. 'del_time' => 1,
  132. ]);
  133. }
  134. }
  135. function batchInsertData(array $boom, array $process, array $time_arr)
  136. {
  137. foreach ($time_arr as $t) {
  138. if (isset($boom[$t]) && count($boom[$t]) > 0) {
  139. $boom_model = new OrdersProductBom(['channel'=> $t]);
  140. $boom_model->insert($boom[$t]);
  141. }
  142. if (isset($process[$t]) && count($process[$t]) > 0) {
  143. $process_model = new OrdersProductProcess(['channel' => $t]);
  144. $process_model->insert($process[$t]);
  145. }
  146. }
  147. }
  148. public function productionDel($data,$command_id){
  149. $id = $data['id'];
  150. try {
  151. DB::beginTransaction();
  152. //生产订单
  153. OrdersProduct::whereIn('id',$id)->update([
  154. 'del_time' => time()
  155. ]);
  156. //生产订单主表
  157. $subquery = DB::table('orders_product')
  158. ->select('production_no')
  159. ->whereIn('id',$id);
  160. $order_no = DB::table('orders_product')
  161. ->select('orders_product.production_no','orders_product.del_time')
  162. ->joinSub($subquery, 'sub', function ($join) {
  163. $join->on('orders_product.production_no', '=', 'sub.production_no');
  164. })->get()->toArray();
  165. $update_order = [];
  166. if(! empty($order_no)){
  167. $tmp = [];
  168. foreach ($order_no as $value){
  169. $value = (array)$value;
  170. if($value['del_time'] == 0){
  171. $tmp[] = $value['production_no'];
  172. }else{
  173. $update_order[] = $value['production_no'];
  174. }
  175. }
  176. $tmp = array_unique($tmp);
  177. $update_order = array_unique($update_order);
  178. $update_order = array_diff($update_order, $tmp);
  179. }
  180. if(! empty($update_order)) OrdersProductMain::whereIn('production_no',$update_order)->update(['del_time' => time()]);
  181. //生产订单子表
  182. $message = OrdersProduct::whereIn('id',$id)->select('out_order_no_time','sale_orders_product_id')->get()->toArray();
  183. //销售订单里的已生产数量
  184. (new ProductionOrderService())->writeProductionQuantityDEL(array_column($message,'sale_orders_product_id'));
  185. $time = array_unique(array_column($message,'out_order_no_time'));
  186. $arr_time = [];
  187. if(! empty($time)){
  188. foreach ($time as $value){
  189. $time_tmp = date("Ymd", $value);
  190. if(! in_array($time_tmp,$arr_time)) $arr_time[] = $time_tmp;
  191. }
  192. }
  193. if(! empty($arr_time)){
  194. foreach ($arr_time as $value){
  195. $modelBom = new OrdersProductBom(['channel'=> $value]);
  196. $modelBom->whereIn('order_product_id',$id)->update(['del_time' => time()]);
  197. $modelProcess = new OrdersProductProcess(['channel' => $value]);
  198. $modelProcess->whereIn('order_product_id',$id)->update(['del_time' => time()]);
  199. }
  200. }
  201. DB::commit();
  202. }catch (\Throwable $e){
  203. DB::rollBack();
  204. CommandList::where('id', $command_id)
  205. ->update([
  206. 'mark' => $e->getLine().':'.$e->getMessage(),
  207. 'del_time' => 1,
  208. ]);
  209. }
  210. }
  211. }