Order.php 7.6 KB

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