Orders.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. <?php namespace App\Http\Controllers\Admin;
  2. use App\Http\Requests\Admin\Orders as Request;
  3. use App\Models\Custom;
  4. use App\Models\CustomAddr;
  5. use App\Models\FilesManager;
  6. use App\Models\Product;
  7. use App\Models\Orders as Model;
  8. use App\Models\OrdersAddr;
  9. use App\Models\OrdersProduct;
  10. use PhpOffice\PhpSpreadsheet\IOFactory;
  11. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  12. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  13. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  14. use PhpOffice\PhpSpreadsheet\Style\Border;
  15. use PhpOffice\PhpSpreadsheet\Style\Color;
  16. use PhpOffice\PhpSpreadsheet\Style\Fill;
  17. /**
  18. * 订单管理
  19. *
  20. * @author 刘相欣
  21. *
  22. */
  23. class Orders extends Auth{
  24. protected function _initialize(){
  25. parent::_initialize();
  26. $this->assign('breadcrumb1','销售管理');
  27. $this->assign('breadcrumb2','订单管理');
  28. }
  29. /**
  30. * 首页列表
  31. *
  32. * */
  33. public function index(Model $Model,OrdersProduct $OrdersProduct,Product $Product,Custom $Custom){
  34. // 接受参数
  35. $code = request('order_code','');
  36. $productCode = request('product_code','');
  37. $phone = request('phone','');
  38. $customCode = request('custom_code','');
  39. $productName = request('product_name','');
  40. $status = request('status',0);
  41. $startTime = request('start_time','');
  42. $endTime = request('end_time','');
  43. // 编码转ID
  44. $id = $code ? $Model->codeToId($code) : 0;
  45. $productId = $productCode ? $Product->codeToId($productCode) : 0;
  46. $uid = $customCode ? $Custom->codeToId($customCode) : 0;
  47. // 查询条件
  48. $map = [];
  49. // 编码ID
  50. if( $id ) $map[] = ['orders_product.order_id','=',$id];
  51. if( $uid ) $map[] = ['custom.uid','=',$uid];
  52. if( $productId ) $map[] = ['orders_product.product_id','=',$productId];
  53. if( $productName ) $map[] = ['orders_product.product_name','=',$productName];
  54. if( $phone ) $map[] = ['orders_addr.contact_phone','=',$phone];
  55. if( $startTime ) $map[] = ['orders_product.insert_time','>=',strtotime($startTime)];
  56. if( $endTime ) $map[] = ['orders_product.insert_time','<=',strtotime($endTime)];
  57. if( $status ) $map[] = ['orders_product.status','=',$status];
  58. // 查询数据
  59. $list = $OrdersProduct->query()
  60. ->join('custom','orders_product.custom_uid','=','custom.uid')
  61. ->join('orders_addr','orders_addr.order_id','=','orders_product.order_id')
  62. ->where($map)
  63. ->orderByDesc('id')
  64. ->select([
  65. 'orders_product.*','custom.username as custom_name',
  66. 'orders_addr.contact_name','orders_addr.contact_shop','orders_addr.contact_phone','orders_addr.contact_province','orders_addr.contact_city','orders_addr.contact_area','orders_addr.contact_addr'
  67. ])
  68. ->paginate(request('limit',config('page_num',10)))->appends(request()->all());
  69. // 循环处理数据
  70. foreach ($list as $key => $value) {
  71. // id转编号
  72. $value['order_code'] = $Model->idToCode($value['order_id']);
  73. $value['custom_code'] = $Custom->idToCode($value['custom_uid']);
  74. $value['state'] = $Model->getState($value['status'],'state');
  75. $value['product_code'] = $Product->idToCode($value['product_id']);
  76. // 重组
  77. $list[$key] = $value;
  78. }
  79. // 分配数据
  80. $this->assign('empty', '<tr><td colspan="20">~~暂无数据</td></tr>');
  81. $this->assign('list', $list);
  82. // 加载模板
  83. return $this->fetch();
  84. }
  85. /**
  86. * 状态
  87. *
  88. * */
  89. public function set_status( Request $request, Model $Model,OrdersProduct $OrdersProduct){
  90. // 验证参数
  91. $request->scene('set_status')->validate();
  92. // 接收参数
  93. $id = request('id',0);
  94. $status = request('status',0);
  95. // 获取产品和数量
  96. $oldData = $Model->query()->find($id);
  97. // 如果用户不存在
  98. if( !$oldData ) return json_send(['code'=>'error','msg'=>'订单不存在']);
  99. // 查询数据
  100. $result = $Model->edit($id,['status'=>$status]);
  101. // 提示新增失败
  102. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  103. // 查询数据
  104. $result = $OrdersProduct->query()->where([['order_id','=',$id]])->update(['status'=>$status,'update_time'=>time()]);
  105. // 提示新增失败
  106. if( !$result ) return json_send(['code'=>'error','msg'=>'设置失败']);
  107. // 记录行为
  108. $this->addAdminHistory(admin('uid'),$Model->getTable(),$id,2,[],['status'=>$status]);
  109. // 告知结果
  110. return json_send(['code'=>'success','msg'=>'设置成功','path'=>'']);
  111. }
  112. /**
  113. * 表格导入
  114. *
  115. * */
  116. public function import_execl( Request $request,Model $Model,Custom $Custom,OrdersAddr $OrdersAddr,OrdersProduct $OrdersProduct, FilesManager $FilesManager,CustomAddr $CustomAddr){
  117. // 验证参数
  118. $request->scene('import_execl')->validate();
  119. // 获取表格信息
  120. $file = request()->file('order_file');
  121. // 返回结果
  122. $sheetList = $FilesManager->excelToOrder($file);
  123. // 如果不存在结果
  124. if( isset($sheetList['error']) ) return json_send(['code'=>'error','msg'=>$sheetList['error']]);
  125. // 订单列表
  126. $orderList = [];
  127. // 当前时间
  128. $time = time();
  129. // 循环表格数据
  130. foreach ($sheetList as $value) {
  131. // 状态更改
  132. $value['status'] = $Model->getWeibanStatus($value['status']);
  133. // 客户手机号
  134. $orderList[$value['weizan_orderid']]['custom'] = ['phone'=>$value['contact_phone'],'username'=>$value['buyer_nick']];
  135. // 组合成订单的收件地址
  136. $orderList[$value['weizan_orderid']]['contact'] = [
  137. 'contact_name'=>$value['contact_name'],
  138. 'contact_phone'=>$value['contact_phone'],
  139. 'contact_province'=>$value['contact_province'],
  140. 'contact_city'=>$value['contact_city'],
  141. 'contact_area'=>$value['contact_area'],
  142. 'contact_addr'=>$value['contact_addr']
  143. ];
  144. // 组合成订单的收件地址
  145. $orderList[$value['weizan_orderid']]['product'][] = [
  146. 'status'=>$value['status'],
  147. 'product_name'=>$value['product_name'],
  148. 'sku_attr_names'=>$value['sku_attr_names'],
  149. 'buy_num'=>$value['buy_num'],
  150. 'pay_total'=>$value['pay_total'],
  151. 'price_total'=>$value['pay_total'],
  152. 'insert_time'=>$value['insert_time'],
  153. 'update_time'=>$time,
  154. ];
  155. // 组合成订单的需要的数据
  156. if( !isset($orderList[$value['weizan_orderid']]['order']) ) $orderList[$value['weizan_orderid']]['order'] = ['weizan_orderid'=>$value['weizan_orderid'],'status'=>$value['status'],'pay_total'=>0,'price_total'=>0,'insert_time'=>$value['insert_time']];
  157. // 价格
  158. $orderList[$value['weizan_orderid']]['order']['pay_total'] = $orderList[$value['weizan_orderid']]['order']['pay_total'] + $value['pay_total'];
  159. $orderList[$value['weizan_orderid']]['order']['price_total'] = $orderList[$value['weizan_orderid']]['order']['pay_total'];
  160. }
  161. // 新增地址
  162. $newAddrList = [];
  163. // 要更新的订单子表
  164. $orderProduct = [];
  165. // 循环订单列表
  166. foreach ($orderList as $value) {
  167. // 获取手机号,查询是否用客户
  168. $custom = $Custom->getOneByPhone($value['custom']['phone']);
  169. // 如果存在手机号
  170. $uid = $custom ? $custom['uid'] : $Custom->add($value['custom']);
  171. // 如果客户存在
  172. if( !$uid ) return json_send(['code'=>'error','msg'=>$value['custom']['username'].'【'.$value['custom']['phone'].'】'.'无法创建用户']);
  173. // 通过订单号查询是否存在系统订单
  174. $orderId = $Model->query()->where([['weizan_orderid','=',$value['order']['weizan_orderid']]])->value('id');
  175. // 客户ID
  176. $value['order']['custom_uid'] = $uid;
  177. // 存在订单获取订单ID,不存在则新增
  178. $orderId = $orderId ? $Model->edit($orderId,$value['order']) : $Model->add($value['order']);
  179. // 如果客户存在
  180. if( !$orderId ) return json_send(['code'=>'error','msg'=>$orderId.'订单写入失败']);
  181. // 联系地址
  182. $contact = $value['contact'];
  183. // 存在详细地址,才创建地址库
  184. if( $contact['contact_name'] && $contact['contact_phone'] && $contact['contact_province'] && $contact['contact_city'] && $contact['contact_area'] && $contact['contact_addr'] ) {
  185. // 收件地址是否存在
  186. $oldAddr = $CustomAddr->query()->where([['custom_uid','=',$uid]])->first();
  187. // 如果不存在地址
  188. if( !$oldAddr ) $CustomAddr->add(['custom_uid'=>$uid,'contact_name'=>$contact['contact_name'],'contact_phone'=>$contact['contact_phone'],'contact_province'=>$contact['contact_province'],'contact_city'=>$contact['contact_city'],'contact_area'=>$contact['contact_area'],'contact_addr'=>$contact['contact_addr']]);
  189. }
  190. // 订单地址库
  191. $addrId = $OrdersAddr->query()->where([['order_id','=',$orderId]])->value('id');
  192. // 订单ID
  193. $value['contact']['order_id'] = $orderId;
  194. // 订单地址ID
  195. $value['contact']['id'] = (int)$addrId;
  196. // 不存在地址的话
  197. $newAddrList[] = $value['contact'];
  198. // 循环子订单
  199. foreach ( $value['product'] as $product ) {
  200. // 数据结果
  201. $product['order_id'] = $orderId;
  202. $product['custom_uid'] = $uid;
  203. $product['id'] = (int) $OrdersProduct->query()->where([['order_id','=',$orderId],'product_name'=>$product['product_name'],'sku_attr_names'=>$product['sku_attr_names']])->value('id');
  204. $orderProduct[] = $product;
  205. }
  206. }
  207. // 新地址写入
  208. $OrdersProduct->query()->upsert($orderProduct,'id',['product_name','sku_attr_names','buy_num','pay_total','price_total','update_time']);
  209. // 新地址写入
  210. $OrdersAddr->query()->upsert($newAddrList,'id',['contact_name','contact_phone','contact_province','contact_city','contact_area','contact_addr']);
  211. // 提示成功
  212. return json_send(['code'=>'success','msg'=>'订单导入成功','path'=>'']);
  213. }
  214. /**
  215. * 导出表格导入
  216. *
  217. * */
  218. public function down_excel(Model $Model,OrdersProduct $OrdersProduct,Product $Product,Custom $Custom){
  219. // 接受参数
  220. $code = request('order_code','');
  221. $productCode = request('product_code','');
  222. $customCode = request('custom_code','');
  223. $productName = request('product_name','');
  224. $status = request('status',0);
  225. $startTime = request('start_time','');
  226. $endTime = request('end_time','');
  227. // 编码转ID
  228. $id = $code ? $Model->codeToId($code) : 0;
  229. $productId = $productCode ? $Product->codeToId($productCode) : 0;
  230. $uid = $customCode ? $Custom->codeToId($customCode) : 0;
  231. // 查询条件
  232. $map = [];
  233. // 编码ID
  234. if( $id ) $map[] = ['orders_product.order_id','=',$id];
  235. if( $uid ) $map[] = ['custom.uid','=',$uid];
  236. if( $productId ) $map[] = ['orders_product.product_id','=',$productId];
  237. if( $productName ) $map[] = ['orders_product.product_name','=',$productName];
  238. if( $startTime ) $map[] = ['orders_product.insert_time','>=',strtotime($startTime)];
  239. if( $endTime ) $map[] = ['orders_product.insert_time','<=',strtotime($endTime)];
  240. if( $status ) $map[] = ['orders_product.status','=',$status];
  241. // 查询数据
  242. $list = $OrdersProduct->query()
  243. ->join('custom','orders_product.custom_uid','=','custom.uid')
  244. ->join('orders_addr','orders_addr.order_id','=','orders_product.order_id')
  245. ->where($map)
  246. ->orderByDesc('orders_product.id')
  247. ->select([
  248. 'orders_product.id as id',
  249. 'orders_product.order_id',
  250. 'orders_product.custom_uid',
  251. 'orders_product.product_name',
  252. 'orders_product.sku_attr_names as product_spec',
  253. 'orders_product.product_thumb',
  254. 'orders_product.buy_num',
  255. 'orders_product.pay_total',
  256. 'orders_product.coupon_total',
  257. 'orders_product.status',
  258. 'orders_product.insert_time',
  259. 'custom.username as custom_name','custom.weiban_extid as weiban_extid',
  260. 'orders_addr.contact_name','orders_addr.contact_shop','orders_addr.contact_phone','orders_addr.contact_province','orders_addr.contact_city','orders_addr.contact_area','orders_addr.contact_addr'
  261. ])->get()->toArray();
  262. // 返回结果
  263. $data = [];
  264. // 循环处理数据
  265. foreach ($list as $value) {
  266. // id转编号
  267. $value['order_id'] = $Model->idToCode($value['order_id']);
  268. $value['status'] = $Model->getState($value['status'],'state');
  269. $value['custom_uid'] = $Custom->idToCode($value['custom_uid']);
  270. $value['product_price'] = $value['buy_num'] ? ($value['pay_total'] / $value['buy_num']) : $value['buy_num'];
  271. // 重组
  272. $data[$value['order_id']]['order_id'] = $value['order_id'];
  273. $data[$value['order_id']]['custom_uid'] = $value['custom_uid'];
  274. $data[$value['order_id']]['custom_name'] = $value['custom_name'];
  275. $data[$value['order_id']]['weiban_extid'] = $value['weiban_extid'];
  276. $data[$value['order_id']]['status'] = $value['status'];
  277. $data[$value['order_id']]['insert_time'] = $value['insert_time'];
  278. // 地址
  279. $data[$value['order_id']]['contact_name'] = $value['contact_name'];
  280. $data[$value['order_id']]['contact_phone'] = $value['contact_phone'];
  281. $data[$value['order_id']]['contact_province'] = $value['contact_province'];
  282. $data[$value['order_id']]['contact_city'] = $value['contact_city'];
  283. $data[$value['order_id']]['contact_area'] = $value['contact_area'];
  284. $data[$value['order_id']]['contact_addr'] = $value['contact_addr'] .($value['contact_shop'] ? '【'.$value['contact_shop'].'】' : '');
  285. // 子订单
  286. $data[$value['order_id']]['product'][] = ['product_name'=>$value['product_name'],'product_spec'=>$value['product_spec'],'product_thumb'=>$value['product_thumb'],'product_price'=>$value['product_price'],'buy_num'=>$value['buy_num'],'pay_total'=>$value['pay_total'],'coupon_total'=>$value['coupon_total']];
  287. }
  288. try {
  289. // 去下载
  290. $this->toDown($data);
  291. } catch (\Throwable $th) {
  292. dd($th);
  293. }
  294. }
  295. /**
  296. * 去下载
  297. */
  298. private function toDown($data){
  299. // 创建新的电子表格对象
  300. $spreadsheet = new Spreadsheet();
  301. // 设置合并单元格的行和列,例如合并A1到B2的单元格
  302. $sheet = $this->setStyle($spreadsheet);
  303. // 从第二行写入
  304. $row = 2;
  305. // 循环写入
  306. foreach ($data as $key => $value) {
  307. // 如果有多个商品
  308. $count = count($value['product']);
  309. // 如果有多个商品
  310. if( $count > 1 ) {
  311. // 合并单元格
  312. $sheet->mergeCells('A'.$row.':'.'A'.($row+$count-1));
  313. $sheet->mergeCells('B'.$row.':'.'B'.($row+$count-1));
  314. $sheet->mergeCells('C'.$row.':'.'C'.($row+$count-1));
  315. $sheet->mergeCells('D'.$row.':'.'D'.($row+$count-1));
  316. $sheet->mergeCells('E'.$row.':'.'E'.($row+$count-1));
  317. $sheet->mergeCells('F'.$row.':'.'F'.($row+$count-1));
  318. $sheet->mergeCells('G'.$row.':'.'G'.($row+$count-1));
  319. $sheet->mergeCells('H'.$row.':'.'H'.($row+$count-1));
  320. $sheet->mergeCells('I'.$row.':'.'I'.($row+$count-1));
  321. $sheet->mergeCells('J'.$row.':'.'J'.($row+$count-1));
  322. $sheet->mergeCells('Q'.$row.':'.'Q'.($row+$count-1));
  323. $sheet->mergeCells('R'.$row.':'.'R'.($row+$count-1));
  324. }
  325. // 单元格内容写入
  326. $sheet->setCellValue('A'.$row, $value['order_id']);
  327. $sheet->setCellValue('B'.$row, $value['custom_uid']);
  328. $sheet->setCellValue('C'.$row, $value['custom_name']);
  329. $sheet->setCellValue('D'.$row, $value['status']);
  330. $sheet->setCellValue('E'.$row, $value['contact_name']);
  331. $sheet->setCellValue('F'.$row, $value['contact_phone']);
  332. $sheet->setCellValue('G'.$row, $value['contact_province']);
  333. $sheet->setCellValue('H'.$row, $value['contact_city']);
  334. $sheet->setCellValue('I'.$row, $value['contact_area']);
  335. $sheet->setCellValue('J'.$row, $value['contact_addr']);
  336. $sheet->setCellValue('Q'.$row, $value['weiban_extid']);
  337. $sheet->setCellValue('R'.$row, date('Y-m-d H:i:s',$value['insert_time']));
  338. // 循环产品
  339. foreach ($value['product'] as $v) {
  340. $sheet->setCellValue('K'.$row, $v['product_name']);
  341. $sheet->setCellValue('L'.$row, $v['product_spec']);
  342. $sheet->setCellValue('M'.$row, $v['product_price']);
  343. $sheet->setCellValue('N'.$row, $v['buy_num']);
  344. $sheet->setCellValue('O'.$row, $v['coupon_total']);
  345. $sheet->setCellValue('P'.$row, $v['pay_total']);
  346. // 函数自增
  347. $row++;
  348. }
  349. }
  350. //
  351. // 创建内容
  352. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  353. header('Pragma: public');
  354. header('Content-type:application/vnd.ms-excel');
  355. header('Content-Disposition: inline;filename=下载订单.xlsx');
  356. // 输出数据流
  357. return $writer->save('php://output');
  358. }
  359. /**
  360. * 设置表格样式
  361. *
  362. */
  363. private function setStyle(Spreadsheet $spreadsheet){
  364. // 选择当前活动的工作表
  365. $sheet = $spreadsheet->getActiveSheet();
  366. // 宽
  367. $sheet->getColumnDimension('A')->setWidth(15);
  368. $sheet->getColumnDimension('B')->setWidth(15);
  369. $sheet->getColumnDimension('C')->setWidth(15);
  370. $sheet->getColumnDimension('D')->setWidth(15);
  371. $sheet->getColumnDimension('E')->setWidth(15);
  372. $sheet->getColumnDimension('F')->setWidth(15);
  373. $sheet->getColumnDimension('G')->setWidth(15);
  374. $sheet->getColumnDimension('H')->setWidth(15);
  375. $sheet->getColumnDimension('I')->setWidth(15);
  376. $sheet->getColumnDimension('J')->setWidth(50);
  377. $sheet->getColumnDimension('K')->setWidth(80);
  378. $sheet->getColumnDimension('L')->setWidth(80);
  379. $sheet->getColumnDimension('M')->setWidth(10);
  380. $sheet->getColumnDimension('N')->setWidth(10);
  381. $sheet->getColumnDimension('O')->setWidth(10);
  382. $sheet->getColumnDimension('P')->setWidth(10);
  383. $sheet->getColumnDimension('Q')->setWidth(50);
  384. $sheet->getColumnDimension('R')->setWidth(20);
  385. // 默认高度
  386. $sheet->getDefaultRowDimension()->setRowHeight(18);
  387. // 加粗第一行
  388. $sheet->getStyle('A:R')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER)->setVertical(Alignment::VERTICAL_CENTER);
  389. $sheet->getStyle('A1:R1')->getFont()->setBold(true);
  390. $sheet->getStyle('A1:R1')->getFill()->setFillType(Fill::FILL_SOLID)->getStartColor()->setARGB('FF00FF00'); // ARGB颜色代码,例如绿色
  391. // 设置表格标题
  392. $sheet
  393. ->setCellValue('A1', '订单ID')
  394. ->setCellValue('B1', '客户ID')
  395. ->setCellValue('C1', '客户昵称')
  396. ->setCellValue('D1', '订单状态')
  397. ->setCellValue('E1', '收货人')
  398. ->setCellValue('F1', '收货人手机号')
  399. ->setCellValue('G1', '省')
  400. ->setCellValue('H1', '市')
  401. ->setCellValue('I1', '区县')
  402. ->setCellValue('J1', '收货地址')
  403. ->setCellValue('K1', '产品名称')
  404. ->setCellValue('L1', '产品规格')
  405. ->setCellValue('M1', '产品价格')
  406. ->setCellValue('N1', '产品数量')
  407. ->setCellValue('O1', '优惠金额')
  408. ->setCellValue('P1', '产品金额')
  409. ->setCellValue('Q1', '微伴ID')
  410. ->setCellValue('R1', '下单时间');
  411. // 返回结果
  412. return $sheet;
  413. }
  414. }