MyProductProcessForm.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\plugins\product_traceability\models\client;
  8. use app\models\Goods;
  9. use app\models\SaasUser;
  10. use app\models\Shop;
  11. use app\models\Store;
  12. use app\models\User;
  13. use app\plugins\food\models\FoodOrderDetail;
  14. use app\plugins\product_traceability\models\Product;
  15. use app\plugins\product_traceability\models\ProductBatch;
  16. use app\plugins\product_traceability\models\ProductBatchProcess;
  17. use app\plugins\product_traceability\models\ProductBatchProcessLog;
  18. use app\utils\Tools;
  19. use yii\base\Model;
  20. use yii\data\Pagination;
  21. use yii\helpers\Json;
  22. use yii\helpers\Json as HelpersJson;
  23. class MyProductProcessForm extends Model
  24. {
  25. public $store_id;
  26. public $user_id;
  27. public $page;
  28. public $limit;
  29. public function rules()
  30. {
  31. return [
  32. [['store_id', 'user_id'], 'required'],
  33. [['page', 'limit'], 'integer'],
  34. [['page'], 'default', 'value' => 1],
  35. [['limit'], 'default', 'value' => 20],
  36. ];
  37. }
  38. public function search()
  39. {
  40. if (!$this->validate()) {
  41. return ['code' => 1, 'msg' => $this->getErrorSummary(false)[0]];
  42. }
  43. $query = ProductBatchProcess::find()->alias('pbp')
  44. ->innerJoin(['pbpl' => ProductBatchProcessLog::tableName()], 'pbp.id=pbpl.product_batch_process_id')
  45. ->leftJoin(['u' => User::tableName()], 'u.id=pbp.process_user_id')
  46. ->leftJoin(['pb' => ProductBatch::tableName()], 'pb.id=pbp.product_batch_id')
  47. ->leftJoin(['p' => Product::tableName()], 'p.id=pb.product_id')
  48. ->leftJoin(['g' => Goods::tableName()], 'g.id=pb.goods_id')
  49. ->where([
  50. 'pbpl.is_delete' => 0,
  51. 'pbpl.store_id' => get_store_id(),
  52. 'pbpl.state' => 0,
  53. 'pbp.process_user_id' => $this->user_id
  54. ])
  55. ->orderBy('pbp.sort ASC')
  56. ->groupBy("pbpl.product_batch_id")
  57. ->select(['pbpl.*', 'pbp.process_name', 'pb.batch_number', 'pb.batch_name', 'p.product_name', 'g.name as goods_name', 'u.nickname']);
  58. $count = $query->count();
  59. $pagination = new Pagination(['totalCount' => $count, 'page' => $this->page - 1, 'pageSize' => $this->limit]);
  60. $list = $query->limit($pagination->limit)->offset($pagination->offset)->asArray()->all();
  61. foreach ($list as $index => $value) {
  62. $product_batch_current_index = 0;
  63. $product_batch_current_count = 0;
  64. $product_batch_also_index = 0;
  65. $product_batch_also_count = 0;
  66. $product_batch_list = ProductBatchProcess::find()->alias("pbp")
  67. ->innerJoin(['pbpl' => ProductBatchProcessLog::tableName()], 'pbp.id=pbpl.product_batch_process_id')
  68. ->where([
  69. 'pbp.is_delete' => 0,
  70. 'pbp.store_id' => get_store_id(),
  71. 'pbp.product_batch_id' => $value["product_batch_id"],
  72. ])->orderBy("pbp.sort asc")->select(['pbpl.*', 'pbp.process_name', 'pbp.process_user_id'])->asArray()->all();
  73. foreach ($product_batch_list as $k => $v) {
  74. if ($v['state'] == 1 && $product_batch_list[$k+1]['state'] == 0) {
  75. $product_batch_current_count = $k+1;
  76. continue;
  77. }
  78. }
  79. foreach ($product_batch_list as $k => $v) {
  80. if ($v['process_user_id'] == $this->user_id && $v['state'] == 0 && $product_batch_also_index == 0) {
  81. $product_batch_also_count = $k;
  82. $product_batch_also_index++;
  83. continue;
  84. }
  85. }
  86. $product_batch_total_count = $product_batch_list ? count($product_batch_list) : 0;
  87. $product_batch_current_count = $product_batch_current_count > 0 ? $product_batch_current_count : 0;
  88. $product_batch_also_count = ($product_batch_also_count - $product_batch_current_count) > 0 ? $product_batch_also_count - $product_batch_current_count : 0;
  89. $list[$index]['product_batch_total_count'] = $product_batch_total_count;
  90. $list[$index]['product_batch_current_count'] = $product_batch_current_count;
  91. $list[$index]['product_batch_also_count'] = $product_batch_also_count;
  92. $list[$index]['product_batch_list'] = $product_batch_list;
  93. }
  94. return [
  95. 'code' => 0,
  96. 'msg' => 'success',
  97. 'data' => [
  98. 'row_count' => $count,
  99. 'page_count' => $pagination->pageCount,
  100. 'list' => $list,
  101. ],
  102. ];
  103. }
  104. }