SearchAllGoodsForm.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\plugins\product_traceability\models\form;
  8. use app\models\Cat;
  9. use app\models\Goods;
  10. use app\models\GoodsCat;
  11. use app\models\MchCat;
  12. use app\models\MchGoodsCat;
  13. use app\plugins\product_traceability\models\ProductBatch;
  14. use app\plugins\product_traceability\models\ProductBatchProcess;
  15. use app\plugins\product_traceability\models\ProductBatchProcessLog;
  16. use app\plugins\product_traceability\models\ProductBatchProcessLogSheet;
  17. use app\plugins\product_traceability\models\ProductBatchProcessSheet;
  18. use yii\base\Model;
  19. use yii\helpers\Json;
  20. use app\jobs\SyncMdGoodsJob;
  21. use app\utils\Tools;
  22. class SearchAllGoodsForm extends Model
  23. {
  24. public $id;
  25. public $store_id;
  26. public $process_user_id;
  27. public $product_batch_id;
  28. public $process_name;
  29. public $sort;
  30. public $is_change_sheet;
  31. public $form_list;
  32. /**
  33. * 初始化
  34. */
  35. public function init()
  36. {
  37. $this->store_id = get_store_id();
  38. }
  39. /**
  40. * @return array
  41. */
  42. public function rules()
  43. {
  44. return [
  45. [['store_id', 'process_user_id', 'product_batch_id','form_list', 'process_name', 'sort'], 'required'],
  46. [['store_id', 'id', 'process_user_id', 'product_batch_id', 'is_change_sheet' ,'sort', 'id'], 'integer'],
  47. [['process_name',], 'string', 'max' => 255],
  48. [['form_list',], 'safe',],
  49. [['product_batch_id', 'is_change_sheet'], 'default', 'value' => 0],
  50. ];
  51. }
  52. public function attributeLabels()
  53. {
  54. return [
  55. 'id' => 'ID',
  56. 'store_id' => '商城id',
  57. 'process_name' => '流程名称',
  58. 'process_user_id' => '流程操作用户',
  59. 'product_batch_id' => '批次id',
  60. 'created_at' => '创建时间',
  61. 'updated_at' => '更新时间',
  62. 'is_delete' => 'Is Delete',
  63. 'sort' => 'sort',
  64. ];
  65. }
  66. /**
  67. * @param array $arr | status 状态 name 名称 cat_id 分类id mch_id 入驻商 is_verify 审核 select 查询数据
  68. * @return array
  69. */
  70. public static function getList ($arr = [])
  71. {
  72. $query = Goods::find()->alias('g')
  73. ->where(['g.store_id' => get_store_id(), 'g.is_delete' => 0, 'g.md_food_id' => 0])->andWhere(['not like', 'g.name', '当面付']);
  74. if (isset($arr['status']) && $arr['status'] > -1) {
  75. $query->andWhere([
  76. 'g.status' => $arr['status']
  77. ]);
  78. }
  79. if (!empty($arr['is_verify_card'])) {
  80. $query->andWhere([
  81. 'g.product_type' => 0,
  82. 'g.status' => 1
  83. ]);
  84. } else {
  85. if (isset($arr['product_type']) && in_array($arr['product_type'], Goods::$validProductType)) {
  86. $query->andWhere([
  87. 'g.product_type' => $arr['product_type']
  88. ]);
  89. }
  90. }
  91. //是否是云仓商品
  92. if (isset($arr['is_cloud']) && $arr['is_cloud'] ) {
  93. $query->andWhere([ '>', 'g.cloud_goods_id' , 0 ]);
  94. }
  95. if (isset($arr['name']) && !empty($arr['name'])) {
  96. $query->andWhere([
  97. 'like',
  98. 'g.name',
  99. $arr['name']
  100. ]);
  101. }
  102. if (isset($arr['cat_id']) && $arr['cat_id'] > 0) {
  103. if ($arr['mch_id'] > 0) {
  104. $query->leftJoin(['mgc' => MchGoodsCat::tableName()], 'g.id = mgc.goods_id')->andWhere([
  105. 'mgc.cat_id' => MchCat::getCatId($arr['cat_id'])
  106. ])->groupBy('mgc.goods_id');
  107. } else {
  108. $query->leftJoin(['gc' => GoodsCat::tableName()], 'g.id = gc.goods_id')->andWhere([
  109. 'gc.cat_id' => Cat::getCatId($arr['cat_id'])
  110. ])->groupBy('gc.goods_id');
  111. }
  112. }
  113. if (isset($arr['mch_cat_id']) && $arr['mch_cat_id'] > 0) {
  114. $query->leftJoin(['mgc' => MchGoodsCat::tableName()], 'g.id = mgc.goods_id')->andWhere([
  115. 'mgc.cat_id' => MchCat::getCatId($arr['mch_cat_id'])
  116. ])->groupBy('mgc.goods_id');
  117. }
  118. // 根据入驻商查询 0 平台 -1 全部入驻商 >0 对应入驻商
  119. if (empty($arr['mch_id'])) {
  120. $query->andWhere(['g.mch_id' => 0]);
  121. } elseif(isset($arr['mch_id']) && $arr['mch_id'] == -1) {
  122. $query->andWhere([
  123. '>',
  124. 'g.mch_id',
  125. 0
  126. ]);
  127. } elseif (isset($arr['mch_id']) && $arr['mch_id'] > 0){
  128. $query->andWhere(['g.mch_id' => $arr['mch_id']]);
  129. }
  130. // 获取入驻商待审核商品
  131. if (isset($arr['is_verify'])) {
  132. $query->andWhere([
  133. 'g.is_verify' => $arr['is_verify']
  134. ]);
  135. }
  136. if (!empty($arr['select'])) {
  137. $select = $arr['select'];
  138. } else {
  139. $select = ['g.id', 'g.name', 'g.status', 'g.service',
  140. 'g.updated_at', 'g.virtual_sales', 'g.is_verify', 'g.is_negotiable',
  141. 'g.price', 'g.goods_num', 'g.sort' ,'g.cover_pic', 'g.original_price',
  142. 'g.quick_purchase', 'g.attr', 'g.mch_id', 'g.detail', 'g.use_attr', 'g.product_type', 'g.is_recommend'];
  143. }
  144. $query->with(['mch'])->select($select)
  145. ->orderBy(['g.sort' => SORT_DESC, 'g.id' => SORT_DESC]);
  146. // return $query->createCommand()->getRawSql();
  147. $pagination = pagination_make($query);
  148. $pagination['data'] = $pagination['list'];
  149. foreach($pagination['data'] as $k => $v) {
  150. $goods_cat = GoodsCat::find()->alias('gc')
  151. ->leftJoin(['c' => Cat::tableName()], 'gc.cat_id=c.id')
  152. ->where([ 'gc.goods_id' => $v['id'] ])
  153. ->select(['c.name'])
  154. ->asArray()
  155. ->all();
  156. $pagination['data'][$k]['cat'] = $goods_cat;
  157. }
  158. unset($pagination['list']);
  159. $book_goods_ids = Goods::checkBookGoods();
  160. return [
  161. 'code' => 0,
  162. 'msg' => 'success',
  163. 'book_goods_ids' => $book_goods_ids,
  164. 'data' => $pagination
  165. ];
  166. }
  167. }