| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339 |
- <?php
- namespace app\admin\controller\pickup;
- use app\common\controller\Backend;
- use think\Db;
- use think\Config;
- use app\common\library\Sms as Smslib;
- use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
- use PhpOffice\PhpSpreadsheet\Reader\Xlsx;
- use PhpOffice\PhpSpreadsheet\Reader\Xls;
- use PhpOffice\PhpSpreadsheet\Reader\Csv;
- use think\exception\PDOException;
- use think\exception\ValidateException;
- /**
- * 兑换订单
- *
- * @icon fa fa-circle-o
- */
- class Order extends Backend {
- /**
- * GiftOrder模型对象
- * @var \app\admin\model\GiftOrder
- */
- protected $model = null;
- public function _initialize() {
- parent::_initialize();
- $this->model = new \app\admin\model\pickup\Order;
- $Config = get_addon_config('pickup');
- $kd_company = $Config['kd_company'];
- $this->view->assign("statusList", $this->model->getStatusList());
- $this->view->assign("kd_company", $kd_company);
- }
- /**
- * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
- * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
- * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
- */
- /**
- * 查看
- */
- public function index() {
- //设置过滤方法
- $this->request->filter(['strip_tags']);
- if ($this->request->isAjax()) {
- //如果发送的来源是Selectpage,则转发到Selectpage
- if ($this->request->request('keyField')) {
- return $this->selectpage();
- }
- list($where, $sort, $order, $offset, $limit) = $this->buildparams();
- $total = $this->model
- ->where($where)
- ->order($sort, $order)
- ->count();
- $list = $this->model
- ->where($where)
- ->order($sort, $order)
- ->limit($offset, $limit)
- ->select();
- $list = collection($list)->toArray();
- foreach ($list as $k => &$v) {
- $v['gift_goods_title'] = json_decode($v['gift_goods_title'], true);
- $v['gift_goods_image'] = json_decode($v['gift_goods_image'], true);
- $v['gift_goods_ids'] = json_decode($v['gift_goods_ids'], true);
- }
- $result = array("total" => $total, "rows" => $list);
- return json($result);
- }
- return $this->view->fetch();
- }
- /**
- * 订单发货
- */
- public function delivery($ids = null) {
- $ids = $ids ? $ids : $this->request->param("ids");
- $detail = $this->model->get($ids);
- $goodsList = json_decode($detail['gift_goods'], true);
- if ($this->request->isPost()) {
- $row = $detail;
- $params = $this->request->post("row/a");
- if ($params['kd_company'] && $params['kd_number'] && $row['status'] == 1) {
- $data = [
- "kd_company" => $params['kd_company'],
- "kd_number" => $params['kd_number'],
- "ship_date" => time(),
- "status" => 2,
- ];
- } else {
- $this->error("快递信息不正确,或者该订单已经发货");
- }
- if ($params) {
- $params = $this->preExcludeFields($params);
- $result = false;
- Db::startTrans();
- try {
- //是否采用模型验证
- if ($this->modelValidate) {
- $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
- $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
- $row->validateFailException(true)->validate($validate);
- }
- $result = $row->allowField(true)->save($data);
- Db::commit();
- } catch (ValidateException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (PDOException $e) {
- Db::rollback();
- $this->error($e->getMessage());
- } catch (Exception $e) {
- Db::rollback();
- $this->error($e->getMessage());
- }
- if ($result !== false) {
- $this->success();
- } else {
- $this->error(__('No rows were updated'));
- }
- }
- $this->error(__('Parameter %s can not be empty', ''));
- }
- $this->view->assign("detail", $detail);
- $this->view->assign("goodsList", $goodsList);
- return $this->view->fetch();
- }
- /**
- * 查看物流
- */
- public function transport($ids = null) {
- $ids = $ids ? $ids : $this->request->param("ids");
- $detail = $this->model->get($ids);
- $goodsList = json_decode($detail['gift_goods'], true);
- if ($this->request->isPost()) {
- }
- $Kuaidi = [];
- if ($detail['status'] > 1) {
- $Kuaidi = \addons\pickup\library\Kuaidi100::getKdInfo([
- "com" => $detail['kd_company'],
- "num" => $detail['kd_number'],
- ]);
- }
- $this->view->assign("Kuaidi", $Kuaidi);
- $this->view->assign("detail", $detail);
- $this->view->assign("goodsList", $goodsList);
- return $this->view->fetch();
- }
- /**
- * 订单详情
- */
- public function detail($ids = null) {
- $ids = $ids ? $ids : $this->request->param("ids");
- $detail = $this->model->get($ids);
- $goodsList = json_decode($detail['gift_goods'], true);
- if ($this->request->isPost()) {
- }
- $Kuaidi = [];
- if ($detail['status'] > 1) {
- $Kuaidi = \addons\pickup\library\Kuaidi100::getKdInfo([
- "com" => $detail['kd_company'],
- "num" => $detail['kd_number'],
- ]);
- }
- $this->view->assign("Kuaidi", $Kuaidi);
- $this->view->assign("detail", $detail);
- $this->view->assign("goodsList", $goodsList);
- return $this->view->fetch();
- }
- /**
- * 修改订单状态
- */
- public function setstatus($ids = null) {
- $ids = $ids ? $ids : $this->request->param("itemid");
- $status = $this->request->param("status");
- $detail = $this->model->get($ids);
- if ($detail) {
- $detail->status = $status;
- $res = $detail->save();
- if ($res) {
- $this->success("设置成功");
- } else {
- $this->error(__('设置失败'));
- }
- } else {
- $this->error(__('订单不存在'));
- }
- }
- /**
- * 导入发货单
- */
- public function impDelivery() {
- $file = $this->request->request('file');
- if (!$file) {
- $this->error(__('Parameter %s can not be empty', 'file'));
- }
- $filePath = ROOT_PATH . DS . 'public' . DS . $file;
- if (!is_file($filePath)) {
- $this->error(__('No results were found'));
- }
- //实例化reader
- $ext = pathinfo($filePath, PATHINFO_EXTENSION);
- if (!in_array($ext, ['xls', 'xlsx'])) {
- $this->error(__('Unknown data format'));
- }
- if ($ext === 'xls') {
- $reader = new Xls();
- } else {
- $reader = new Xlsx();
- }
- //导入文件首行类型,默认是注释,如果需要使用字段名称请使用name
- // $importHeadType = isset($this->importHeadType) ? $this->importHeadType : 'comment';
- // $importHeadType = isset($this->importHeadType) ? $this->importHeadType : 'name';
- $importHeadType = "comment";
- $table = $this->model->getQuery()->getTable();
- $database = \think\Config::get('database.database');
- $fieldArr = [];
- $list = db()->query("SELECT COLUMN_NAME,COLUMN_COMMENT FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ? AND TABLE_SCHEMA = ?", [$table, $database]);
- foreach ($list as $k => $v) {
- if ($importHeadType == 'comment') {
- $fieldArr[$v['COLUMN_COMMENT']] = $v['COLUMN_NAME'];
- } else {
- $fieldArr[$v['COLUMN_NAME']] = $v['COLUMN_NAME'];
- }
- }
- //加载文件
- $insert = [];
- try {
- if (!$PHPExcel = $reader->load($filePath)) {
- $this->error(__('Unknown data format'));
- }
- $currentSheet = $PHPExcel->getSheet(0); //读取文件中的第一个工作表
- $allColumn = $currentSheet->getHighestDataColumn(); //取得最大的列号
- $allRow = $currentSheet->getHighestRow(); //取得一共有多少行
- $maxColumnNumber = Coordinate::columnIndexFromString($allColumn);
- $fields = [];
- for ($currentRow = 1; $currentRow <= 1; $currentRow++) {
- for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
- $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
- $fields[] = $val;
- }
- }
- for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
- $values = [];
- for ($currentColumn = 1; $currentColumn <= $maxColumnNumber; $currentColumn++) {
- $val = $currentSheet->getCellByColumnAndRow($currentColumn, $currentRow)->getValue();
- $values[] = is_null($val) ? '' : $val;
- }
- $row = [];
- $temp = array_combine($fields, $values);
- foreach ($temp as $k => $v) {
- if (isset($fieldArr[$k]) && $k !== '') {
- $row[$fieldArr[$k]] = $v;
- }
- }
- if ($row) {
- $insert[] = $row;
- }
- }
- } catch (Exception $exception) {
- $this->error($exception->getMessage());
- }
- if (!$insert) {
- $this->error(__('No rows were updated'));
- }
- try {
- //是否包含admin_id字段
- $has_admin_id = false;
- foreach ($fieldArr as $name => $key) {
- if ($key == 'admin_id') {
- $has_admin_id = true;
- break;
- }
- }
- if ($has_admin_id) {
- $auth = Auth::instance();
- foreach ($insert as &$val) {
- if (!isset($val['admin_id']) || empty($val['admin_id'])) {
- $val['admin_id'] = $auth->isLogin() ? $auth->id : 0;
- }
- }
- }
- $Config = get_addon_config('pickup');
- $kd_company = $Config['kd_company'];
- foreach ($insert as $k => $v) {
- if (!empty($v['id'] && $v['id'] > 0)) {
- $kd_company_code = "";
- foreach ($kd_company as $key => $val) {
- if ($v['kd_company'] == $val) {
- $kd_company_code = $key;
- }
- }
- if ($v['kd_number'] != "") {
- $mod = new \app\admin\model\pickup\Order;
- $mod->save(
- [
- 'status' => 2,
- 'kd_company' => $kd_company_code,
- 'kd_number' => $v['kd_number'],
- "ship_date" => time(),
- ],
- ['id' => $v['id'], 'status' => 1]);
- }
- }
- }
- } catch (PDOException $exception) {
- $msg = $exception->getMessage();
- if (preg_match("/.+Integrity constraint violation: 1062 Duplicate entry '(.+)' for key '(.+)'/is", $msg, $matches)) {
- $msg = "导入失败,包含【{$matches[1]}】的记录已存在";
- }
- $this->error($msg);
- } catch (Exception $e) {
- $this->error($e->getMessage());
- }
- $this->success();
- }
- }
|