Order.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. namespace app\admin\controller\pig;
  3. use app\common\controller\Backend;
  4. use think\Db;
  5. /**
  6. * 订单
  7. *
  8. * @icon fa fa-circle-o
  9. */
  10. class Order extends Backend
  11. {
  12. /**
  13. * Order模型对象
  14. * @var \app\admin\model\pig\Order
  15. */
  16. protected $model = null;
  17. protected $dataLimit = 'auth';
  18. protected $searchFields = ['orderid'];
  19. public function _initialize()
  20. {
  21. parent::_initialize();
  22. $this->model = new \app\admin\model\pig\Order;
  23. $this->view->assign("statusList", $this->model->getStatusList());
  24. }
  25. /**
  26. * 默认生成的控制器所继承的父类中有index/add/edit/del/multi五个基础方法、destroy/restore/recyclebin三个回收站方法
  27. * 因此在当前控制器中可不用编写增删改查的代码,除非需要自己控制这部分逻辑
  28. * 需要将application/admin/library/traits/Backend.php中对应的方法复制到当前控制器,然后进行修改
  29. */
  30. /**
  31. * 查看
  32. */
  33. public function index()
  34. {
  35. //当前是否为关联查询
  36. $this->relationSearch = true;
  37. //设置过滤方法
  38. $this->request->filter(['strip_tags', 'trim']);
  39. if ($this->request->isAjax()) {
  40. //如果发送的来源是Selectpage,则转发到Selectpage
  41. if ($this->request->request('keyField')) {
  42. return $this->selectpage();
  43. }
  44. list($where, $sort, $order, $offset, $limit) = $this->buildparams();
  45. $list = $this->model
  46. ->with(['province'])
  47. ->where($where)
  48. ->order($sort, $order)
  49. ->paginate($limit);
  50. foreach ($list as $row) {
  51. }
  52. $result = array("total" => $list->total(), "rows" => $list->items());
  53. return json($result);
  54. }
  55. return $this->view->fetch();
  56. }
  57. /**
  58. * 添加
  59. */
  60. public function add()
  61. {
  62. if ($this->request->isPost()) {
  63. $params = $this->request->post("row/a");
  64. if ($params) {
  65. $params = $this->preExcludeFields($params);
  66. if ($params['num']<=0) {
  67. $this->error('生成码数量需要大于0!');
  68. }
  69. if ($this->dataLimit && $this->dataLimitFieldAutoFill) {
  70. $params[$this->dataLimitField] = $this->auth->id;
  71. }
  72. $result = false;
  73. Db::startTrans();
  74. try {
  75. //是否采用模型验证
  76. if ($this->modelValidate) {
  77. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  78. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.add' : $name) : $this->modelValidate;
  79. $this->model->validateFailException(true)->validate($validate);
  80. }
  81. //pengchanglu
  82. $params['orderid'] = 's'. date("YmdHis") . mt_rand(100, 999);
  83. //绑定码
  84. $list = Db::query('SELECT id FROM `pi_p_code` WHERE `admin_id` = '.$this->auth->id.' AND `orderid` = \'\' ORDER BY `id` ASC LIMIT 0,'.$params['num'].'; ');
  85. //$list = Db::query('SELECT id FROM `pi_p_code` WHERE `admin_id` = 4 AND `orderid` = \'\' ORDER BY `id` ASC LIMIT 0,'.$params['num'].'; ');
  86. if (!$list) {
  87. $this->error('溯源码为空,请先购买!');
  88. }
  89. $str = $list[0]['id'] . '-' ;//码段
  90. $step = 0;
  91. $sql = 'UPDATE `pi_p_code` SET `orderid` = \''.$params['orderid'].'\' WHERE `id` IN (' ;
  92. foreach ($list as $k => $v) {
  93. $sql .= $v['id'].',';
  94. if ($k>0) {
  95. if ($list[$k-1]['id']+1 != $list[$k]['id']) {
  96. if ($step < 1) {
  97. $str = rtrim($str, '-');
  98. $str .= ','.$list[$k]['id'].'-' ;//码段
  99. } else {
  100. $str .= $list[$k-1]['id'].','.$list[$k]['id'].'-' ;//码段
  101. }
  102. $step = 0;
  103. } else {
  104. $step ++;
  105. }
  106. }
  107. }
  108. $sql = rtrim($sql, ',');
  109. $sql .= ');';
  110. Db::execute($sql);
  111. if ($step == 0) {
  112. $params['code'] = rtrim($str, '-');
  113. } else {
  114. $params['code'] = $str.$list[count($list)-1]['id'];
  115. }
  116. $result = $this->model->allowField(true)->save($params);
  117. Db::commit();
  118. } catch (ValidateException $e) {
  119. Db::rollback();
  120. $this->error($e->getMessage());
  121. } catch (PDOException $e) {
  122. Db::rollback();
  123. $this->error($e->getMessage());
  124. } catch (Exception $e) {
  125. Db::rollback();
  126. $this->error($e->getMessage());
  127. }
  128. if ($result !== false) {
  129. $this->success();
  130. } else {
  131. $this->error(__('No rows were inserted'));
  132. }
  133. }
  134. $this->error(__('Parameter %s can not be empty', ''));
  135. }
  136. return $this->view->fetch();
  137. }
  138. /**
  139. * 编辑
  140. */
  141. public function edit($ids = null)
  142. {
  143. $row = $this->model->get($ids);
  144. if (!$row) {
  145. $this->error(__('No Results were found'));
  146. }
  147. $adminIds = $this->getDataLimitAdminIds();
  148. if (is_array($adminIds)) {
  149. if (!in_array($row[$this->dataLimitField], $adminIds)) {
  150. $this->error(__('You have no permission'));
  151. }
  152. }
  153. if ($this->request->isPost()) {
  154. $params = $this->request->post("row/a");
  155. if ($params) {
  156. $params = $this->preExcludeFields($params);
  157. $result = false;
  158. Db::startTrans();
  159. try {
  160. //是否采用模型验证
  161. if ($this->modelValidate) {
  162. $name = str_replace("\\model\\", "\\validate\\", get_class($this->model));
  163. $validate = is_bool($this->modelValidate) ? ($this->modelSceneValidate ? $name . '.edit' : $name) : $this->modelValidate;
  164. $row->validateFailException(true)->validate($validate);
  165. }
  166. $result = $row->allowField(true)->save($params);
  167. Db::commit();
  168. } catch (ValidateException $e) {
  169. Db::rollback();
  170. $this->error($e->getMessage());
  171. } catch (PDOException $e) {
  172. Db::rollback();
  173. $this->error($e->getMessage());
  174. } catch (Exception $e) {
  175. Db::rollback();
  176. $this->error($e->getMessage());
  177. }
  178. if ($result !== false) {
  179. $this->success();
  180. } else {
  181. $this->error(__('No rows were updated'));
  182. }
  183. }
  184. $this->error(__('Parameter %s can not be empty', ''));
  185. }
  186. $this->view->assign("row", $row);
  187. return $this->view->fetch();
  188. }
  189. }