ImportService.php 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. <?php
  2. namespace App\Service;
  3. use App\Exports\TableHeadExport;
  4. use App\Import\Import;
  5. use App\Import\ImportAll;
  6. use App\Model\BasicType;
  7. use App\Model\Customer;
  8. use App\Model\CustomerInfo;
  9. use App\Model\Employee;
  10. use App\Model\Product;
  11. use App\Model\ProductCategory;
  12. use App\Model\ProductPriceDetail;
  13. use App\Model\SalesOrder;
  14. use App\Model\SalesOrderProductInfo;
  15. use Illuminate\Support\Facades\DB;
  16. use Maatwebsite\Excel\Facades\Excel;
  17. use PhpOffice\PhpSpreadsheet\IOFactory;
  18. class ImportService extends Service
  19. {
  20. public static $type = [
  21. 'product', //产品
  22. 'customer', //客户
  23. 'salesOnline', //线上订单
  24. ];
  25. //写活的导入------------------- 暂时不好用
  26. //导入入口
  27. public function import($data,$user){
  28. if(empty($data['type'])) return [false,'缺少导入类型,导入失败'];
  29. if(! in_array($data['type'],self::$type)) return [false,'导入类型不存在,导入失败'];
  30. if(empty($data['file'])) return [false,'导入文件不能为空'];
  31. //导入的数据并且校验写入
  32. list($status,$msg) = $this->importMain($data,$user);
  33. if(! $status) return [false, $msg];
  34. return [true, ''];
  35. }
  36. //主方法
  37. public function importMain($data,$user){
  38. //获取配置文件
  39. $config = "excel." . $data['type'];
  40. $config_array = config($config) ?? [];
  41. if(empty($config_array)) return [false, '配置文件不存在'];
  42. //(特殊 额外的表头数据)
  43. $config_array = $this->getTableTitle($config_array,$user,$data);
  44. //获取合并单元格范围
  45. $uploadedFile = $_FILES['file']['tmp_name']; // 获取上传文件的临时路径
  46. $spreadsheet = IOFactory::load($uploadedFile); // 加载上传的 Excel 文件
  47. $worksheet = $spreadsheet->getActiveSheet(); // 获取第一个工作表
  48. $mergedCells = $worksheet->getMergeCells(); // 获取单元格合并范围
  49. // 需要导入的公用数据
  50. $msg['user_id'] = $user['id'];
  51. $msg['depart_id'] = $this->getDepart($user);
  52. $msg['top_depart_id'] = $user['depart_map'][$msg['depart_id']] ?? 0;
  53. //导入
  54. $import = new Import();
  55. $import->setConfig($config_array, $mergedCells,$msg);
  56. \Maatwebsite\Excel\Facades\Excel::import($import,$data['file']);
  57. //异常提示报错
  58. if($import->getMsg()) return [false, $import->getMsg()];
  59. return [true, ''];
  60. }
  61. //表头入口
  62. public function getTableTitle($config_array,$user,$data){
  63. if(! empty($config_array['dynamics_field'])){
  64. $func = $config_array['dynamics_field']['func'];
  65. return $this->$func($config_array,$user,$data);
  66. }
  67. return $config_array;
  68. }
  69. //产品导入的额外表头
  70. public function productTitle($config_array,$user,$data){
  71. $model = BasicType::TopClear($user,$data);
  72. $result = $model->whereRaw('type = 22 And del_time = 0')->get()->toArray();
  73. if(! empty($result)){
  74. foreach ($result as $value){
  75. $config_array['field'][$value['title']] = [
  76. "key" => $config_array['dynamics_field']['name'],
  77. "key_array" => [
  78. "basic_type_id" => $value['id'],
  79. "price" => 0,
  80. ],
  81. "rule" => "",
  82. "other_rule" => "is_numeric",
  83. "multiple" => true,
  84. "map" => [
  85. $value['title'] => "price",
  86. ],
  87. ];
  88. }
  89. }
  90. return $config_array;
  91. }
  92. //产品导入的额外数据
  93. public function fillInsertProductData($time){
  94. $last_insert_data = Product::where('crt_time',$time)
  95. ->where('del_time',0)
  96. ->select("id","product_category_id")
  97. ->get()->toArray();
  98. if(empty($last_insert_data)) return;
  99. $list = ProductCategory::where('del_time',0)
  100. ->select('id','parent_id')
  101. ->get()->toArray();
  102. foreach ($last_insert_data as $value){
  103. $parentsId = $this->findParentIds($value['product_category_id'], $list);
  104. array_unshift($parentsId, $value['product_category_id']);
  105. $result = array_reverse($parentsId);
  106. Product::where('id',$value['id'])->update([
  107. 'product_category' => json_encode($result)
  108. ]);
  109. }
  110. }
  111. //写活的导入------------------- 暂时不好用
  112. //写死的导入
  113. public function getTableTitleXls($data,$user){
  114. if(empty($data['type'])) return [false,'缺少类型'];
  115. if(! in_array($data['type'],self::$type)) return [false,'类型不存在'];
  116. //获取配置文件
  117. $fuc = $data['type'];
  118. list($status,$msg,$filename) = $this->$fuc($data,$user);
  119. if(!$status) return [false, $msg];
  120. $headers = array_column($msg,'value');
  121. Excel::store(new TableHeadExport([], $headers),"/public/export/{$filename}", null, 'Xlsx', []);
  122. return [true, ['file' => $filename]];
  123. }
  124. private function customer($data,$user){
  125. //生成下载文件
  126. $filename = "客户模板_" . time() . '.' . 'xlsx';
  127. //获取配置文件
  128. $config = "excel.customerTable";
  129. $config_array = config($config) ?? [];
  130. if(empty($config_array)) return [false, '配置文件不存在',''];
  131. return [true, $config_array,$filename];
  132. }
  133. private function product($data,$user){
  134. //获取配置文件
  135. $config = "excel.productTable";
  136. $config_array = config($config) ?? [];
  137. if(empty($config_array)) return [false, '配置文件不存在', ''];
  138. $model = BasicType::TopClear($user,$data);
  139. $result = $model->whereRaw('type = 22 And del_time = 0')->get()->toArray();
  140. if(! empty($result)){
  141. foreach ($result as $value){
  142. $config_array[] = [
  143. 'key' => 'table_id.' . $value['id'],
  144. 'value' => $value['title'],
  145. ];
  146. }
  147. }
  148. //生成下载文件
  149. $filename = "产品模板_" . time() . '.' . 'xlsx';
  150. return [true, $config_array,$filename];
  151. }
  152. private function salesOnline($data,$user){
  153. //生成下载文件
  154. $filename = "线上订单模板_" . time() . '.' . 'xlsx';
  155. //获取配置文件
  156. $config = "excel.salesOnlineTable";
  157. $config_array = config($config) ?? [];
  158. if(empty($config_array)) return [false, '配置文件不存在',''];
  159. return [true, $config_array,$filename];
  160. }
  161. //导入入口
  162. public function importAll($data,$user){
  163. if(empty($data['type'])) return [false,'缺少导入类型,导入失败'];
  164. if(! in_array($data['type'],self::$type)) return [false,'导入类型不存在,导入失败'];
  165. if(empty($data['file'])) return [false,'导入文件不能为空'];
  166. $import = new ImportAll();
  167. //设置导入人id
  168. $import->setCrt($user['id']);
  169. $import->setUser($user);
  170. $import->setType($data['type']);
  171. //导入
  172. \Maatwebsite\Excel\Facades\Excel::import($import,$data['file']);
  173. if($import->getMsg()) return [false, $import->getMsg()];
  174. return [true, ''];
  175. }
  176. public function customerImport($array, $user){
  177. $head = $user['head']['id'] ?? 0;
  178. // 去除表头
  179. unset($array[0]);
  180. if(empty($array)) return [false, '导入数据不能为空'];
  181. //第一次表格数据校验 非空 已经过滤数据
  182. $array_clean = $contact_info = [];
  183. foreach ($array as $key => $value){
  184. $rowData = array_filter($value);
  185. if (empty($rowData)) {
  186. unset($array[$key]);
  187. } elseif(empty($value[0]) || empty($value[1])) {
  188. return [false, '带*号的字段项必填'];
  189. }else{
  190. foreach ($value as $k => $v){
  191. $value[$k] = trim($v);
  192. }
  193. if(! isset(Customer::dk[$value[0]])) return [false, '客户模板填写错误'];
  194. $value[0] = Customer::dk[$value[0]];
  195. if(in_array($value[1],$array_clean)) return [false, '客户名称不能重复'];
  196. $array_clean[] = $value[1];
  197. $array[$key] = $value;
  198. if(! empty($value[13])){
  199. if(in_array($value[13],$contact_info)) return [false, '联系方式内容不能重复'];
  200. $contact_info[] = $value[13];
  201. }
  202. }
  203. }unset($array_clean);
  204. if(empty($array)) return [false, '导入数据不能为空'];
  205. //客户
  206. $model = Customer::Clear($user,[]);
  207. $customer = $model->where('del_time',0)
  208. ->whereIn('title',array_unique(array_column($array,'1')))
  209. ->pluck('id','title')
  210. ->toArray();
  211. $model = BasicType::TopClear($user,[]);
  212. $basic = $model->where('del_time',0)
  213. ->whereIn('type',[1,2,3,4,5,9,10])
  214. ->select('id','title','type')
  215. ->get()
  216. ->toArray();
  217. $basic_list = [];
  218. foreach ($basic as $value){
  219. if($value['type'] == 1){
  220. $basic_list[2][$value['title']] = $value['id'];
  221. }elseif ($value['type'] == 2){
  222. $basic_list[3][$value['title']] = $value['id'];
  223. }elseif ($value['type'] == 3){
  224. $basic_list[4][$value['title']] = $value['id'];
  225. }elseif ($value['type'] == 4){
  226. $basic_list[12][$value['title']] = $value['id'];
  227. }elseif ($value['type'] == 5){
  228. $basic_list[7][$value['title']] = $value['id'];
  229. }elseif ($value['type'] == 9){
  230. $basic_list[8][$value['title']] = $value['id'];
  231. }elseif ($value['type'] == 10){
  232. $basic_list[9][$value['title']] = $value['id'];
  233. }
  234. }
  235. $model = Product::ProductClear($user,[]);
  236. $product = $model->where('del_time',0)
  237. ->whereIn('title',array_unique(array_column($array,'6')))
  238. ->pluck('id','title')
  239. ->toArray();
  240. $emp = Employee::where('del_time',0)
  241. ->whereIn('number',array_unique(array_column($array,'14')))
  242. ->pluck('id','number')
  243. ->toArray();
  244. $top_depart_id = $user['depart_top'][0] ?? [];
  245. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  246. $contact_info_array = CustomerInfo::from('customer_info as a')
  247. ->join('customer as b','b.id','a.customer_id')
  248. ->where('a.del_time',0)
  249. ->where('b.del_time',0)
  250. ->where('b.top_depart_id',$top_depart_id)
  251. ->whereIn('a.contact_info', $contact_info)
  252. ->select('a.contact_info')->get()->toArray();
  253. $contact_info_array = array_column($contact_info_array,'contact_info');
  254. $time = time();
  255. $insert = [];
  256. $insert_detail = $insert_detail2 = [];
  257. foreach ($array as $value){
  258. $tmp = [
  259. 'model_type' => '',
  260. 'title' => '',
  261. 'customer_intention' => '',
  262. 'customer_from' => '',
  263. 'customer_type' => '',
  264. 'car_type' => '',
  265. 'consulting_product' => '',
  266. 'intention_product' => '',
  267. 'progress_stage' => '',
  268. 'state_type' => '',
  269. 'address2' => '',
  270. 'mark' => '',
  271. 'depart_id' => $head,
  272. 'top_depart_id' => $head,
  273. 'crt_id' => $user['id'],
  274. 'crt_time' => $time,
  275. 'upd_time' => $time,
  276. ];
  277. $tmp['model_type'] = $value['0'];
  278. $tmp['consulting_product'] = $value['5'];
  279. $tmp['mark'] = $value['10'];
  280. $tmp['address2'] = $value['11'];
  281. if(! empty($customer[$value['1']])) return [false, '客户:' . $value['1'] . '已存在'];
  282. $tmp['title'] = $value['1'];
  283. if($value['2']){
  284. if(empty($basic_list[2][$value['2']])) return [false, '客户意向度:' . $value['2'] . '不存在'];
  285. $tmp['customer_intention'] = $basic_list[2][$value['2']];
  286. }
  287. if($value['3']){
  288. if(empty($basic_list[3][$value['3']])) return [false, '客户来源:' . $value['3'] . '不存在'];
  289. $tmp['customer_from'] = $basic_list[3][$value['3']];
  290. }
  291. if($value['4']){
  292. if(empty($basic_list[4][$value['4']])) return [false, '客户类别:' . $value['4'] . '不存在'];
  293. $tmp['customer_type'] = $basic_list[4][$value['4']];
  294. }
  295. if($value['6']){
  296. if(empty($product[$value['6']])) return [false, '意向产品:' . $value['6'] . '不存在'];
  297. $tmp['intention_product'] = $product[$value['6']];
  298. }
  299. if($value['7']){
  300. if(empty($basic_list[7][$value['7']])) return [false, '进展阶段:' . $value['7'] . '不存在'];
  301. $tmp['progress_stage'] = $basic_list[7][$value['7']];
  302. }
  303. if($value['8']){
  304. if(empty($basic_list[8][$value['8']])) return [false, '状态:' . $value['8'] . '不存在'];
  305. $tmp['state_type'] = $basic_list[8][$value['8']];
  306. }
  307. if($value['9']){
  308. if(empty($basic_list[9][$value['9']])) return [false, '车型:' . $value['9'] . '不存在'];
  309. $tmp['car_type'] = $basic_list[9][$value['9']];
  310. }
  311. $contact_id = 0;
  312. if($value['12']){
  313. if(empty($basic_list[12][$value['12']])) return [false, '联系方式类型:' . $value['12'] . '不存在'];
  314. $contact_id = $basic_list[12][$value['12']];
  315. }
  316. if($value['13'] && in_array($value['13'],$contact_info_array)) return [false, '联系方式内容:' . $value['13'] . '已存在'];
  317. $man = 0;
  318. if($value['14']){
  319. if(empty($emp[$value['14']])) return [false, '负责人:' . $value['14'] . '不存在'];
  320. $man = $emp[$value['14']];
  321. }
  322. // if($value['12']){
  323. // if(empty($emp[$value['12']])) return [false, '协同人:' . $value['12'] . '不存在'];
  324. // $tmp['emp_two'] = $emp[$value['12']];
  325. // }
  326. $insert[] = $tmp;
  327. $insert_detail[] = [
  328. 'customer_id' => 0,
  329. 'contact_type' => $contact_id,
  330. 'contact_info' => $value['13'],
  331. 'crt_time' => $time,
  332. 'type' => CustomerInfo::type_one
  333. ];
  334. $insert_detail2[] = [
  335. 'customer_id' => 0,
  336. 'data_id' => $man,
  337. 'crt_time' => $time,
  338. 'type' => CustomerInfo::type_two
  339. ];
  340. }
  341. try{
  342. DB::beginTransaction();
  343. if(! empty($insert)) Customer::insert($insert);
  344. //获取上一次所有id
  345. $last_insert_id = Customer::where('crt_time',$time)
  346. ->where('crt_time',$time)
  347. ->where('depart_id',$head)
  348. ->where('top_depart_id',$head)
  349. ->where('crt_id',$user['id'])
  350. ->select('id')->get()->toArray();
  351. $last_insert_id = array_column($last_insert_id,'id');
  352. //组织数据 写入与主表的关联id
  353. $insert_detail_1 = [];
  354. foreach ($insert_detail as $key => $value){
  355. if(empty($value['contact_type']) && empty($value['contact_info'])) continue;
  356. $value['customer_id'] = $last_insert_id[$key];
  357. $insert_detail_1[] = $value;
  358. }unset($insert_detail);
  359. $insert_detail_2 = [];
  360. foreach ($insert_detail2 as $key => $value){
  361. if(empty($value['data_id'])) continue;
  362. $value['customer_id'] = $last_insert_id[$key];
  363. $insert_detail_2[] = $value;
  364. }unset($insert_detail2);
  365. if(! empty($insert_detail_1)) CustomerInfo::insert($insert_detail_1);
  366. if(! empty($insert_detail_2)) CustomerInfo::insert($insert_detail_2);
  367. DB::commit();
  368. }catch (\Exception $e){
  369. DB::rollBack();
  370. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  371. }
  372. return [true, ''];
  373. }
  374. public function productImport($array, $user){
  375. $head = $user['head']['id'] ?? 0;
  376. // 去除表头
  377. $upload = $array[0];
  378. unset($array[0]);
  379. if(empty($array)) return [false, '导入数据不能为空'];
  380. //第一次表格数据校验 非空 已经过滤数据
  381. $array_clean = [];
  382. $search = "";
  383. foreach ($array as $key => $value){
  384. $rowData = array_filter($value);
  385. if (empty($rowData)) {
  386. unset($array[$key]);
  387. } elseif(empty($value[0]) || empty($value[1]) || empty($value[2])) {
  388. return [false, '带*号的字段项必填'];
  389. }else{
  390. foreach ($value as $k => $v){
  391. $value[$k] = trim($v);
  392. }
  393. $t = $value[0] . $value[1];
  394. if(in_array($t,$array_clean)) return [false, '产品名称与编码不能重复'];
  395. $array_clean[] = $t;
  396. $array[$key] = $value;
  397. $search .= "(title = '".$value[0]."' and code ='".$value[1]."') or";
  398. }
  399. }unset($array_clean);
  400. if(empty($array)) return [false, '导入数据不能为空'];
  401. $search = rtrim($search,' or');
  402. $product = Product::whereRaw($search)
  403. ->where('del_time',0)
  404. ->select('code','title')
  405. ->get()->toArray();
  406. $product_array = [];
  407. foreach ($product as $value){
  408. $product_array[] = $value['title'] . $value['code'];
  409. }
  410. //默认进来 自身顶级公司
  411. $top_depart_id = $user['depart_top'][0] ?? [];
  412. $top_depart_id = $top_depart_id['depart_id'] ?? 0;
  413. $category_list = ProductCategory::where('del_time',0)
  414. ->where('top_depart_id',$top_depart_id)
  415. ->get()->toArray();
  416. $model = BasicType::TopClear($user,[]);
  417. $basic = $model->where('del_time',0)
  418. ->where('type',20)
  419. ->pluck('id','title')
  420. ->toArray();
  421. $category = ProductCategory::whereIn('title',array_unique(array_column($array,'2')))
  422. ->where('del_time',0)
  423. ->where('top_depart_id',$top_depart_id)
  424. ->pluck('id','title')
  425. ->toArray();
  426. $category_parent = array_unique(array_column($category,'parent'));
  427. $time = time();
  428. $table_head = $this->product([],$user);
  429. $heads = $table_head[1];
  430. $tmp = array_column($heads,'key');
  431. $tmp = array_fill_keys($tmp, '');
  432. $tmp['product_category'] = '';
  433. $tmp['depart_id'] = $head;
  434. $tmp['top_depart_id'] = $head;
  435. $tmp['crt_id'] = $user['id'];
  436. $tmp['crt_time'] = $time;
  437. $tmp['upd_time'] = $time;
  438. $tmp['crt_id'] = $user['id'];
  439. $upload = array_flip($upload);
  440. $map = [];
  441. foreach ($heads as $value){
  442. if(strpos($value['key'], 'table_id.') !== false && isset($upload[$value['value']])){
  443. $map[$value['key']] = [
  444. 'col' => $upload[$value['value']],
  445. 'name' => $value['value']
  446. ];
  447. }
  448. }
  449. $array = array_values($array);
  450. $insert = $insert2 = [];
  451. foreach ($array as $key => $value){
  452. $pro_str = $value['0'] . $value['1'];
  453. if(in_array($pro_str, $product_array)) return [false, '产品名称、编码:' . $value['0']. '、' . $value['1'] . '已存在'];
  454. $tmp['title'] = $value['0'];
  455. $tmp['code'] = $value['1'];
  456. if(empty($category[$value['2']])) return [false,'产品分类:' . $value['2'] . '不存在'];
  457. $tmp['product_category_id'] = $category[$value['2']];
  458. if(in_array($tmp['product_category_id'],$category_parent)) return [false,'产品分类:' . $value['2'] . '下存在子分类,请将产品建与最底层分类下'];
  459. $parentsId = $this->findParentIds($tmp['product_category_id'], $category_list);
  460. array_unshift($parentsId, $tmp['product_category_id']);
  461. $result = array_reverse($parentsId);
  462. $tmp['product_category'] = json_encode($result);
  463. $tmp['size'] = $value['3'];
  464. if($value['4']){
  465. if(empty($basic[$value['4']])) return [false, '单位:' . $value['4'] . '不存在'];
  466. $tmp['unit'] = $basic[$value['4']];
  467. }
  468. $tmp['bar_code'] = $value['5'];
  469. $tmp['cost'] = $value['6'];
  470. $tmp['retail_price'] = $value['7'];
  471. foreach ($map as $m => $v){
  472. if($value[$v['col']]){
  473. if(! is_numeric($value[$v['col']])) return [false,$v['name'] . ': 请输入数字且最多两位小数'];
  474. $formattedNumber = number_format($value[$v['col']], 2, '.', '');
  475. if($formattedNumber != $value[$v['col']]) return [false,$v['name'] . ': 请输入数字且最多两位小数'];
  476. }
  477. $tmp[$m] = $value[$v['col']];
  478. }
  479. foreach ($tmp as $k => $v){
  480. if(strpos($k, 'table_id.') !== false){
  481. $tmp2 = [];
  482. $k_n = str_replace('table_id.', "", $k);
  483. $tmp2['basic_type_id'] = $k_n;
  484. $tmp2['price'] = $v;
  485. $tmp2['crt_time'] = $time;
  486. $tmp2['upd_time'] = $time;
  487. $insert2[$key][] = $tmp2;
  488. unset($tmp[$k]);
  489. }
  490. }
  491. $insert[] = $tmp;
  492. }
  493. try{
  494. DB::beginTransaction();
  495. if(! empty($insert)) Product::insert($insert);
  496. if(! empty($insert2)){
  497. $insert_detail = [];
  498. $last_insert_id = Product::where('crt_time',$time)
  499. ->select('id')
  500. ->get()->toArray();
  501. $last_insert_id = array_column($last_insert_id,'id');
  502. foreach ($last_insert_id as $key => $value){
  503. if(isset($insert2[$key])) {
  504. foreach ($insert2[$key] as $val){
  505. $val['product_id'] = $value;
  506. $insert_detail[] = $val;
  507. }
  508. }
  509. }
  510. ProductPriceDetail::insert($insert_detail);
  511. }
  512. DB::commit();
  513. }catch (\Exception $e){
  514. DB::rollBack();
  515. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  516. }
  517. return [true, ''];
  518. }
  519. public function salesOnlineImport($array, $user){
  520. $head = $user['head']['id'] ?? 0;
  521. // 去除表头
  522. unset($array[0]);
  523. if(empty($array)) return [false, '导入数据不能为空'];
  524. $model = BasicType::TopClear($user,[]);
  525. $basic_type = $model->where('del_time',0)
  526. ->where('type',24)
  527. ->pluck('id','title')
  528. ->toArray();
  529. $search = "";
  530. foreach ($array as $key => $value){
  531. $rowData = array_filter($value);
  532. if (empty($rowData)) {
  533. unset($array[$key]);
  534. } elseif(empty($value[0]) || empty($value[1]) || empty($value[2]) || empty($value[3]) || empty($value[4]) || empty($value[5]) || empty($value[6])) {
  535. return [false, '带*号的字段项必填'];
  536. }else{
  537. foreach ($value as $k => $v){
  538. $value[$k] = trim($v);
  539. }
  540. if(! isset($basic_type[$value[1]])) return [false, '店铺(平台类型):' . $value[1] .'不存在'];
  541. $value[1] = $basic_type[$value[1]];
  542. if(! isset(SalesOrder::$order_type[$value[6]])) return [false, '产品类型填写错误'];
  543. $array[$key] = $value;
  544. $search .= "(code = '".$value[2]."' and title ='".$value[3]."') or";
  545. }
  546. }
  547. if(empty($array)) return [false, '导入数据不能为空'];
  548. $search = rtrim($search,' or');
  549. $model = Product::ProductClear($user,[]);
  550. $product = $model->whereRaw($search)
  551. ->where('del_time',0)
  552. ->select('title','id','code','cost','retail_price')
  553. ->get()->toArray();
  554. $product_map = [];
  555. foreach ($product as $value){
  556. $product_map[$value['code'] . $value['title']] = [
  557. 'id' => $value['id'],
  558. 'cost' => $value['cost'],
  559. 'retail_price' => $value['retail_price'],
  560. ];
  561. }
  562. $time = time();
  563. $tmp = [
  564. 'model_type' => SalesOrder::Model_type_four,
  565. 'sales_order_type' => 0,
  566. 'order_number' => '',
  567. 'plat_type' => '',
  568. 'plat_order' => '',
  569. 'sign_time' => $time,
  570. 'product_total' => 0,
  571. 'contract_fee' => 0,
  572. 'rate' => 100,
  573. 'depart_id' => $head,
  574. 'top_depart_id' => $head,
  575. 'crt_id' => $user['id'],
  576. 'crt_time' => $time,
  577. 'upd_time' => $time,
  578. ];
  579. $tmp_detail = [
  580. 'sales_order_id' => 0,
  581. 'product_id' => 0,
  582. 'cost' => 0,
  583. 'retail_price' => 0,
  584. 'price' => 0,
  585. 'final_amount' => 0,
  586. 'number' => '',
  587. 'crt_time' => $time,
  588. ];
  589. $insert = $insert_detail = [];
  590. $prefix = SalesOrder::$prefix[salesOrder::Model_type_four];
  591. foreach ($array as $value){
  592. $product_str = $value[2] . $value[3];
  593. if(! isset($product_map[$product_str])) return [false, '产品:' . '[' . $value[2]. ']' . '[' . $value[3]. ']' . '不存在'];
  594. $product_tmp = $product_map[$product_str] ?? [];
  595. $tmp['product_total'] = $tmp['contract_fee'] = 0;
  596. $keys = $value[0] . $value[6];
  597. if(! isset($insert[$keys])){
  598. $tmp['order_number'] = OrderNoService::createSalesOrderNumber($prefix);
  599. $tmp['sales_order_type'] = $value[6];
  600. $tmp['plat_type'] = $value[1];
  601. $tmp['plat_order'] = $value[0];
  602. $tmp['product_total'] += $value[4] * $value[5];
  603. $tmp['contract_fee'] += $value[4] * $value[5];
  604. $insert[$keys] = $tmp;
  605. }else{
  606. $insert[$keys]['product_total'] += $value[4] * $value[5];
  607. $insert[$keys]['contract_fee'] += $value[4] * $value[5];
  608. }
  609. $tmp_detail['product_id'] = $product_tmp['id'];
  610. $tmp_detail['cost'] = $product_tmp['cost'];
  611. $tmp_detail['retail_price'] = $product_tmp['retail_price'];
  612. $tmp_detail['price'] = $value[5];
  613. $tmp_detail['final_amount'] = $value[4] * $value[5];
  614. $tmp_detail['number'] = $value[4];
  615. $insert_detail[$keys][] = $tmp_detail;
  616. }
  617. $insert_detail = array_values($insert_detail);
  618. try{
  619. DB::beginTransaction();
  620. if(! empty($insert)) SalesOrder::insert($insert);
  621. if(! empty($insert_detail)){
  622. $insert2 = [];
  623. $last_insert_id = SalesOrder::where('crt_time',$time)
  624. ->select('id')
  625. ->get()->toArray();
  626. $last_insert_id = array_column($last_insert_id,'id');
  627. foreach ($last_insert_id as $key => $value){
  628. if(isset($insert_detail[$key])) {
  629. foreach ($insert_detail[$key] as $val){
  630. $val['sales_order_id'] = $value;
  631. $insert2[] = $val;
  632. }
  633. }
  634. }
  635. SalesOrderProductInfo::insert($insert2);
  636. }
  637. DB::commit();
  638. }catch (\Exception $e){
  639. DB::rollBack();
  640. return [false, $e->getMessage() . $e->getLine() . $e->getCode()];
  641. }
  642. return [true, ''];
  643. }
  644. }