CommandService.php 9.2 KB

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