Order.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php
  2. namespace app\admin\controller\pickup;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. use think\Config;
  6. use app\common\library\Sms as Smslib;
  7. use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
  8. use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
  9. use PhpOffice\PhpSpreadsheet\Reader\Xls;
  10. use PhpOffice\PhpSpreadsheet\Reader\Csv;
  11. use think\exception\PDOException;
  12. use think\exception\ValidateException;
  13. /**
  14. * 兑换订单
  15. *
  16. * @icon fa fa-circle-o
  17. */
  18. class Order extends Backend {
  19. /**
  20. * GiftOrder模型对象
  21. * @var \app\admin\model\GiftOrder
  22. */
  23. protected $model = null;
  24. public function _initialize() {
  25. parent::_initialize();
  26. $this->model = new \app\admin\model\pickup\Order;
  27. $Config = get_addon_config('pickup');
  28. $kd_company = $Config['kd_company'];
  29. $this->view->assign("statusList", $this->model->getStatusList());
  30. $this->view->assign("kd_company", $kd_company);
  31. }
  32. /**
  33. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  34. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  35. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  36. */
  37. /**
  38. * 查看
  39. */
  40. public function index() {
  41. //设置过滤方法
  42. $this->request->filter(['strip_tags']);
  43. if ($this->request->isAjax()) {
  44. //如果发送的来源是Selectpage,则转发到Selectpage
  45. if ($this->request->request('keyField')) {
  46. return $this->selectpage();
  47. }
  48. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  49. $total = $this->model
  50. ->where($where)
  51. ->order($sort, $order)
  52. ->count();
  53. $list = $this->model
  54. ->where($where)
  55. ->order($sort, $order)
  56. ->limit($offset, $limit)
  57. ->select();
  58. $list = collection($list)->toArray();
  59. foreach ($list as $k => &$v) {
  60. $v['gift_goods_title'] = json_decode($v['gift_goods_title'], true);
  61. $v['gift_goods_image'] = json_decode($v['gift_goods_image'], true);
  62. $v['gift_goods_ids'] = json_decode($v['gift_goods_ids'], true);
  63. }
  64. $result = array("total" => $total, "rows" => $list);
  65. return json($result);
  66. }
  67. return $this->view->fetch();
  68. }
  69. /**
  70. * 订单发货
  71. */
  72. public function delivery($ids = null) {
  73. $ids = $ids ? $ids : $this->request->param("ids");
  74. $detail = $this->model->get($ids);
  75. $goodsList = json_decode($detail['gift_goods'], true);
  76. if ($this->request->isPost()) {
  77. $row = $detail;
  78. $params = $this->request->post("row/a");
  79. if ($params['kd_company'] && $params['kd_number'] && $row['status'] == 1) {
  80. $data = [
  81. "kd_company" => $params['kd_company'],
  82. "kd_number" => $params['kd_number'],
  83. "ship_date" => time(),
  84. "status" => 2,
  85. ];
  86. } else {
  87. $this->error("快递信息不正确,或者该订单已经发货");
  88. }
  89. if ($params) {
  90. $params = $this->preExcludeFields($params);
  91. $result = false;
  92. Db::startTrans();
  93. try {
  94. //是否采用模型验证
  95. if ($this->modelValidate) {
  96. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  97. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  98. $row->validateFailException(true)->validate($validate);
  99. }
  100. $result = $row->allowField(true)->save($data);
  101. Db::commit();
  102. } catch (ValidateException $e) {
  103. Db::rollback();
  104. $this->error($e->getMessage());
  105. } catch (PDOException $e) {
  106. Db::rollback();
  107. $this->error($e->getMessage());
  108. } catch (Exception $e) {
  109. Db::rollback();
  110. $this->error($e->getMessage());
  111. }
  112. if ($result !== false) {
  113. $this->success();
  114. } else {
  115. $this->error(__('No rows were updated'));
  116. }
  117. }
  118. $this->error(__('Parameter %s can not be empty', ''));
  119. }
  120. $this->view->assign("detail", $detail);
  121. $this->view->assign("goodsList", $goodsList);
  122. return $this->view->fetch();
  123. }
  124. /**
  125. * 查看物流
  126. */
  127. public function transport($ids = null) {
  128. $ids = $ids ? $ids : $this->request->param("ids");
  129. $detail = $this->model->get($ids);
  130. $goodsList = json_decode($detail['gift_goods'], true);
  131. if ($this->request->isPost()) {
  132. }
  133. $Kuaidi = [];
  134. if ($detail['status'] > 1) {
  135. $Kuaidi = \addons\pickup\library\Kuaidi100::getKdInfo([
  136. "com" => $detail['kd_company'],
  137. "num" => $detail['kd_number'],
  138. ]);
  139. }
  140. $this->view->assign("Kuaidi", $Kuaidi);
  141. $this->view->assign("detail", $detail);
  142. $this->view->assign("goodsList", $goodsList);
  143. return $this->view->fetch();
  144. }
  145. /**
  146. * 订单详情
  147. */
  148. public function detail($ids = null) {
  149. $ids = $ids ? $ids : $this->request->param("ids");
  150. $detail = $this->model->get($ids);
  151. $goodsList = json_decode($detail['gift_goods'], true);
  152. if ($this->request->isPost()) {
  153. }
  154. $Kuaidi = [];
  155. if ($detail['status'] > 1) {
  156. $Kuaidi = \addons\pickup\library\Kuaidi100::getKdInfo([
  157. "com" => $detail['kd_company'],
  158. "num" => $detail['kd_number'],
  159. ]);
  160. }
  161. $this->view->assign("Kuaidi", $Kuaidi);
  162. $this->view->assign("detail", $detail);
  163. $this->view->assign("goodsList", $goodsList);
  164. return $this->view->fetch();
  165. }
  166. /**
  167. * 修改订单状态
  168. */
  169. public function setstatus($ids = null) {
  170. $ids = $ids ? $ids : $this->request->param("itemid");
  171. $status = $this->request->param("status");
  172. $detail = $this->model->get($ids);
  173. if ($detail) {
  174. $detail->status = $status;
  175. $res = $detail->save();
  176. if ($res) {
  177. $this->success("设置成功");
  178. } else {
  179. $this->error(__('设置失败'));
  180. }
  181. } else {
  182. $this->error(__('订单不存在'));
  183. }
  184. }
  185. /**
  186. * 导入发货单
  187. */
  188. public function impDelivery() {
  189. $file = $this->request->request('file');
  190. if (!$file) {
  191. $this->error(__('Parameter %s can not be empty', 'file'));
  192. }
  193. $filePath = ROOT_PATH . DS . 'public' . DS . $file;
  194. if (!is_file($filePath)) {
  195. $this->error(__('No results were found'));
  196. }
  197. //实例化reader
  198. $ext = pathinfo($filePath, PATHINFO_EXTENSION);
  199. if (!in_array($ext, ['xls', 'xlsx'])) {
  200. $this->error(__('Unknown data format'));
  201. }
  202. if ($ext === 'xls') {
  203. $reader = new Xls();
  204. } else {
  205. $reader = new Xlsx();
  206. }
  207. //导入文件首行类型,默认是注释,如果需要使用字段名称请使用name
  208. // $importHeadType = isset($this->importHeadType) ? $this->importHeadType : 'comment';
  209. // $importHeadType = isset($this->importHeadType) ? $this->importHeadType : 'name';
  210. $importHeadType = "comment";
  211. $table = $this->model->getQuery()->getTable();
  212. $database = \think\Config::get('database.database');
  213. $fieldArr = [];
  214. $list = db()->query("SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database]);
  215. foreach ($list as $k => $v) {
  216. if ($importHeadType == 'comment') {
  217. $fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME'];
  218. } else {
  219. $fieldArr[$v['COLUMN_NAME']] = $v['COLUMN_NAME'];
  220. }
  221. }
  222. //加载文件
  223. $insert = [];
  224. try {
  225. if (!$PHPExcel = $reader->load($filePath)) {
  226. $this->error(__('Unknown data format'));
  227. }
  228. $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
  229. $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
  230. $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
  231. $maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
  232. $fields = [];
  233. for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
  234. for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
  235. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  236. $fields[] = $val;
  237. }
  238. }
  239. for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
  240. $values = [];
  241. for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
  242. $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
  243. $values[] = is_null($val) ? '' : $val;
  244. }
  245. $row = [];
  246. $temp = array_combine($fields, $values);
  247. foreach ($temp as $k => $v) {
  248. if (isset($fieldArr[$k]) && $k !== '') {
  249. $row[$fieldArr[$k]] = $v;
  250. }
  251. }
  252. if ($row) {
  253. $insert[] = $row;
  254. }
  255. }
  256. } catch (Exception $exception) {
  257. $this->error($exception->getMessage());
  258. }
  259. if (!$insert) {
  260. $this->error(__('No rows were updated'));
  261. }
  262. try {
  263. //是否包含admin_id字段
  264. $has_admin_id = false;
  265. foreach ($fieldArr as $name => $key) {
  266. if ($key == 'admin_id') {
  267. $has_admin_id = true;
  268. break;
  269. }
  270. }
  271. if ($has_admin_id) {
  272. $auth = Auth::instance();
  273. foreach ($insert as &$val) {
  274. if (!isset($val['admin_id']) || empty($val['admin_id'])) {
  275. $val['admin_id'] = $auth->isLogin() ? $auth->id : 0;
  276. }
  277. }
  278. }
  279. $Config = get_addon_config('pickup');
  280. $kd_company = $Config['kd_company'];
  281. foreach ($insert as $k => $v) {
  282. if (!empty($v['id'] && $v['id'] > 0)) {
  283. $kd_company_code = "";
  284. foreach ($kd_company as $key => $val) {
  285. if ($v['kd_company'] == $val) {
  286. $kd_company_code = $key;
  287. }
  288. }
  289. if ($v['kd_number'] != "") {
  290. $mod = new \app\admin\model\pickup\Order;
  291. $mod->save(
  292. [
  293. 'status' => 2,
  294. 'kd_company' => $kd_company_code,
  295. 'kd_number' => $v['kd_number'],
  296. "ship_date" => time(),
  297. ],
  298. ['id' => $v['id'], 'status' => 1]);
  299. }
  300. }
  301. }
  302. } catch (PDOException $exception) {
  303. $msg = $exception->getMessage();
  304. if (preg_match("/.+Integrity constraint violation: 1062 Duplicate entry '(.+)' for key '(.+)'/is", $msg, $matches)) {
  305. $msg = "导入失败,包含【{$matches[1]}】的记录已存在";
  306. }
  307. $this->error($msg);
  308. } catch (Exception $e) {
  309. $this->error($e->getMessage());
  310. }
  311. $this->success();
  312. }
  313. }