OrderImport.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\OrderImportFromC as OIFCModel;
  4. use app\BaseController;
  5. use Exception;
  6. use think\facade\Db;
  7. use think\facade\Validate;
  8. //C端订单导入及处理类
  9. class OrderImport extends Base
  10. {
  11. //订单导入
  12. public function import()
  13. {
  14. $param = $this->request->only(['token', 'list'], 'post', 'trim');
  15. $val_params = Validate::rule(['token' => 'require', 'list' => 'require|array']);
  16. if (!$val_params->check($param)) throw new \think\Exception($val_params->getError());
  17. Db::startTrans();
  18. try {
  19. $userinfo = GetUserInfo($param['token']);
  20. $createrid = isset($user["data"]['id']) ? $userinfo["data"]['id'] : 0;
  21. $creater = isset($user["data"]['nickname']) ? $userinfo["data"]['nickname'] : "";
  22. $val = Validate::rule([
  23. 'platform_code|平台订单号' => 'require|length:0,255',
  24. 'po_code|其他单号' => 'length:0,255',
  25. 'platform_time|平台下单时间' => 'require|date',
  26. 'sale_source|销售渠道' => 'require|length:0,255',
  27. 'good_code|商品编号' => 'require|length:0,255',
  28. 'price|单价' => 'require|float',
  29. 'num|数量' => 'require|number',
  30. 'before_discount_all_price|优惠前总金额' => 'require|float',
  31. 'discount_price|优惠金额' => 'require|float',
  32. 'after_price|商品优惠后金额' => 'require|float',
  33. 'activity_name|优惠活动名称' => 'require|length:0,255',
  34. 'mode|收费模式' => 'require|length:0,255',
  35. 'order_remark|订单备注' => 'length:0,255',
  36. 'contactor|收货人' => 'require|length:0,255',
  37. 'mobile|联系电话' => 'require|length:0,20',
  38. 'addr|联系地址' => 'require|length:0,255',
  39. ]);
  40. $insert_data = [];
  41. foreach ($param['list'] as $key => $value) {
  42. if (!$val->check($value)) throw new \think\Exception('第' . (string)($key + 1) . '行数据格式有问题,' . $val->getError());
  43. else {
  44. $value['createrid'] = $createrid;
  45. $value['creater'] = $creater;
  46. $insert_data[] = $value;
  47. }
  48. }
  49. $res = Db::name('order_import_from_c')
  50. ->limit(100)
  51. ->insertAll($insert_data);
  52. if ($res) {
  53. Db::commit();
  54. return app_show(0, "导入成功,共" . (string)$res . '条记录');
  55. } else {
  56. Db::rollback();
  57. return error_show(1005, '导入失败');
  58. }
  59. } catch (Exception $exception) {
  60. Db::rollback();
  61. return error_show(1005, $exception->getMessage());
  62. }
  63. }
  64. //查看订单录入列表
  65. public function getImportList()
  66. {
  67. $param = $this->request->only([
  68. 'status' => 0,
  69. 'platform_code' => '',
  70. 'po_code' => '',
  71. 'platform_time_start' => '',
  72. 'platform_time_end' => '',
  73. 'addtime_start' => '',
  74. 'addtime_end' => '',
  75. 'good_code' => '',
  76. 'page' => 1,
  77. 'size' => 15,
  78. ], 'post', 'trim');
  79. $where = [];
  80. if ($param['status']) $where[] = ['c.status', '=', $param['status']];
  81. if ($param['platform_code']) $where[] = ['c.platform_code', 'like', '%' . $param['platform_code'] . '%'];
  82. if ($param['po_code']) $where[] = ['c.po_code', 'like', '%' . $param['po_code'] . '%'];
  83. if ($param['platform_time_start'] && $param['platform_time_end']) $where[] = ['c.platform_time', 'between', [$param['platform_time_start'], $param['platform_time_end']]];
  84. if ($param['addtime_start'] && $param['addtime_end']) $where[] = ['c.addtime', 'between', [$param['addtime_start'], $param['addtime_end']]];
  85. if ($param['good_code']) $where[] = ['c.good_code', 'like', '%' . $param['good_code'] . '%'];
  86. $db = OIFCModel::alias('c')->where($where);
  87. $total = $db->count('id');
  88. $list = $db
  89. ->page($param['page'], $param['size'])
  90. ->order('id', 'desc')
  91. ->select()
  92. ->toArray();
  93. return app_show(0, "获取成功", ['list' => $list, 'count' => $total]);
  94. }
  95. }