ProductPresellDao.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\common\dao\store\product;
  12. use app\common\dao\BaseDao;
  13. use app\common\model\store\product\ProductPresell;
  14. use app\common\model\system\merchant\Merchant;
  15. use app\common\repositories\store\product\SpuRepository;
  16. use app\common\repositories\system\merchant\MerchantRepository;
  17. class ProductPresellDao extends BaseDao
  18. {
  19. protected function getModel(): string
  20. {
  21. return ProductPresell::class;
  22. }
  23. public function search(array $where)
  24. {
  25. $query = ProductPresell::hasWhere('product',function($query)use($where){
  26. $query->when(isset($where['product_show']) && $where['product_show'] !== '',function($query)use($where){
  27. $query->where('is_del',0)->where('mer_status',1);
  28. })
  29. ->when(isset($where['product_type']) && $where['product_type'] !== '',function($query)use($where){
  30. $query->where('product_type',2);
  31. })
  32. ->where('status',1);
  33. });
  34. $query->join('StoreSpu U', 'ProductPresell.product_presell_id = U.activity_id')->where('U.product_type', 2);
  35. $query->when(isset($where['product_presell_id']) && $where['product_presell_id'] !== '',function($query)use($where){
  36. $query->where('product_presell_id',$where['product_presell_id']);
  37. })
  38. ->when(isset($where['mer_id']) && $where['mer_id'] !== '',function($query)use($where){
  39. $query->where('ProductPresell.mer_id',$where['mer_id']);
  40. })
  41. ->when(isset($where['is_del']) && $where['is_del'] !== '',function($query)use($where){
  42. $query->where('ProductPresell.is_del',$where['is_del']);
  43. })
  44. ->when(isset($where['action_status']) && $where['action_status'] !== '',function($query)use($where){
  45. $query->where('ProductPresell.action_status',$where['action_status']);
  46. })
  47. ->when(isset($where['keyword']) && $where['keyword'] !== '',function($query)use($where){
  48. $query->whereLike('ProductPresell.store_name|ProductPresell.product_id|ProductPresell.product_presell_id',"%{$where['keyword']}%");
  49. })
  50. ->when(isset($where['product_status']) && $where['product_status'] !== '',function($query)use($where){
  51. if($where['product_status'] == -1){
  52. $query->where('ProductPresell.product_status','in',[-1,-2]);
  53. }else{
  54. $query->where('ProductPresell.product_status',$where['product_status']);
  55. }
  56. })
  57. ->when(isset($where['presell_type']) && $where['presell_type'] !== '',function($query)use($where){
  58. $query->where('ProductPresell.presell_type',$where['presell_type']);
  59. })
  60. ->when(isset($where['type']) && $where['type'] !== '',function($query)use($where){
  61. switch ($where['type']){
  62. case 0: //未开始
  63. if(isset($where['api_type'])){
  64. $query->where('product_status',1);
  65. }
  66. $query->where('action_status',1);
  67. $query->where(function($query){
  68. $query->whereTime('start_time','>',time())->whereOr(function ($query){
  69. $query->whereTime('start_time','<',time())->whereTime('end_time','>',time())->where(function($query){
  70. $query->where('ProductPresell.product_status','<>',1)->whereOr('ProductPresell.is_show','<>',1)->whereOr('ProductPresell.status','<>',1);
  71. });
  72. });
  73. });
  74. break;
  75. case 1: //进行中
  76. $query->where('action_status',1)
  77. ->whereTime('start_time','<=',time())
  78. ->whereTime('end_time','>',time())
  79. ->where('product_status',1)
  80. ->where('ProductPresell.status',1)
  81. ->where('ProductPresell.is_show',1);
  82. break;
  83. case 2: //已结束
  84. $query->where(function($query){
  85. $query->where('action_status',-1)->whereOr('end_time','<= TIME',time());
  86. });
  87. break;
  88. case 3: //已关闭
  89. $query->where(function($query){
  90. $query->where(function($query){
  91. $query->where('ProductPresell.presell_type',1)->whereTime('end_time','<',time());
  92. })->whereOr(function($query){
  93. $query->where('ProductPresell.presell_type',2)->whereTime('final_end_time','<',time());
  94. });
  95. });
  96. break;
  97. default:
  98. $query->where(function($query){
  99. $query->where(function($query){
  100. $query->where('ProductPresell.presell_type',1)->whereTime('end_time','>',time());
  101. })->whereOr(function($query){
  102. $query->where('ProductPresell.presell_type',2)->whereTime('final_end_time','>',time());
  103. });
  104. });
  105. break;
  106. }
  107. })
  108. ->when(isset($where['status']) && $where['status'] !== '',function($query)use($where){
  109. $query->where('ProductPresell.status',$where['status']);
  110. })
  111. ->when(isset($where['is_show']) && $where['is_show'] !== '',function($query)use($where){
  112. $query->where('ProductPresell.is_show',$where['is_show']);
  113. })
  114. ->when(isset($where['mer_name']) && $where['mer_name'] !== '',function($query)use($where){
  115. $make = app()->make(MerchantRepository::class);
  116. $mer_id = $make->search(['keyword' => $where['mer_name']])->column('mer_id');
  117. $query->whereIn('ProductPresell.mer_id',$mer_id);
  118. })
  119. ->when(isset($where['is_trader']) && $where['is_trader'] !== '',function($query)use($where){
  120. $make = app()->make(MerchantRepository::class);
  121. $mer_id = $make->search(['is_trader' => $where['is_trader']])->column('mer_id');
  122. $query->whereIn('ProductPresell.mer_id',$mer_id);
  123. })
  124. ->when(isset($where['us_status']) && $where['us_status'] !== '',function($query)use($where){
  125. if($where['us_status'] == 0) {
  126. $query->where('ProductPresell.is_show',0)->where('ProductPresell.status',1)->where('ProductPresell.product_status',1);
  127. }
  128. if($where['us_status'] == 1) {
  129. $query->where('ProductPresell.is_show',1)->where('ProductPresell.status',1)->where('ProductPresell.product_status',1);
  130. }
  131. if($where['us_status'] == -1) {
  132. $query->where(function($query){
  133. $query->where('ProductPresell.status',0)->whereOr('ProductPresell.product_status','<>',1);
  134. });
  135. }
  136. });
  137. $query->when(isset($where['mer_labels']) && $where['mer_labels'] !== '', function ($query) use ($where) {
  138. $query->whereLike('U.mer_labels', "%,{$where['mer_labels']},%");
  139. })
  140. ->when(isset($where['sys_labels']) && $where['sys_labels'] !== '', function ($query) use ($where) {
  141. $query->whereLike('U.sys_labels', "%,{$where['sys_labels']},%");
  142. })
  143. ->when(isset($where['star']),function($query)use($where){
  144. $query->when($where['star'] !== '', function ($query) use ($where) {
  145. $query->where('U.star', $where['star']);
  146. });
  147. $query->order('U.star DESC,U.rank DESC,ProductPresell.create_time DESC');
  148. });
  149. $query->where('ProductPresell.is_del',0);
  150. return $query;
  151. }
  152. /**
  153. * 移动端展示 条件
  154. * @return array
  155. * @author Qinii
  156. * @day 2020-10-19
  157. */
  158. public function actionShow()
  159. {
  160. return [
  161. 'product_show' => 1,
  162. //'product_status' => 1,
  163. 'status' => 1,
  164. 'is_show' => 1,
  165. 'api_type' => 1,
  166. 'is_del' => 0,
  167. ];
  168. }
  169. /**
  170. * 检查并更新过期活动的状态
  171. * 该方法用于定期检查所有活动的结束时间,如果活动已经结束,则将其状态设置为失效(-1)。
  172. * 同时,它也会更新关联的SPU(Special Product Unit)的状态,将这些SPU的活动状态设置为失效。
  173. * 这样做的目的是为了维护数据库中活动和SPU状态的准确性,确保只有当前有效的活动和SPU被展示或参与。
  174. */
  175. public function valActiveStatus()
  176. {
  177. // 根据当前时间,查询所有已经结束的活动,这些活动的状态为1(进行中)
  178. $query = $this->getModel()::getDB()->whereTime('end_time','<=',time())->where('action_status',1);
  179. // 获取这些活动的ID
  180. $id = $query->column($this->getPk());
  181. // 如果查询到了已结束的活动ID
  182. if($id){
  183. // 更新这些活动的状态为-1(失效)
  184. $this->getModel()::getDB()->where($this->getPk(),'in',$id)->update(['action_status' => -1]);
  185. // 准备更新SPU状态的条件,即活动类型为2且活动ID在已结束的活动ID列表中
  186. $where = [
  187. 'product_type' => 2,
  188. 'activity_ids' => $id
  189. ];
  190. // 更新所有符合条件的SPU的状态为0(失效)
  191. app()->make(SpuRepository::class)->getSearch($where)->update(['status' => 0]);
  192. }
  193. }
  194. /**
  195. * 软删除商户的所有商品
  196. * @param $merId
  197. * @author Qinii
  198. * @day 5/15/21
  199. */
  200. public function clearProduct($merId)
  201. {
  202. $this->getModel()::getDb()->where('mer_id', $merId)->update(['is_del' => 1]);
  203. }
  204. }