Orders.php 17 KB

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