Product.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <?php
  2. namespace app\supplier\controller\product;
  3. use app\common\model\settings\Setting;
  4. use app\supplier\model\product\Product as ProductModel;
  5. use app\common\model\product\Category as CategoryModel;
  6. use app\supplier\service\ProductService;
  7. use app\supplier\model\supplier\Supplier as SupplierModel;
  8. use app\supplier\controller\Controller;
  9. /**
  10. * 商品管理控制器
  11. */
  12. class Product extends Controller
  13. {
  14. /**
  15. * 商品列表(全部)
  16. */
  17. public function index()
  18. {
  19. $supplier = SupplierModel::detail($this->getSupplierId());
  20. // 获取全部商品列表
  21. $model = new ProductModel;
  22. $list = $model->getList(array_merge(['status' => -1, 'shop_supplier_id' => $this->getSupplierId()], $this->postData()));
  23. // 商品分类
  24. $category = CategoryModel::getCacheTree();
  25. // 数量
  26. $product_count = [
  27. 'sell' => $model->getCount('sell', $this->getSupplierId()),
  28. 'stock' => $model->getCount('stock', $this->getSupplierId()),
  29. 'recovery' => $model->getCount('recovery', $this->getSupplierId()),
  30. 'draft' => $model->getCount('draft', $this->getSupplierId()),
  31. 'lower' => $model->getCount('lower', $this->getSupplierId()),
  32. 'audit' => $model->getCount('audit', $this->getSupplierId()),
  33. 'no_audit' => $model->getCount('no_audit', $this->getSupplierId())
  34. ];
  35. return $this->renderSuccess('', compact('list', 'category', 'supplier', 'product_count'));
  36. }
  37. /**
  38. * 添加商品
  39. */
  40. public function add($scene = 'add')
  41. {
  42. // get请求
  43. if($this->request->isGet()){
  44. return $this->getBaseData();
  45. }
  46. //post请求
  47. $data = json_decode($this->postData()['params'], true);
  48. if($scene == 'copy'){
  49. unset($data['create_time']);
  50. unset($data['sku']['product_sku_id']);
  51. unset($data['sku']['product_id']);
  52. unset($data['product_sku']['product_sku_id']);
  53. unset($data['product_sku']['product_id']);
  54. // 如果是多规格
  55. if($data['spec_type'] == 20){
  56. foreach ($data['spec_many']['spec_list'] as &$sku){
  57. $sku['product_sku_id'] = 0;
  58. }
  59. }
  60. //初始化销量等数据
  61. $data['sales_initial'] = 0;
  62. }
  63. //是否需要审核
  64. $add_audit = Setting::getItem('store')['add_audit'];
  65. if($add_audit == 0){
  66. // 如果不需要审核,则审核状态是已审核
  67. $data['audit_status'] = 10;
  68. }
  69. $model = new ProductModel;
  70. if (isset($data['product_id'])) {
  71. $data['product_id'] = 0;
  72. }
  73. $data['shop_supplier_id'] = $this->getSupplierId();
  74. if ($model->add($data)) {
  75. return $this->renderSuccess('添加成功');
  76. }
  77. return $this->renderError($model->getError() ?: '添加失败');
  78. }
  79. /**
  80. * 获取基础数据
  81. */
  82. public function getBaseData()
  83. {
  84. return $this->renderSuccess('', array_merge(ProductService::getEditData(null, 'add', $this->getSupplierId()), []));
  85. }
  86. /**
  87. * 获取编辑数据
  88. */
  89. public function getEditData($product_id, $scene = 'edit')
  90. {
  91. $model = ProductModel::detail($product_id);
  92. return $this->renderSuccess('', array_merge(ProductService::getEditData($model, $scene, $this->getSupplierId()), compact('model')));
  93. }
  94. /**
  95. * 商品编辑
  96. */
  97. public function edit($product_id, $scene = 'edit')
  98. {
  99. if($this->request->isGet()){
  100. $model = ProductModel::detail($product_id);
  101. return $this->renderSuccess('', array_merge(ProductService::getEditData($model, $scene, $this->getSupplierId()), compact('model')));
  102. }
  103. if ($scene == 'copy') {
  104. return $this->add($scene);
  105. }
  106. // 商品详情
  107. $model = ProductModel::detail($product_id);
  108. // 更新记录
  109. if ($model->edit(json_decode($this->postData()['params'], true),$this->getSupplierId())) {
  110. return $this->renderSuccess('更新成功');
  111. }
  112. return $this->renderError($model->getError() ?: '更新失败');
  113. }
  114. /**
  115. * 修改商品状态
  116. */
  117. public function state($product_id, $state)
  118. {
  119. // 商品详情
  120. $model = ProductModel::detail($product_id);
  121. if($model['audit_status'] != 10){
  122. return $this->renderError('商品状态不正确');
  123. }
  124. if (!$model->setStatus($state)) {
  125. return $this->renderError('操作失败');
  126. }
  127. return $this->renderSuccess('操作成功');
  128. }
  129. /**
  130. * 删除商品
  131. */
  132. public function delete($product_id)
  133. {
  134. // 商品详情
  135. $model = ProductModel::detail($product_id);
  136. if (!$model->setDelete()) {
  137. return $this->renderError($model->getError() ?: '删除失败');
  138. }
  139. return $this->renderSuccess('删除成功');
  140. }
  141. }