DispatchController.php 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Author: 芸众商城 www.yunzshop.com
  5. * Date: 2017/3/3
  6. * Time: 下午4:30
  7. */
  8. namespace app\backend\modules\goods\controllers;
  9. use app\backend\modules\goods\services\DispatchService;
  10. use app\common\components\BaseController;
  11. use app\backend\modules\goods\models\Dispatch;
  12. use app\backend\modules\goods\models\Area;
  13. use app\common\helpers\PaginationHelper;
  14. use app\common\helpers\Url;
  15. use Setting;
  16. use app\common\models\goods\GoodsDispatch;
  17. class DispatchController extends BaseController
  18. {
  19. /**
  20. * 配送模板列表
  21. * @return array $item
  22. */
  23. public function index()
  24. {
  25. // $shopset = Setting::get('shop');
  26. // $pageSize = 10;
  27. // $list = Dispatch::uniacid()->where(['is_plugin' => 0 , 'plugin_id' => 0])->orderBy('display_order', 'desc')->orderBy('id', 'desc')->paginate($pageSize)->toArray();
  28. // $pager = PaginationHelper::show($list['total'], $list['current_page'], $list['per_page']);
  29. // return view('goods.dispatch.list', [
  30. // 'list' => $list,
  31. // 'pager' => $pager,
  32. // 'shopset' => $shopset
  33. // ])->render();
  34. return view('goods.dispatch.list')->render();
  35. }
  36. public function dispatchData()
  37. {
  38. // $shopset = Setting::get('shop');
  39. $pageSize = 10;
  40. $list = Dispatch::uniacid()->where(['is_plugin' => 0 , 'plugin_id' => 0])
  41. ->orderBy('display_order', 'desc')
  42. ->orderBy('id', 'desc')
  43. ->paginate($pageSize)->toArray();
  44. foreach($list['data'] as &$item){
  45. $item['piece_data'] = unserialize($item['piece_data']);
  46. $item['weight_data'] = unserialize($item['weight_data']);
  47. }
  48. return $this->successJson('ok',$list);
  49. }
  50. public function editSave()
  51. {
  52. return view('goods.dispatch.info', [
  53. 'id' => request()->id,
  54. ])->render();
  55. }
  56. /**
  57. * 配送模板添加
  58. * @return array $item
  59. */
  60. public function add()
  61. {
  62. $dispatchModel = new Dispatch();
  63. $areas = Area::getProvinces(0);
  64. $requestDispatch = request()->dispatch;
  65. $random = 1;
  66. if ($requestDispatch) {
  67. // dd($requestDispatch);
  68. $requestDispatch = DispatchService::getDispatch($requestDispatch);
  69. //将数据赋值到model
  70. $dispatchModel->setRawAttributes($requestDispatch);
  71. //其他字段赋值
  72. $dispatchModel->uniacid = \YunShop::app()->uniacid;
  73. //字段检测
  74. $validator = $dispatchModel->validator($dispatchModel->getAttributes());
  75. if ($validator->fails()) {//检测失败
  76. $this->errorJson($validator->messages());
  77. } else {
  78. //取消其他默认模板
  79. if($dispatchModel->is_default){
  80. $defaultModel = Dispatch::getOneByDefault();
  81. if ($defaultModel) {
  82. $defaultModel->is_default = 0;
  83. $defaultModel->save();
  84. }
  85. }
  86. //dd($dispatchModel);
  87. //数据保存
  88. if ($dispatchModel->save()) {
  89. //显示信息并跳转
  90. // return $this->message('配送模板创建成功', Url::absoluteWeb('goods.dispatch.edit',['id' => $dispatchModel->id]));
  91. return $this->successJson('配送模板创建成功');
  92. } else {
  93. $this->errorJson('配送模板创建失败');
  94. }
  95. }
  96. }
  97. $data = [
  98. 'dispatch' => $dispatchModel,
  99. 'parents' => $areas->toArray(),
  100. 'random' => $random,
  101. ];
  102. return $this->successJson('ok',$data);
  103. // return view('goods.dispatch.info', [
  104. // 'dispatch' => $dispatchModel,
  105. // 'parents' => $areas->toArray(),
  106. // 'random' => $random,
  107. // ])->render();
  108. }
  109. /**
  110. * 配送模板编辑
  111. * @return array $item
  112. */
  113. public function edit()
  114. {
  115. $dispatchModel = Dispatch::find(request()->id);
  116. if (!$dispatchModel) {
  117. return $this->errorJson('无此记录或已被删除');
  118. }
  119. $dispatchModel->weight_data = unserialize($dispatchModel->weight_data);
  120. $dispatchModel->piece_data = unserialize($dispatchModel->piece_data);
  121. if(!$dispatchModel->calculate_type){
  122. $random = $dispatchModel->weight_data ? count($dispatchModel->weight_data) + 1 : 1;
  123. }else{
  124. $random = $dispatchModel->piece_data ? count($dispatchModel->piece_data) + 1 : 1;
  125. }
  126. $areas = Area::getProvinces(0);
  127. $requestDispatch = request()->dispatch;
  128. if ($requestDispatch) {
  129. $requestDispatch = DispatchService::getDispatch($requestDispatch);
  130. //将数据赋值到model
  131. $dispatchModel->setRawAttributes($requestDispatch);
  132. //其他字段赋值
  133. $dispatchModel->uniacid = \YunShop::app()->uniacid;
  134. //字段检测
  135. $validator = $dispatchModel->validator($dispatchModel->getAttributes());
  136. if ($validator->fails()) {//检测失败
  137. $this->errorJson($validator->messages());
  138. } else {
  139. //取消其他默认模板
  140. if($dispatchModel->is_default){
  141. $defaultModel = Dispatch::getOneByDefault();
  142. if ($defaultModel && ($defaultModel->id != request()->id) ) {
  143. $defaultModel->is_default = 0;
  144. $defaultModel->save();
  145. }
  146. }
  147. //数据保存
  148. if ($dispatchModel->save()) {
  149. //显示信息并跳转
  150. return $this->successJson('配送模板更新成功');
  151. } else {
  152. $this->errorJson('配送模板更新失败');
  153. }
  154. }
  155. }
  156. $data = [
  157. 'dispatch' => $dispatchModel,
  158. 'parents' => $areas->toArray(),
  159. 'random' => $random,
  160. ];
  161. return $this->successJson('ok',$data);
  162. //dd($dispatchModel);
  163. // return view('goods.dispatch.info', [
  164. // 'dispatch' => $dispatchModel,
  165. // 'parents' => $areas->toArray(),
  166. // 'random' => $random,
  167. // ])->render();
  168. }
  169. /**
  170. * 配送模板删除
  171. * @return array $item
  172. */
  173. public function delete()
  174. {
  175. $dispatch = Dispatch::getOne(request()->id);
  176. if (!$dispatch) {
  177. return $this->errorJson('无此配送模板或已经删除');
  178. }
  179. $model = Dispatch::find(request()->id);
  180. if($model->is_default == 1){
  181. return $this->errorJson('默认模板不支持删除,如果需要删除请先设置其他的模板为默认模板');
  182. }
  183. $models= Dispatch::uniacid()->where('is_default',1)->first();
  184. if(!$models){
  185. return $this->errorJson('删除失败,请先添加默认的模板,当删除这个模板后,所有这个配送模板商品会自动选择到默认的配送模板');
  186. }
  187. $res = GoodsDispatch::where('dispatch_id',$model->id)->update(['dispatch_id' => $models->id]);
  188. if ($model->delete()) {
  189. return $this->successJson('删除模板成功');
  190. } else {
  191. return $this->errorJson('删除模板失败');
  192. }
  193. }
  194. /**
  195. * 配送模板排序
  196. * @return array $item
  197. */
  198. public function sort()
  199. {
  200. $displayOrders = request()->display_order;
  201. foreach($displayOrders as $id => $displayOrder){
  202. $dispatch = Dispatch::find($id);
  203. $dispatch->display_order = $displayOrder;
  204. $dispatch->save();
  205. }
  206. return $this->successJson('排序成功');
  207. }
  208. /**
  209. * 配送模板排序
  210. * @return array $item
  211. */
  212. public function sortV2()
  213. {
  214. $id = request()->id;
  215. $sort = request()->sort;
  216. $dispatch = Dispatch::find($id);
  217. $dispatch->display_order = $sort;
  218. $dispatch->save();
  219. return $this->successJson('排序成功');
  220. }
  221. public function selectCity()
  222. {
  223. $citys = Area::getAreasByCity(request()->parent_id);
  224. return $this->successJson('ok',$citys->toArray());
  225. // return view('area.selectcitys', [
  226. // 'citys' => $citys->toArray()
  227. // ])->render();
  228. }
  229. public function quickEdit()
  230. {
  231. $id = request()->id;
  232. $type = request()->type;
  233. $status = request()->status;
  234. $result = Dispatch::quickUpdatedDispatch($id,$type,$status);
  235. if ($result){
  236. return $this->successJson('修改成功');
  237. }else{
  238. return $this->errorJson('修改失败');
  239. }
  240. }
  241. }