FengsuShip.php 5.5 KB

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