Orders.php 19 KB

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