DwyService.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  1. <?php
  2. namespace App\Service;
  3. /**
  4. * dwy相关
  5. * @package App\Models
  6. */
  7. class DwyService extends Service
  8. {
  9. private $url = 'https://tm.dwycloud.com';
  10. protected $num = 0;
  11. protected static $instance;
  12. public static function getInstance(): self
  13. {
  14. if (self::$instance == null) {
  15. self::$instance = new DwyService();
  16. }
  17. return self::$instance;
  18. }
  19. public function setBoxData($token,$dv,$data,$box_list,$old_data){
  20. //
  21. $this->num = 0;
  22. // 测试
  23. // $token = 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI1NjYxNTc5MjMwMDUzNzAzNjgiLCJhdXRoIjoiUk9MRV9CUkFORF9TVVBQTElFUixST0xFX1VTRV9CUkFORF9TVVBQTElFUixST0xFX0lOTkVSX1VTRVIsUk9MRV9JTlRFUkZBQ0UsUk9MRV9TRU5EX0JSQU5EX1NVUFBMSUVSIiwidG9rZW5JZCI6IjQiLCJleHAiOjE3MTk2NzI1OTN9.VKDLhTILWHe6MIAlzNvxCk4pBQaV-Et1UFYb12xTxl-QcVWpYaWLZnk6_QJ2bDXMplp75DuIKADmjuHDjDFSWQ';
  24. // $dv = 'DV00001';
  25. // $box_list = [
  26. // 'CK00007',
  27. // 'CK00008',
  28. // ];
  29. //
  30. // $data = [
  31. // 'CK00007'=>[
  32. // 'fake_qty' => '3',
  33. // 'detail' => [
  34. // "WJY98V5BJRHDEA3IQT4R",
  35. // "ULW9TDTIV092DGYD81SU"
  36. //
  37. //
  38. // ],
  39. // ],
  40. // 'CK00008'=>[
  41. // 'fake_qty' => '3',
  42. // 'detail' => [
  43. // "20A70CCRQ96EDM7X6C07",
  44. // "4V6GUEDJSLVAS89JJ557"
  45. //
  46. // ],
  47. // ]
  48. // ];
  49. // $old_data = json_decode('{
  50. // "key": "DV00001",
  51. // "lead_bind": {
  52. // "device_code": "DV00001",
  53. // "material_list": [
  54. // {
  55. // "product_code": "BC020202000999",
  56. // "product_code_show": "\u4fe1\u606f\u90e8\u6d4b\u8bd51800\u9999\u6749\u6728",
  57. // "order_item_id": "581719319017431040",
  58. // "order_item_id_show": "SO202405240088",
  59. // "material_code": "40010501000999",
  60. // "material_code_show": "\u94f6\u76fe\u6297\u83cc\u5706\u6807\uff08\u6d4b\u8bd5\u767d\u6807\uff09",
  61. // "process_title": null,
  62. // "process_title_two": null,
  63. // "color": null,
  64. // "fake_qty": 3,
  65. // "color_two": null,
  66. // "brand_qr_code_list": [
  67. // "WJY98V5BJRHDEA3IQT4R",
  68. // "ULW9TDTIV092DGYD81SU"
  69. // ],
  70. // "order_no_list": "SO202405240088"
  71. // },
  72. // {
  73. // "product_code": "BC020202000999",
  74. // "product_code_show": "\u4fe1\u606f\u90e8\u6d4b\u8bd51800\u9999\u6749\u6728",
  75. // "order_item_id": "581719319017431040",
  76. // "order_item_id_show": "SO202405240088",
  77. // "material_code": "40010101000999",
  78. // "material_code_show": "E0\u6279\u96f6\u514d\u6f06\uff08\u6d4b\u8bd5\u767d\u6807\uff09",
  79. // "process_title": null,
  80. // "process_title_two": null,
  81. // "color": null,
  82. // "fake_qty": 3,
  83. // "color_two": null,
  84. // "brand_qr_code_list": [
  85. // "20A70CCRQ96EDM7X6C07",
  86. // "4V6GUEDJSLVAS89JJ557"
  87. // ],
  88. // "order_no_list": "SO202405240088"
  89. // }
  90. // ]
  91. // },
  92. // "lead_out": {
  93. // "brand_out_stock_list": [
  94. // {
  95. // "in_out_type_code": "CK00004",
  96. // "in_out_type_code_show": "\u5546\u6807\u4ed3\u6b63\u5e38\u9886\u6807\u51fa\u5e93",
  97. // "send_box_code": "CK00007",
  98. // "send_box_code_show": "\u5546\u6807\u56db\u53f7\u4ed3",
  99. // "device_code": "DV00001",
  100. // "get_brand_source_dk": "SALE_ORDER",
  101. // "brand_out_stock_dtl": [
  102. // {
  103. // "item_no": 10,
  104. // "brand_qr_code_list": "WJY98V5BJRHDEA3IQT4R,ULW9TDTIV092DGYD81SU",
  105. // "material_code": "40010501000999",
  106. // "restock_qty": 0,
  107. // "material_code_show": "\u94f6\u76fe\u6297\u83cc\u5706\u6807\uff08\u6d4b\u8bd5\u767d\u6807\uff09",
  108. // "unit_code": "ST",
  109. // "unit_code_show": "\u5f20",
  110. // "qty": "3",
  111. // "fake_qty": 3,
  112. // "bus_type_dk": "SALE_ORDER",
  113. // "bus_type_dk_show": "\u9500\u552e\u8ba2\u5355",
  114. // "bus_no": "581719319017431040",
  115. // "bus_no_show": "SO202405240088"
  116. // }
  117. // ]
  118. // },
  119. // {
  120. // "in_out_type_code": "CK00004",
  121. // "in_out_type_code_show": "\u5546\u6807\u4ed3\u6b63\u5e38\u9886\u6807\u51fa\u5e93",
  122. // "send_box_code": "CK00008",
  123. // "send_box_code_show": "\u5546\u6807\u4e94\u53f7\u4ed3",
  124. // "device_code": "DV00001",
  125. // "get_brand_source_dk": "SALE_ORDER",
  126. // "brand_out_stock_dtl": [
  127. // {
  128. // "item_no": 10,
  129. // "brand_qr_code_list": "20A70CCRQ96EDM7X6C07,4V6GUEDJSLVAS89JJ557",
  130. // "material_code": "40010101000999",
  131. // "restock_qty": 0,
  132. // "material_code_show": "E0\u6279\u96f6\u514d\u6f06\uff08\u6d4b\u8bd5\u767d\u6807\uff09",
  133. // "unit_code": "ST",
  134. // "unit_code_show": "\u5f20",
  135. // "qty": "3",
  136. // "fake_qty": 3,
  137. // "bus_type_dk": "SALE_ORDER",
  138. // "bus_type_dk_show": "\u9500\u552e\u8ba2\u5355",
  139. // "bus_no": "581719319017431040",
  140. // "bus_no_show": "SO202405240088"
  141. // }
  142. // ]
  143. // }
  144. // ]
  145. // }
  146. //}',true);
  147. //请求获取仓位卷码信息
  148. $box_roll_list = $this->getBoxData($dv,$token,$box_list);
  149. // var_dump($box_roll_list);die;
  150. $new_data = [];
  151. foreach ($data as $k=>$box_detail){
  152. foreach ($box_detail['detail'] as $v){
  153. foreach ($box_roll_list as $roll_number=>$roll_detail){
  154. if(in_array($v,$roll_detail['detail'])){
  155. if(!isset($new_data[$roll_detail['box_code']])) $new_data[$roll_detail['box_code']] = [
  156. 'detail' => [],
  157. 'fake_qty' => $data[$roll_detail['box_code']]['fake_qty'],
  158. 'material_code' => $roll_detail['material_code'],
  159. 'roll_number' => $roll_number
  160. ];
  161. $new_data[$roll_detail['box_code']]['detail'][] = $v;
  162. break;
  163. }
  164. }
  165. }
  166. }
  167. //补全数据
  168. $data = $this->setBoxFake($new_data);
  169. // var_dump($data);
  170. //把数据拼回去
  171. list($lind_bind,$lead_out) = $this->setNewData($old_data,$data);
  172. // var_dump(json_encode($lind_bind));
  173. // var_dump(json_encode($lead_out));die;
  174. return [$lind_bind,$lead_out];
  175. }
  176. // 获取指定数量的值,并从原数组中删除这些值
  177. function getValues(&$array, $count) {
  178. // 获取前$count个值
  179. $result = array_slice($array, 0, $count);
  180. // 从原数组中删除这些值
  181. $array = array_slice($array, $count);
  182. return $result;
  183. }
  184. private function setNewData($old_data,$data){
  185. $code_key_data = [];
  186. foreach ($data as $v){
  187. if(!isset($code_key_data[$v['material_code']])) $code_key_data[$v['material_code']] = [];
  188. $code_key_data[$v['material_code']] = array_merge($code_key_data[$v['material_code']],$v['detail']);
  189. }
  190. $lind_bind = $old_data['lead_bind'];
  191. foreach ($lind_bind['material_list'] as &$l){
  192. unset($l['brand_qr_code_list']);
  193. // $l['qty'] = $l['fake_qty'];
  194. $l['brand_qr_code_list'] = $this->getValues( $code_key_data[$l['material_code']],$l['fake_qty']);
  195. unset($l['fake_qty']);
  196. }
  197. $lead_out = $old_data['lead_out']['brand_out_stock_list'];
  198. foreach ($lead_out as &$ll){
  199. unset($ll['brand_out_stock_dtl'][0]['brand_qr_code_list']);
  200. $ll['brand_out_stock_dtl'][0]['qty'] = $ll['brand_out_stock_dtl'][0]['fake_qty'];
  201. unset($ll['brand_out_stock_dtl'][0]['fake_qty']);
  202. // var_dump($data[$ll['send_box_code']]);
  203. $ll['brand_out_stock_dtl'][0]['brand_qr_code_list'] = implode(',',$data[$ll['send_box_code']]['detail']);
  204. }
  205. file_put_contents('1.txt',json_encode(['lind_bind'=>$lind_bind,'lead_out'=>$lead_out]));
  206. // var_dump($lind_bind);
  207. // var_dump($lead_out);die;
  208. return [$lind_bind,['brand_out_stock_list'=>$lead_out]];
  209. //{
  210. // "key": "DV00001",
  211. // "lead_bind": {
  212. // "device_code": "DV00001",
  213. // "material_list": [
  214. // {
  215. // "product_code": "BC020202000999",
  216. // "product_code_show": "\u4fe1\u606f\u90e8\u6d4b\u8bd51800\u9999\u6749\u6728",
  217. // "order_item_id": "581732116677795840",
  218. // "order_item_id_show": "SO202405240088",
  219. // "material_code": "40010101000999",
  220. // "material_code_show": "E0\u6279\u96f6\u514d\u6f06\uff08\u6d4b\u8bd5\u767d\u6807\uff09",
  221. // "process_title": null,
  222. // "process_title_two": null,
  223. // "color": null,
  224. // "fake_qty": 20,
  225. // "color_two": null,
  226. // "brand_qr_code_list": [
  227. // "DN2EYGAT3XJJRGMHJ5CL",
  228. // ],
  229. // "order_no_list": "SO202405240088"
  230. // },
  231. // {
  232. // "product_code": "BC020202000999",
  233. // "product_code_show": "\u4fe1\u606f\u90e8\u6d4b\u8bd51800\u9999\u6749\u6728",
  234. // "order_item_id": "581732116677795840",
  235. // "order_item_id_show": "SO202405240088",
  236. // "material_code": "40010101000999",
  237. // "material_code_show": "E0\u6279\u96f6\u514d\u6f06\uff08\u6d4b\u8bd5\u767d\u6807\uff09",
  238. // "process_title": null,
  239. // "process_title_two": null,
  240. // "color": null,
  241. // "fake_qty": 10,
  242. // "color_two": null,
  243. // "brand_qr_code_list": [
  244. // "13NMKBBM8O8JBRWDJHW3",
  245. // ],
  246. // "order_no_list": "SO202405240088"
  247. // }
  248. // ]
  249. // },
  250. // "lead_out": {
  251. // "brand_out_stock_list": [
  252. // {
  253. // "in_out_type_code": "CK00004",
  254. // "in_out_type_code_show": "\u5546\u6807\u4ed3\u6b63\u5e38\u9886\u6807\u51fa\u5e93",
  255. // "send_box_code": "CK00014",
  256. // "send_box_code_show": "\u5546\u6807\u5341\u4e00\u53f7\u4ed3",
  257. // "device_code": "DV00001",
  258. // "get_brand_source_dk": "SALE_ORDER",
  259. // "brand_out_stock_dtl": [
  260. // {
  261. // "item_no": 10,
  262. // "brand_qr_code_list": "",
  263. // "material_code": "40010101000999",
  264. // "restock_qty": 0,
  265. // "material_code_show": "E0\u6279\u96f6\u514d\u6f06\uff08\u6d4b\u8bd5\u767d\u6807\uff09",
  266. // "unit_code": "ST",
  267. // "unit_code_show": "\u5f20",
  268. // "qty": "24",
  269. // "bus_type_dk": "SALE_ORDER",
  270. // "bus_type_dk_show": "\u9500\u552e\u8ba2\u5355",
  271. // "bus_no": "581732116677795840",
  272. // "bus_no_show": "SO202405240088"
  273. // }
  274. // ]
  275. // },
  276. // {
  277. // "in_out_type_code": "CK00004",
  278. // "in_out_type_code_show": "\u5546\u6807\u4ed3\u6b63\u5e38\u9886\u6807\u51fa\u5e93",
  279. // "send_box_code": "CK00012",
  280. // "send_box_code_show": "\u5546\u6807\u4e5d\u53f7\u4ed3",
  281. // "device_code": "DV00001",
  282. // "get_brand_source_dk": "SALE_ORDER",
  283. // "brand_out_stock_dtl": [
  284. // {
  285. // "item_no": 10,
  286. // "brand_qr_code_list": "13NMKBBM8O8JBRWDJHW3",
  287. // "material_code": "40010101000999",
  288. // "restock_qty": 0,
  289. // "material_code_show": "E0\u6279\u96f6\u514d\u6f06\uff08\u6d4b\u8bd5\u767d\u6807\uff09",
  290. // "unit_code": "ST",
  291. // "unit_code_show": "\u5f20",
  292. // "qty": "5",
  293. // "bus_type_dk": "SALE_ORDER",
  294. // "bus_type_dk_show": "\u9500\u552e\u8ba2\u5355",
  295. // "bus_no": "581732116677795840",
  296. // "bus_no_show": "SO202405240088"
  297. // }
  298. // ]
  299. // }
  300. // ]
  301. // }
  302. //}
  303. }
  304. private function setBoxFake($data){
  305. foreach ($data as $k=>$v){
  306. if($v['fake_qty'] > count($v['detail'])){
  307. $diff = $v['fake_qty'] - count($v['detail']);
  308. var_dump($diff);
  309. $return = $this->fakeData($v['roll_number'],$diff);
  310. $data[$k]['detail'] = array_merge($v['detail'],$return);
  311. }
  312. }
  313. return $data;
  314. }
  315. private function fakeData($number,$diff){
  316. $return = [];
  317. for ($i=0;$i < $diff;$i++){
  318. $this->num++;
  319. $return[] = 'f'.$number.'a'.$this->num.rand(10,99);
  320. }
  321. return $return;
  322. }
  323. private function getBoxData($dv,$token,$box_list){
  324. //为了取商标卷码
  325. $res = $this->post_helper($this->url.'/jbl/api/module-data/device/device/diy/device_code',['device_code'=>$dv],$token);
  326. $res = json_decode($res,true);
  327. $detail = [];
  328. $qr_codes = [];
  329. if(isset($res['data'])){
  330. foreach ($res['data'] as $v){
  331. if(in_array($v['box_code'],$box_list)){
  332. $detail[$v['roll_qr_code']] = $v['box_code'];
  333. $qr_codes[] = $v['roll_qr_code'];
  334. }
  335. }
  336. }else{
  337. return [false,''];
  338. };
  339. //为了取仓对应卷码进行获取
  340. $res = $this->post_helper($this->url.'/jbl/api/module-data/brand_sale_order/brand_sale_order/diy/find_roll_qr_code_list',['roll_qr_code_list'=>$qr_codes],$token);
  341. $res = json_decode($res,true);
  342. $qr_roll_list = [];
  343. if(isset($res['data'])){
  344. foreach ($res['data'] as $v){
  345. if(!empty($v['out_brand_qr_code'])) continue;
  346. if(!isset($qr_roll_list[$v['roll_qr_code']]['material_code'])) {
  347. $qr_roll_list[$v['roll_qr_code']]['material_code'] = $v['material_code'];
  348. $qr_roll_list[$v['roll_qr_code']]['box_code'] = $detail[$v['roll_qr_code']];
  349. }
  350. $qr_roll_list[$v['roll_qr_code']]['detail'][] = $v['brand_qr_code'];
  351. }
  352. }
  353. return $qr_roll_list;
  354. }
  355. public function post_helper($url, $data, $auth)
  356. {
  357. $header = [
  358. 'Content-Type: application/json',
  359. 'Authorization: ' . $auth,
  360. ];
  361. $data = json_encode($data);
  362. // var_dump($url);die;
  363. $ch = curl_init();
  364. curl_setopt($ch, CURLOPT_POST, 1);
  365. curl_setopt($ch, CURLOPT_URL, $url);
  366. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  367. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  368. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  369. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  370. if (!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  371. $r = curl_exec($ch);
  372. curl_close($ch);
  373. return $r;
  374. }
  375. }