Order.php 9.2 KB

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