DwyService.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. // $token = 'Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiI1NjYxNTc5MjMwMDUzNzAzNjgiLCJhdXRoIjoiUk9MRV9CUkFORF9TVVBQTElFUixST0xFX1VTRV9CUkFORF9TVVBQTElFUixST0xFX0lOTkVSX1VTRVIsUk9MRV9JTlRFUkZBQ0UsUk9MRV9TRU5EX0JSQU5EX1NVUFBMSUVSIiwidG9rZW5JZCI6IjQiLCJleHAiOjE3MTk2NzI1OTN9.VKDLhTILWHe6MIAlzNvxCk4pBQaV-Et1UFYb12xTxl-QcVWpYaWLZnk6_QJ2bDXMplp75DuIKADmjuHDjDFSWQ';
  23. // $dv = 'DV00001';
  24. // $box_list = [
  25. // 'CK00007',
  26. // 'CK00004',
  27. // ];
  28. //
  29. // $data = [
  30. // 'CK00007'=>[
  31. // 'fake_qty' => '6',
  32. // 'detail' => ['K45SGTLA5HMM7YL1SPMM','R0FWCC4OG0BJFGY5D6BW','7I1V5NXUB8HO68TQT6F3','8EDJPMJQ3K0RCQ0DW45R'],
  33. // ],
  34. // 'CK00004'=>[
  35. // 'fake_qty' => '7',
  36. // 'detail' => ['WO17UD8SJ5CMF8X4RCNF','IKUOBRFEDM2QI2KQ15TI','GB0C22CDHO4WMWCVO5GL','OHYL6X2RYCBQ2M6FYTDI','8IVEAK4UXO6HY9YN4MTI','B9MCUXN8CB25750R1U41'],
  37. // ]
  38. // ];
  39. //请求获取仓位卷码信息
  40. $box_roll_list = $this->getBoxData($dv,$token,$box_list);
  41. $new_data = [];
  42. foreach ($data as $k=>$box_detail){
  43. foreach ($box_detail['detail'] as $v){
  44. foreach ($box_roll_list as $roll_number=>$roll_detail){
  45. if(in_array($v,$roll_detail['detail'])){
  46. if(!isset($new_data[$roll_detail['box_code']])) $new_data[$roll_detail['box_code']] = [
  47. 'detail' => [],
  48. 'fake_qty' => $data[$roll_detail['box_code']]['fake_qty'],
  49. 'material_code' => $roll_detail['material_code'],
  50. 'roll_number' => $roll_number
  51. ];
  52. $new_data[$roll_detail['box_code']]['detail'][] = $v;
  53. break;
  54. }
  55. }
  56. }
  57. }
  58. // var_dump($new_data);die;
  59. //补全数据
  60. $data = $this->setBoxFake($new_data);
  61. //把数据拼回去
  62. list($lind_bind,$lead_out) = $this->setNewData($old_data,$data);
  63. return [$lind_bind,$lead_out];
  64. }
  65. // 获取指定数量的值,并从原数组中删除这些值
  66. function getValues(&$array, $count) {
  67. // 获取前$count个值
  68. $result = array_slice($array, 0, $count);
  69. // 从原数组中删除这些值
  70. $array = array_slice($array, $count);
  71. return $result;
  72. }
  73. private function setNewData($old_data,$data){
  74. $code_key_data = [];
  75. foreach ($data as $v){
  76. if(!isset($code_key_data[$v['material_code']])) $code_key_data[$v['material_code']] = [];
  77. $code_key_data[$v['material_code']] = array_merge($code_key_data[$v['material_code']],$v['detail']);
  78. }
  79. $lind_bind = $old_data['lead_bind'];
  80. foreach ($lind_bind['material_list'] as &$l){
  81. unset($l['brand_qr_code_list']);
  82. $l['brand_qr_code_list'] = $this->getValues( $code_key_data[$l['material_code']],$l['fake_qty']);
  83. }
  84. $lead_out = $old_data['brand_out_stock_list'];
  85. foreach ($lead_out as &$ll){
  86. unset($ll['brand_qr_code_list']);
  87. $ll['brand_qr_code_list'] = $data[$ll['send_box_code']];
  88. }
  89. return [$lind_bind,$lead_out];
  90. //{
  91. // "key": "DV00001",
  92. // "lead_bind": {
  93. // "device_code": "DV00001",
  94. // "material_list": [
  95. // {
  96. // "product_code": "BC020202000999",
  97. // "product_code_show": "\u4fe1\u606f\u90e8\u6d4b\u8bd51800\u9999\u6749\u6728",
  98. // "order_item_id": "581732116677795840",
  99. // "order_item_id_show": "SO202405240088",
  100. // "material_code": "40010101000999",
  101. // "material_code_show": "E0\u6279\u96f6\u514d\u6f06\uff08\u6d4b\u8bd5\u767d\u6807\uff09",
  102. // "process_title": null,
  103. // "process_title_two": null,
  104. // "color": null,
  105. // "fake_qty": 20,
  106. // "color_two": null,
  107. // "brand_qr_code_list": [
  108. // "DN2EYGAT3XJJRGMHJ5CL",
  109. // ],
  110. // "order_no_list": "SO202405240088"
  111. // },
  112. // {
  113. // "product_code": "BC020202000999",
  114. // "product_code_show": "\u4fe1\u606f\u90e8\u6d4b\u8bd51800\u9999\u6749\u6728",
  115. // "order_item_id": "581732116677795840",
  116. // "order_item_id_show": "SO202405240088",
  117. // "material_code": "40010101000999",
  118. // "material_code_show": "E0\u6279\u96f6\u514d\u6f06\uff08\u6d4b\u8bd5\u767d\u6807\uff09",
  119. // "process_title": null,
  120. // "process_title_two": null,
  121. // "color": null,
  122. // "fake_qty": 10,
  123. // "color_two": null,
  124. // "brand_qr_code_list": [
  125. // "13NMKBBM8O8JBRWDJHW3",
  126. // ],
  127. // "order_no_list": "SO202405240088"
  128. // }
  129. // ]
  130. // },
  131. // "lead_out": {
  132. // "brand_out_stock_list": [
  133. // {
  134. // "in_out_type_code": "CK00004",
  135. // "in_out_type_code_show": "\u5546\u6807\u4ed3\u6b63\u5e38\u9886\u6807\u51fa\u5e93",
  136. // "send_box_code": "CK00014",
  137. // "send_box_code_show": "\u5546\u6807\u5341\u4e00\u53f7\u4ed3",
  138. // "device_code": "DV00001",
  139. // "get_brand_source_dk": "SALE_ORDER",
  140. // "brand_out_stock_dtl": [
  141. // {
  142. // "item_no": 10,
  143. // "brand_qr_code_list": "",
  144. // "material_code": "40010101000999",
  145. // "restock_qty": 0,
  146. // "material_code_show": "E0\u6279\u96f6\u514d\u6f06\uff08\u6d4b\u8bd5\u767d\u6807\uff09",
  147. // "unit_code": "ST",
  148. // "unit_code_show": "\u5f20",
  149. // "qty": "24",
  150. // "bus_type_dk": "SALE_ORDER",
  151. // "bus_type_dk_show": "\u9500\u552e\u8ba2\u5355",
  152. // "bus_no": "581732116677795840",
  153. // "bus_no_show": "SO202405240088"
  154. // }
  155. // ]
  156. // },
  157. // {
  158. // "in_out_type_code": "CK00004",
  159. // "in_out_type_code_show": "\u5546\u6807\u4ed3\u6b63\u5e38\u9886\u6807\u51fa\u5e93",
  160. // "send_box_code": "CK00012",
  161. // "send_box_code_show": "\u5546\u6807\u4e5d\u53f7\u4ed3",
  162. // "device_code": "DV00001",
  163. // "get_brand_source_dk": "SALE_ORDER",
  164. // "brand_out_stock_dtl": [
  165. // {
  166. // "item_no": 10,
  167. // "brand_qr_code_list": "13NMKBBM8O8JBRWDJHW3",
  168. // "material_code": "40010101000999",
  169. // "restock_qty": 0,
  170. // "material_code_show": "E0\u6279\u96f6\u514d\u6f06\uff08\u6d4b\u8bd5\u767d\u6807\uff09",
  171. // "unit_code": "ST",
  172. // "unit_code_show": "\u5f20",
  173. // "qty": "5",
  174. // "bus_type_dk": "SALE_ORDER",
  175. // "bus_type_dk_show": "\u9500\u552e\u8ba2\u5355",
  176. // "bus_no": "581732116677795840",
  177. // "bus_no_show": "SO202405240088"
  178. // }
  179. // ]
  180. // }
  181. // ]
  182. // }
  183. //}
  184. }
  185. private function setBoxFake($data){
  186. foreach ($data as $k=>$v){
  187. if($v['fake_qty'] > count($v['detail'])){
  188. $diff = $v['fake_qty'] - count($v['detail']);
  189. var_dump($diff);
  190. $return = $this->fakeData($v['roll_number'],$diff);
  191. $data[$k]['detail'] = array_merge($v['detail'],$return);
  192. }
  193. }
  194. return $data;
  195. }
  196. private function fakeData($number,$diff){
  197. $return = [];
  198. for ($i=0;$i < $diff;$i++){
  199. $this->num++;
  200. $return[] = 'f'.$number.'a'.$this->num.rand(10,99);
  201. }
  202. return $return;
  203. }
  204. private function getBoxData($dv,$token,$box_list){
  205. //为了取商标卷码
  206. $res = $this->post_helper($this->url.'/jbl/api/module-data/device/device/diy/device_code',['device_code'=>$dv],$token);
  207. $res = json_decode($res,true);
  208. $detail = [];
  209. $qr_codes = [];
  210. if(isset($res['data'])){
  211. foreach ($res['data'] as $v){
  212. if(in_array($v['box_code'],$box_list)){
  213. $detail[$v['roll_qr_code']] = $v['box_code'];
  214. $qr_codes[] = $v['roll_qr_code'];
  215. }
  216. }
  217. }else{
  218. return [false,''];
  219. }
  220. //为了取仓对应卷码进行获取
  221. $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);
  222. $res = json_decode($res,true);
  223. $qr_roll_list = [];
  224. if(isset($res['data'])){
  225. foreach ($res['data'] as $v){
  226. if(!isset($qr_roll_list[$v['roll_qr_code']]['material_code'])) {
  227. $qr_roll_list[$v['roll_qr_code']]['material_code'] = $v['material_code'];
  228. $qr_roll_list[$v['roll_qr_code']]['box_code'] = $detail[$v['roll_qr_code']];
  229. }
  230. $qr_roll_list[$v['roll_qr_code']]['detail'][] = $v['brand_qr_code'];
  231. }
  232. }
  233. return $qr_roll_list;
  234. }
  235. public function post_helper($url, $data, $auth)
  236. {
  237. $header = [
  238. 'Content-Type: application/json',
  239. 'Authorization: ' . $auth,
  240. ];
  241. $data = json_encode($data);
  242. // var_dump($url);die;
  243. $ch = curl_init();
  244. curl_setopt($ch, CURLOPT_POST, 1);
  245. curl_setopt($ch, CURLOPT_URL, $url);
  246. curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
  247. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  248. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  249. curl_setopt($ch, CURLOPT_TIMEOUT, 30);
  250. if (!is_null($data)) curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
  251. $r = curl_exec($ch);
  252. curl_close($ch);
  253. return $r;
  254. }
  255. }