ProductBatchController.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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\models\Goods;
  9. use app\modules\admin\models\GoodsForm;
  10. use app\plugins\product_traceability\models\form\SearchAllGoodsForm;
  11. use app\plugins\product_traceability\models\Product;
  12. use app\plugins\product_traceability\models\ProductBatch;
  13. use app\utils\QrCode;
  14. use app\utils\ShareQrcode;
  15. use yii\base\BaseObject;
  16. use yii\helpers\Json;
  17. class ProductBatchController extends BaseController
  18. {
  19. public function actionProductList()
  20. {
  21. $list = Product::find()->where([
  22. 'is_delete' => 0,
  23. 'store_id' => get_store_id(),
  24. 'is_show' => 1,
  25. ])->all();
  26. return [
  27. 'code' => 0,
  28. 'msg' => 'success',
  29. 'data' => [
  30. 'data' => $list,
  31. ],
  32. ];
  33. }
  34. public function actionProductBatchList()
  35. {
  36. $start_time = get_params('dateStart');
  37. $end_time = get_params('dateEnd');
  38. $search_product_name_key = get_params('search_product_name_key');
  39. $search_batch_name_key = get_params('search_batch_name_key');
  40. $search_goods_name_key = get_params('search_goods_name_key');
  41. $search_state = get_params('search_state', '-1');
  42. $query = ProductBatch::find()->alias('pb')
  43. ->leftJoin(['p' => Product::tableName()], 'p.id=pb.product_id')
  44. ->leftJoin(['g' => Goods::tableName()], 'g.id=pb.goods_id')
  45. ->where([
  46. 'pb.is_delete' => 0,
  47. 'pb.store_id' => get_store_id()
  48. ])->select(['pb.*', 'p.product_name', 'g.name as goods_name']);
  49. if (!empty($search_product_name_key)) {
  50. $query->andWhere(['like', 'p.product_name', trim($search_product_name_key)]);
  51. }
  52. if (!empty($search_batch_name_key)) {
  53. $query->andWhere(['like', 'pb.batch_name', trim($search_batch_name_key)]);
  54. }
  55. if (!empty($search_goods_name_key)) {
  56. $query->andWhere(['like', 'g.name', trim($search_goods_name_key)]);
  57. }
  58. if ($search_state != -1) {
  59. $query->andWhere(['pb.state' => $search_state]);
  60. }
  61. if ($start_time) {
  62. $query->andWhere(['>=', 'pb.created_at', strtotime($start_time)]);
  63. }
  64. if ($end_time) {
  65. $query->andWhere(['<=', 'pb.created_at', strtotime($end_time)]);
  66. }
  67. $list = pagination_make($query, true, 'id desc');
  68. $list= $list['list'];
  69. foreach ($list as $k => $v) {
  70. $goods_id_list = [$v['goods_id']];
  71. $list[$k]['goods_list'] = GoodsForm::getGoodsListById($goods_id_list);
  72. }
  73. return [
  74. 'code' => 0,
  75. 'msg' => 'success',
  76. 'data' => [
  77. 'data' => $list,
  78. 'pageNo' => $list['pageNo'],
  79. 'totalCount' => $list['totalCount'],
  80. ],
  81. ];
  82. }
  83. public function actionProductBatchDel()
  84. {
  85. $id = get_params('id');
  86. $product_batch = ProductBatch::findOne($id);
  87. $product_batch->is_delete = 1;
  88. if ($product_batch->save()) {
  89. return [
  90. 'code' => 0,
  91. 'msg' => '删除成功'
  92. ];
  93. }else {
  94. return [
  95. 'code' => 1,
  96. 'msg' => $product_batch->getErrors(),
  97. ];
  98. }
  99. }
  100. /**
  101. * 批量操作
  102. * @return \yii\web\Response
  103. */
  104. public function actionProductBatchOperate()
  105. {
  106. $id = post_params('id');
  107. $type = post_params('type');
  108. $status = post_params('status');
  109. if (empty($id) || !is_array($id)) {
  110. return [
  111. 'code' => 1,
  112. 'msg' => '参数有误'
  113. ];
  114. }
  115. if (!in_array($status, [0, 1])) {
  116. return [
  117. 'code' => 1,
  118. 'msg' => '状态参数有误'
  119. ];
  120. }
  121. if ($type == 'open' || $type == 'disabled') {
  122. ProductBatch::updateAll(['is_show' => $status], ['in', 'id', $id]);
  123. }
  124. if ($type == 'delete') {
  125. $res= ProductBatch::updateAll(['is_delete' => $status], ['in', 'id', $id]);
  126. }
  127. return [
  128. 'code' => 0,
  129. 'msg' => '更新成功'
  130. ];
  131. }
  132. public function actionProductBatchEdit()
  133. {
  134. $store_id = get_store_id();
  135. $id = post_params('id', 0);
  136. $batch_name = post_params('batch_name');
  137. $product_id = post_params('product_id', 0);
  138. $goods_id = post_params('goods_id', 0);
  139. if ($id > 0) {
  140. $product_batch = ProductBatch::findOne($id);
  141. } else {
  142. $product_batch = new ProductBatch();
  143. while (true) {
  144. $batch_number = date('YmdHis') . mt_rand(100000, 999999);
  145. $exist_batch_number = ProductBatch::find()->where(['batch_number' => $batch_number])->exists();
  146. if (!$exist_batch_number) {
  147. break;
  148. }
  149. }
  150. $product_batch->batch_number = $batch_number;
  151. }
  152. $exist_batch_goods = ProductBatch::find()->where(['goods_id' => $goods_id, 'is_delete' => 0])->one();
  153. if ($exist_batch_goods && $exist_batch_goods->id != $id) {
  154. return [
  155. 'code' => 1,
  156. 'msg' => '该商品已经存在,不能重复添加',
  157. ];
  158. }
  159. $product_batch->store_id = $store_id;
  160. $product_batch->product_id = $product_id;
  161. $product_batch->goods_id = $goods_id;
  162. $product_batch->batch_name = $batch_name;
  163. if ($product_batch->save()) {
  164. return [
  165. 'code' => 0,
  166. 'msg' => $id > 0 ? '编辑成功' : '添加成功',
  167. ];
  168. }else{
  169. return [
  170. 'code' => 1,
  171. 'msg' => $product_batch->getErrors(),
  172. ];
  173. }
  174. }
  175. /**
  176. * 获取小程序二维码
  177. */
  178. public function actionGetQr ()
  179. {
  180. $store_id = get_store_id();
  181. $product_batch_id = get_params('product_batch_id', 0);
  182. $md_id = get_md_id();
  183. $scene = "pb_id={$product_batch_id}&md_id={$md_id}&store={$store_id}";
  184. $res = ShareQrcode::wxQrcode('source/history/history', $scene);
  185. if (isset($res['code']) && $res['code'] == 1) {
  186. return [
  187. 'code' => 1,
  188. 'msg' => $res['response']['errmsg'],
  189. ];
  190. }
  191. return [
  192. 'code' => 0,
  193. 'data' => [
  194. 'qr_url' => $res['url_path']
  195. ]
  196. ];
  197. }
  198. public function actionGetAllGoodsList()
  199. {
  200. return SearchAllGoodsForm::getList(get_params());
  201. }
  202. }