Product.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. <?php
  2. namespace app\shop\controller\product;
  3. use app\common\model\product\ProductTimer;
  4. use app\common\model\shop\Task;
  5. use app\shop\model\product\Product as ProductModel;
  6. use app\shop\model\product\Category as CategoryModel;
  7. use app\shop\service\ProductService;
  8. use app\shop\controller\Controller;
  9. use think\facade\Cache;
  10. use think\response\Json;
  11. /**
  12. * 商品管理控制器
  13. */
  14. class Product extends Controller
  15. {
  16. /**
  17. * 商品列表(全部)
  18. */
  19. public function index()
  20. {
  21. // 获取全部商品列表
  22. $model = new ProductModel;
  23. $list = $model->getList(array_merge(['status' => -1], $this->postData()));
  24. // 商品分类
  25. $category = CategoryModel::getCacheTree();
  26. // 数量
  27. $product_count = [
  28. 'sell' => $model->getCount('sell'),
  29. 'stock' => $model->getCount('stock'),
  30. 'recovery' => $model->getCount('recovery'),
  31. 'over' => $model->getCount('over'),
  32. 'lower' => $model->getCount('lower')
  33. ];
  34. return $this->renderSuccess('', compact('list', 'category', 'product_count'));
  35. }
  36. /**
  37. * 商品列表(在售)
  38. */
  39. public function lists()
  40. {
  41. // 获取全部商品列表
  42. $model = new ProductModel;
  43. $list = $model->getLists($this->postData());
  44. // 商品分类
  45. $catgory = CategoryModel::getCacheTree();
  46. return $this->renderSuccess('', compact('list', 'catgory'));
  47. }
  48. /**
  49. * 添加商品
  50. */
  51. public function add($scene = 'add')
  52. {
  53. // get请求
  54. if($this->request->isGet()){
  55. return $this->getBaseData();
  56. }
  57. //post请求
  58. $data = json_decode($this->postData()['params'], true);
  59. if($scene == 'copy'){
  60. unset($data['create_time']);
  61. unset($data['sku']['product_sku_id']);
  62. unset($data['sku']['product_id']);
  63. unset($data['product_sku']['product_sku_id']);
  64. unset($data['product_sku']['product_id']);
  65. if($data['spec_type'] == 20){
  66. foreach ($data['spec_many']['spec_list'] as &$spec){
  67. $spec['product_sku_id'] = 0;
  68. }
  69. }
  70. //初始化销量等数据
  71. $data['sales_initial'] = 0;
  72. }
  73. $model = new ProductModel;
  74. if (isset($data['product_id'])) {
  75. $data['product_id'] = 0;
  76. }
  77. if ($model->add($data)) {
  78. return $this->renderSuccess('添加成功');
  79. }
  80. return $this->renderError($model->getError() ?: '添加失败');
  81. }
  82. /**
  83. * 获取基础数据
  84. */
  85. public function getBaseData()
  86. {
  87. return $this->renderSuccess('', array_merge(ProductService::getEditData(null, 'add'), []));
  88. }
  89. /**
  90. * 获取编辑数据
  91. */
  92. public function getEditData($product_id, $scene = 'edit')
  93. {
  94. $model = ProductModel::detail($product_id);
  95. return $this->renderSuccess('', array_merge(ProductService::getEditData($model, $scene), compact('model')));
  96. }
  97. /**
  98. * 商品编辑
  99. */
  100. public function edit($product_id, $scene = 'edit')
  101. {
  102. if($this->request->isGet()){
  103. $model = ProductModel::detail($product_id);
  104. // p($model['is_enable_grade']);
  105. return $this->renderSuccess('', array_merge(ProductService::getEditData($model, $scene), compact('model')));
  106. }
  107. if ($scene == 'copy') {
  108. return $this->add($scene);
  109. }
  110. // 商品详情
  111. $model = ProductModel::detail($product_id);
  112. // 更新记录
  113. if ($model->edit(json_decode($this->postData()['params'], true))) {
  114. return $this->renderSuccess('更新成功');
  115. }
  116. return $this->renderError($model->getError() ?: '更新失败');
  117. }
  118. /**
  119. * 修改商品状态
  120. */
  121. public function state($product_id, $state)
  122. {
  123. // 商品详情
  124. $model = ProductModel::detail($product_id);
  125. if (!$model->setStatus($state)) {
  126. return $this->renderError('操作失败');
  127. }
  128. // 删除定时器
  129. $timer = ProductTimer::detail($product_id);
  130. if ($timer) {
  131. // 删除定时数据
  132. $timer->deleteTimer($product_id);
  133. // 从缓存中删除队列
  134. Cache::store('redis')->delete($timer['task_id']);
  135. // 写入task表,留存执行记录
  136. $record = [
  137. 'task_id' => $timer['task_id'],
  138. 'category' => 'product',
  139. 'tid' => $timer['product_id'],
  140. 'state' => $timer['state_id'],
  141. 'publish_time' => $timer['publish_time'],
  142. 'payload' => '',
  143. 'result' => 2, // 1成功,0失败,2取消
  144. ];
  145. Task::add($record);
  146. }
  147. return $this->renderSuccess('操作成功');
  148. }
  149. /**
  150. * 商品定时上下架
  151. * @param string $product_id 商品ID
  152. * @param integer $state 状态
  153. * @param string $solt 时间
  154. * @return Json
  155. */
  156. public function addTimer(string $product_id, int $state, string $solt): Json
  157. {
  158. // 商品详情
  159. $model = ProductModel::detail($product_id);
  160. if (!$model) {
  161. return $this->renderError('商品错误');
  162. }
  163. if (!$model->setTimer($model, $state, $solt)) {
  164. return $this->renderError('操作失败');
  165. }
  166. return $this->renderSuccess('操作成功');
  167. }
  168. /**
  169. * 删除商品定时上下架
  170. * @param $product_id
  171. * @return Json
  172. */
  173. public function deleteTimer($product_id): Json
  174. {
  175. $timer = ProductTimer::detail($product_id);
  176. // 删除定时数据
  177. if (!$timer->deleteTimer($product_id)) {
  178. return $this->renderError('操作失败');
  179. }
  180. // 写入task表,留存执行记录
  181. $record = [
  182. 'task_id' => $timer['task_id'],
  183. 'category' => 'product',
  184. 'tid' => $timer['product_id'],
  185. 'state' => $timer['state_id'],
  186. 'publish_time' => $timer['publish_time'],
  187. 'payload' => '',
  188. 'result' => 2, // 1成功,0失败,2取消
  189. ];
  190. Task::add($record);
  191. return $this->renderSuccess('操作成功');
  192. }
  193. /**
  194. * 删除商品
  195. */
  196. public function delete($product_id): Json
  197. {
  198. // 商品详情
  199. $model = ProductModel::detail($product_id);
  200. if (!$model->setDelete()) {
  201. return $this->renderError($model->getError() ?: '删除失败');
  202. }
  203. // 删除定时器
  204. $timer = ProductTimer::detail($product_id);
  205. if ($timer) {
  206. // 删除定时数据
  207. $timer->deleteTimer($product_id);
  208. // 写入task表,留存执行记录
  209. $record = [
  210. 'task_id' => $timer['task_id'],
  211. 'category' => 'product',
  212. 'tid' => $timer['product_id'],
  213. 'state' => $timer['state_id'],
  214. 'publish_time' => $timer['publish_time'],
  215. 'payload' => '',
  216. 'result' => 2, // 1成功,0失败,2取消
  217. ];
  218. Task::add($record);
  219. }
  220. return $this->renderSuccess('删除成功');
  221. }
  222. }