ProductController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\plugins\product_traceability\controllers;
  8. use app\plugins\product_traceability\models\Product;
  9. use app\utils\QrCode;
  10. use yii\base\BaseObject;
  11. use yii\helpers\Json;
  12. class ProductController extends BaseController
  13. {
  14. public function actionProductList()
  15. {
  16. $start_time = get_params('dateStart');
  17. $end_time = get_params('dateEnd');
  18. $search_key = get_params('search_key');
  19. $search_is_show = get_params('search_is_show');
  20. $query = Product::find()->where([
  21. 'is_delete' => 0,
  22. 'store_id' => get_store_id()
  23. ]);
  24. if (!empty($search_key)) {
  25. $query->andWhere(['like', 'product_name', trim($search_key)]);
  26. }
  27. if ($search_is_show != -1) {
  28. $query->andWhere(['is_show' => $search_is_show]);
  29. }
  30. if ($start_time) {
  31. $query->andWhere(['>=', 'created_at', strtotime($start_time)]);
  32. }
  33. if ($end_time) {
  34. $query->andWhere(['<=', 'created_at', strtotime($end_time)]);
  35. }
  36. $list = pagination_make($query);
  37. return [
  38. 'code' => 0,
  39. 'msg' => 'success',
  40. 'data' => [
  41. 'data' => $list['list'],
  42. 'pageNo' => $list['pageNo'],
  43. 'totalCount' => $list['totalCount'],
  44. ],
  45. ];
  46. }
  47. public function actionProductShow()
  48. {
  49. $id = post_params('id');
  50. $product = Product::findOne($id);
  51. $product->is_show = $product->is_show === 1 ? 0 : 1;
  52. if ($product->save()) {
  53. return [
  54. 'code' => 0,
  55. 'msg' => '修改成功'
  56. ];
  57. }
  58. return [
  59. 'code' => 1,
  60. 'msg' => '修改失败',
  61. ];
  62. }
  63. public function actionProductDel()
  64. {
  65. $id = get_params('id');
  66. $product = Product::findOne($id);
  67. $product->is_delete = 1;
  68. if ($product->save()) {
  69. return [
  70. 'code' => 0,
  71. 'msg' => '删除成功'
  72. ];
  73. }
  74. return [
  75. 'code' => 1,
  76. 'msg' => '删除失败',
  77. ];
  78. }
  79. /**
  80. * 批量操作
  81. * @return \yii\web\Response
  82. */
  83. public function actionProductBatch()
  84. {
  85. $id = post_params('id');
  86. $type = post_params('type');
  87. $status = post_params('status');
  88. if (empty($id) || !is_array($id)) {
  89. return [
  90. 'code' => 1,
  91. 'msg' => '参数有误'
  92. ];
  93. }
  94. if (!in_array($status, [0, 1])) {
  95. return [
  96. 'code' => 1,
  97. 'msg' => '状态参数有误'
  98. ];
  99. }
  100. if ($type == 'open' || $type == 'disabled') {
  101. Product::updateAll(['is_show' => $status], ['in', 'id', $id]);
  102. }
  103. if ($type == 'delete') {
  104. Product::updateAll(['is_delete' => $status], ['in', 'id', $id]);
  105. }
  106. return [
  107. 'code' => 0,
  108. 'msg' => '更新成功'
  109. ];
  110. }
  111. public function actionProductEdit()
  112. {
  113. $store_id = get_store_id();
  114. $id = post_params('id', 0);
  115. $name = post_params('product_name');
  116. $is_show = post_params('is_show', 1);
  117. if ($id > 0) {
  118. $product = Product::findOne($id);
  119. } else {
  120. $product = new Product();
  121. }
  122. $product->store_id = $store_id;
  123. $product->product_name = $name;
  124. $product->is_show = $is_show;
  125. if ($product->save()) {
  126. return [
  127. 'code' => 0,
  128. 'msg' => $id > 0 ? '编辑成功' : '添加成功',
  129. ];
  130. }else{
  131. return [
  132. 'code' => 1,
  133. 'msg' => $product->getErrors(),
  134. ];
  135. }
  136. }
  137. }