FengsuShip.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller\goods;
  4. use app\common\controller\Backend;
  5. use app\admin\traits\Actions;
  6. use think\annotation\route\Group;
  7. use think\annotation\route\Route;
  8. use think\facade\Db;
  9. use app\common\model\ShopList;
  10. use app\common\model\StockConfig;
  11. use app\common\model\FengsuShip as FengsuShipModel;
  12. #[Group("goods/fengsu_ship")]
  13. class FengsuShip extends Backend
  14. {
  15. //app\admin\controller\goods\FengsuShip
  16. use Actions{
  17. index as private _index;
  18. del as private _del;
  19. }
  20. protected function _initialize()
  21. {
  22. parent::_initialize();
  23. $this->model = new FengsuShipModel();
  24. $this->assign('shopList', ShopList::where('status', 1)->where('shop_id', '')->column('name','id'));
  25. $this->assign('typeList', StockConfig::where('type_id', 'variety_name')->order('sort desc')->column('title','id'));
  26. $this->assign('packingList', StockConfig::where('type_id', 'packing_box')->column('title','id'));
  27. }
  28. //查看
  29. #[Route("GET,JSON","index")]
  30. public function index()
  31. {
  32. return $this->_index();
  33. }
  34. /**
  35. * 关联店铺
  36. */
  37. #[Route('GET,POST','shops')]
  38. public function shops()
  39. {
  40. if (false === $this->request->isPost()) {
  41. $ids =$this->request->get('ids/s', '');
  42. return $this->fetch();
  43. }
  44. $params = array_merge($this->request->post("row/a"),$this->postParams);
  45. if (empty($params)) {
  46. $this->error(__('提交的参数不能为空'));
  47. }
  48. if(!$this->request->checkToken('__token__',['__token__'=>$this->request->post('__token__')])){
  49. $this->error(__('token错误,请刷新页面重试'));
  50. }
  51. foreach ($params as &$value){
  52. if(is_array($value)){
  53. $value=implode(',',$value);
  54. }
  55. if($value===''){
  56. $value=null;
  57. }
  58. }
  59. $ids =$this->request->get('ids/s', '');
  60. if(empty($params['shop_id'])) $this->error(__('请选择店铺'));
  61. $result = false;
  62. Db::startTrans();
  63. try {
  64. //更新店铺
  65. ShopList::where('id', $params['shop_id'])->update(['shop_id' => $ids]);
  66. //更新状态
  67. $result = $this->model->where('shop_id', $ids)->update(['status' => 2]);
  68. if($this->callback){
  69. $callback=$this->callback;
  70. $callback($this->model);
  71. }
  72. Db::commit();
  73. } catch (\Exception $e) {
  74. Db::rollback();
  75. $this->error($e->getMessage());
  76. }
  77. if ($result === false) {
  78. $this->error(__('没有新增任何数据'));
  79. }
  80. $this->success();
  81. }
  82. /**
  83. * 关联规格
  84. */
  85. #[Route('GET,POST','specs')]
  86. public function specs(mixed $row=null)
  87. {
  88. $ids = $this->request->get('ids');
  89. if(!$row || is_array($row)){
  90. $row = $this->model->find($ids);
  91. }
  92. if (!$row) {
  93. $this->error(__('没有找到记录'));
  94. }
  95. if(count($this->volidateFields)>0){
  96. foreach ($this->volidateFields as $field=>$value){
  97. if($row[$field]!=$value){
  98. $this->error(__('没有操作权限'));
  99. }
  100. }
  101. }
  102. if (false === $this->request->isPost()) {
  103. $this->assign('row', $row);
  104. return $this->fetch();
  105. }
  106. $params = array_merge($this->request->post("row/a"),$this->postParams);
  107. if (empty($params)) {
  108. $this->error(__('提交的参数不能为空'));
  109. }
  110. if(!$this->request->checkToken('__token__',['__token__'=>$this->request->post('__token__')])){
  111. $this->error(__('token错误,请刷新页面重试'));
  112. }
  113. foreach ($params as &$value){
  114. if(is_array($value)){
  115. $value=implode(',',$value);
  116. }
  117. if($value===''){
  118. $value=null;
  119. }
  120. }
  121. if($params['account_term'] == 2 && empty($params['cycle'])) $this->error(__('请选择周结日期'));
  122. $result = false;
  123. Db::startTrans();
  124. try {
  125. $result = $row->save($params);
  126. if($this->callback){
  127. $callback=$this->callback;
  128. $callback($row);
  129. }
  130. Db::commit();
  131. } catch (\Exception $e) {
  132. Db::rollback();
  133. $this->error($e->getMessage());
  134. }
  135. if (false === $result) {
  136. $this->error(__('没有数据被更新'));
  137. }
  138. $this->success();
  139. }
  140. //删除
  141. #[Route("GET,POST","del")]
  142. public function del()
  143. {
  144. //通过定义callback回调函数来执行删除后的操作
  145. $this->callback=function ($ids){};
  146. return $this->_del();
  147. }
  148. }