Orders.php 16 KB

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