Dispatch.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <?php
  2. namespace app\admin\controller\shopro\dispatch;
  3. use think\Db;
  4. use app\admin\controller\shopro\Common;
  5. use app\admin\model\shopro\dispatch\Dispatch as DispatchModel;
  6. use app\admin\model\shopro\dispatch\DispatchExpress as DispatchExpressModel;
  7. use app\admin\model\shopro\dispatch\DispatchAutosend as DispatchAutosendModel;
  8. /**
  9. * 配送管理
  10. */
  11. class Dispatch extends Common
  12. {
  13. protected $noNeedRight = ['select'];
  14. protected $expressModel;
  15. protected $autosendModel;
  16. protected $dispatch_type;
  17. public function _initialize()
  18. {
  19. parent::_initialize();
  20. $this->model = new DispatchModel;
  21. $this->expressModel = new DispatchExpressModel;
  22. $this->autosendModel = new DispatchAutosendModel;
  23. $this->dispatch_type = $this->request->param('type', 'express');
  24. }
  25. /**
  26. * 配送方式列表
  27. */
  28. public function index()
  29. {
  30. if (!$this->request->isAjax()) {
  31. return $this->view->fetch();
  32. }
  33. $dispatchs = $this->model->sheepFilter()->with([$this->dispatch_type])->where('type', $this->dispatch_type)->paginate($this->request->param('list_rows', 10));
  34. $this->success('获取成功', null, $dispatchs);
  35. }
  36. /**
  37. * 添加配送方式
  38. */
  39. public function add()
  40. {
  41. if (!$this->request->isAjax()) {
  42. return $this->view->fetch();
  43. }
  44. $params = $this->request->only([
  45. 'name', 'type', 'status', 'express', 'autosend'
  46. ]);
  47. $this->svalidate($params, ".add");
  48. $data = $params[$this->dispatch_type] ?? [];
  49. unset($params['express'], $params['autosend']);
  50. if ($this->dispatch_type == 'express') {
  51. // 验证 express
  52. foreach ($data as $key => $express) {
  53. $this->svalidate($express, '.express');
  54. }
  55. } else if ($this->dispatch_type == 'autosend') {
  56. // 验证 autosend
  57. $this->svalidate($data, '.autosend');
  58. }
  59. Db::transaction(function () use ($params, $data) {
  60. unset($params['createtime'], $params['updatetime'], $params['id']); // 删除时间
  61. $this->model->allowField(true)->save($params);
  62. if ($this->dispatch_type == 'express') {
  63. foreach ($data as $key => $express) {
  64. $express['dispatch_id'] = $this->model->id;
  65. $expressModel = new DispatchExpressModel();
  66. unset($express['createtime'], $express['updatetime'], $express['id']); // 删除时间
  67. $expressModel->allowField(true)->save($express);
  68. }
  69. } else if ($this->dispatch_type == 'autosend') {
  70. $data['dispatch_id'] = $this->model->id;
  71. $autosendModel = new DispatchAutosendModel();
  72. unset($data['createtime'], $data['updatetime'], $data['id']); // 删除时间
  73. $autosendModel->allowField(true)->save($data);
  74. }
  75. });
  76. $this->success('保存成功');
  77. }
  78. /**
  79. * 配送方式详情
  80. *
  81. * @param $id
  82. */
  83. public function detail($id)
  84. {
  85. $dispatch = $this->model->with([$this->dispatch_type])->where('type', $this->dispatch_type)->where('id', $id)->find();
  86. if (!$dispatch) {
  87. $this->error(__('No Results were found'));
  88. }
  89. $this->success('获取成功', null, $dispatch);
  90. }
  91. /**
  92. * 修改配送方式
  93. */
  94. public function edit($id = null)
  95. {
  96. if (!$this->request->isAjax()) {
  97. return $this->view->fetch('add');
  98. }
  99. $params = $this->request->only([
  100. 'name', 'type', 'status', 'express', 'autosend'
  101. ]);
  102. $this->svalidate($params);
  103. $data = $params[$this->dispatch_type] ?? [];
  104. unset($params['express'], $params['autosend']);
  105. if ($this->dispatch_type == 'express') {
  106. // 验证 express
  107. foreach ($data as $key => $express) {
  108. $this->svalidate($express, '.express');
  109. }
  110. } else if ($this->dispatch_type == 'autosend') {
  111. // 验证 autosend
  112. $this->svalidate($data, '.autosend');
  113. }
  114. $id = explode(',', $id);
  115. $lists = $this->model->whereIn('id', $id)->select();
  116. Db::transaction(function () use ($lists, $params, $data) {
  117. foreach ($lists as $dispatch) {
  118. $dispatch->allowField(true)->save($params);
  119. if ($data) {
  120. if ($this->dispatch_type == 'express') {
  121. // 修改,不是只更新状态
  122. $expressIds = array_column($data, 'id');
  123. DispatchExpressModel::where('dispatch_id', $dispatch->id)->whereNotIn('id', $expressIds)->delete(); // 先删除被删除的记录
  124. foreach ($data as $key => $express) {
  125. if (isset($express['id']) && $express['id']) {
  126. $expressModel = $this->expressModel->find($express['id']);
  127. } else {
  128. $expressModel = new DispatchExpressModel();
  129. $express['dispatch_id'] = $dispatch->id;
  130. }
  131. $express['weigh'] = count($data) - $key; // 权重
  132. unset($express['createtime'], $express['updatetime']);
  133. $expressModel && $expressModel->allowField(true)->save($express);
  134. }
  135. } else if ($this->dispatch_type == 'autosend') {
  136. if (isset($data['id']) && $data['id']) {
  137. $autosendModel = $this->autosendModel->find($data['id']);
  138. } else {
  139. $autosendModel = new DispatchAutosendModel();
  140. $data['dispatch_id'] = $dispatch->id;
  141. }
  142. unset($data['createtime'], $data['updatetime']); // 删除时间
  143. $autosendModel->allowField(true)->save($data);
  144. }
  145. }
  146. }
  147. });
  148. $this->success('更新成功');
  149. }
  150. /**
  151. * 删除配送方式
  152. *
  153. * @param string $id 要删除的配送方式列表
  154. * @return void
  155. */
  156. public function delete($id)
  157. {
  158. if (empty($id)) {
  159. $this->error(__('Parameter %s can not be empty', 'id'));
  160. }
  161. $id = explode(',', $id);
  162. $list = $this->model->with([$this->dispatch_type])->where('type', $this->dispatch_type)->where('id', 'in', $id)->select();
  163. Db::transaction(function () use ($list) {
  164. $count = 0;
  165. foreach ($list as $item) {
  166. if ($this->dispatch_type == 'express') {
  167. // 删除相关的 express 数据
  168. foreach ($item->express as $express) {
  169. $express->delete();
  170. }
  171. } else if ($this->dispatch_type == 'autosend') {
  172. $item->{$this->dispatch_type}->delete();
  173. }
  174. $count += $item->delete();
  175. }
  176. return $count;
  177. });
  178. $this->success('删除成功');
  179. }
  180. /**
  181. * 获取所有配送模板
  182. */
  183. public function select()
  184. {
  185. if (!$this->request->isAjax()) {
  186. return $this->view->fetch();
  187. }
  188. $dispatchs = $this->model->sheepFilter()->field('id, name, type, status')->normal()->where('type', $this->dispatch_type)->select();
  189. $this->success('获取成功', null, $dispatchs);
  190. }
  191. }