StoreSeckillActiveDao.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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;
  12. use app\common\model\store\StoreSeckillActive;
  13. use app\common\dao\BaseDao;
  14. use app\common\repositories\store\order\StoreOrderProductRepository;
  15. use app\common\repositories\store\order\StoreOrderRepository;
  16. use app\common\repositories\store\product\ProductAttrValueRepository;
  17. use app\common\repositories\store\product\ProductRepository;
  18. use app\common\repositories\store\product\SpuRepository;
  19. use app\common\repositories\store\StoreSeckillActiveRepository;
  20. use think\facade\Log;
  21. class StoreSeckillActiveDao extends BaseDao
  22. {
  23. /**
  24. *
  25. * @return string
  26. * @author Qinii
  27. * @day 2020-07-30
  28. */
  29. public function getModel(): string
  30. {
  31. return StoreSeckillActive::class;
  32. }
  33. /**
  34. * 搜索
  35. * @param array $where
  36. * @return mixed
  37. * FerryZhao 2024/4/12
  38. */
  39. public function search(array $where)
  40. {
  41. $query = $this->getModel()::getDB()
  42. ->when(isset($where['name']) && $where['name'] !== '', function ($query) use ($where) {
  43. $query->where('name', 'like', '%' . $where['name'] . '%');
  44. })
  45. ->when(isset($where['active_status']) && $where['active_status'] !== '', function ($query) use ($where) {
  46. $query->where('active_status', $where['active_status']);
  47. })
  48. ->when(isset($where['seckill_active_status']) && $where['seckill_active_status'] !== '', function ($query) use ($where) {
  49. $query->where('status', $where['seckill_active_status']);
  50. })
  51. ->when(isset($where['seckill_active_id']) && $where['seckill_active_id'] !== '', function ($query) use ($where) {
  52. $query->where('seckill_active_id', $where['seckill_active_id']);
  53. })
  54. ->when(isset($where['active_name']) && $where['active_name'] !== '', function ($query) use ($where) {
  55. $query->whereLike('name', "%{$where['active_name']}%");
  56. })
  57. ->when(isset($where['active_name']) && $where['active_name'] !== '', function ($query) use ($where) {
  58. $query->whereLike('name', "%{$where['active_name']}%");
  59. })
  60. ->when(!isset($where['sign']), function ($query) use ($where) {
  61. $query->where('sign', 1);
  62. })
  63. ->when(isset($where['date']) && $where['date'], function ($query) use ($where) {
  64. $timeWhere = explode('-', $where['date']);
  65. $query->whereTime('start_day', '>=', $timeWhere[0]);
  66. $query->whereTime('end_day', '<=', date('Y-m-d', strtotime($timeWhere[1]) + 24 * 60 * 60));
  67. });
  68. $query->order('status desc,seckill_active_id desc');
  69. return $query;
  70. }
  71. /**
  72. * 检测活动状态关闭spu
  73. * @return void
  74. * FerryZhao 2024/4/19
  75. */
  76. public function valActiveStatus()
  77. {
  78. try {
  79. $storeSeckillActiveRepository = app()->make(StoreSeckillActiveRepository::class);
  80. $changeStartIds = $this->getModel()::getDB()->where(['active_status' => 0])->whereTime('start_day', '<', date('Y-m-d H:i:s', time()))->whereTime('end_day', '>', time())->column('seckill_active_id');
  81. if (!empty($changeStartIds)) {
  82. $storeSeckillActiveRepository->getSearch([])->whereIn('seckill_active_id', $changeStartIds)->update(['active_status' => 1]);
  83. }
  84. $changeStartingIds = $this->getModel()::getDB()->where(['active_status' => 1])->whereTime('end_day', '<', time())->column('seckill_active_id');
  85. if (!empty($changeStartingIds)) {
  86. $storeSeckillActiveRepository->getSearch([])->whereIn('seckill_active_id', $changeStartingIds)->update(['active_status' => '-1']);
  87. app()->make(SpuRepository::class)->getSearch([
  88. 'product_type' => 1,
  89. 'activity_ids' => $changeStartingIds
  90. ])->update(['status' => 0]);
  91. }
  92. } catch (\Exception $e) {
  93. Log::error('检测活动状态关闭spu失败,' . $e->getMessage());
  94. }
  95. }
  96. /**
  97. * 不同状态商品
  98. * @param $status
  99. * @return mixed
  100. * @author Qinii
  101. * @day 2020-08-19
  102. */
  103. public function getStatus($status)
  104. {
  105. $day = date('Y-m-d', time());
  106. $_h = date('H', time());
  107. $query = $this->getModel()::getDB();
  108. if ($status == 1) //未开始
  109. $query->where('status', '<>', -1)->where(function ($query) use ($day, $_h) {
  110. $query->whereTime('start_day', '>', $day)->whereOr(function ($query) use ($day, $_h) {
  111. $query->whereTime('start_day', '<=', $day)->where('start_time', '>', $_h);
  112. });
  113. });
  114. if ($status == 2)//进行中
  115. $query->where('status', 1)
  116. ->whereTime('start_day', '<=', $day)->whereTime('end_day', '>', $day)
  117. ->where('start_time', '<=', $_h)->where('end_time', '>', $_h);
  118. if ($status == 3) //结束
  119. $query->where('status', -1)->whereOr(function ($query) use ($day, $_h) {
  120. $query->whereTime('end_day', '<', $day)
  121. ->whereOr(function ($query) use ($day, $_h) {
  122. $query->whereTime('start_day', '<=', $day)->whereTime('end_day', '>=', $day)->where('end_time', '<=', $_h);
  123. });
  124. });
  125. return $query;
  126. }
  127. /**
  128. * 活动参与人列表统计
  129. * @param $activeId
  130. * @param $merId
  131. * @param $where
  132. * @param int $page
  133. * @param int $limit
  134. * @return void
  135. * FerryZhao 2024/4/28
  136. */
  137. public function chartPeople($activeId, $merId = null, $where, int $page = 1, int $limit = 10)
  138. {
  139. $storeOrderRepository = app()->make(StoreOrderRepository::class);
  140. $query = $storeOrderRepository->getSearch([])->alias('ORDERA')
  141. ->leftJoin('StoreOrderProduct ORDERB', 'ORDERA.order_id = ORDERB.order_id')
  142. ->leftJoin('User USER', 'USER.uid = ORDERA.uid')
  143. ->when($merId, function ($query) use ($merId) {
  144. $query->where('ORDERA.mer_id', '=', $merId);
  145. })
  146. ->when(isset($where['keyword']) && $where['keyword'], function ($query) use ($where) {
  147. $query->whereLike('USER.uid|USER.nickname|USER.phone', "%{$where['keyword']}%");
  148. })
  149. ->when(isset($where['date']) && $where['date'], function ($query) use ($where) {
  150. getModelTime($query, $where['date'], 'ORDERA.create_time');
  151. })
  152. ->where([
  153. 'ORDERB.activity_id' => $activeId,
  154. 'ORDERA.paid' => 1,
  155. 'ORDERA.activity_type' => 1,
  156. ])
  157. ->where('ORDERA.status','>',-1)
  158. ->group('ORDERA.uid');
  159. $count = $query->count();
  160. $list = $query->page($page, $limit)
  161. ->field('sum(ORDERA.total_num) as sum_total_num,sum(ORDERA.pay_price) as sum_pay_price,USER.nickname,USER.uid,count(ORDERA.order_id) as order_count,max(ORDERA.create_time) as create_time,ORDERA.is_del,ORDERA.paid,ORDERA.order_type')
  162. ->select();
  163. return compact('count', 'list');
  164. }
  165. /**
  166. * 活动订单统计列表
  167. * @param $activeId
  168. * @param $merId
  169. * @param $where
  170. * @param int $page
  171. * @param int $limit
  172. * @return void
  173. * FerryZhao 2024/4/28
  174. */
  175. public function chartOrder($activeId, $merId = null, $where, $statusWhere = [], int $page = 1, int $limit = 10)
  176. {
  177. $storeOrderRepository = app()->make(StoreOrderRepository::class);
  178. $orderWhere = [
  179. 'ORDERB.activity_id' => $activeId,
  180. ];
  181. if (isset($where['status']) && $where['status'] != '') {
  182. $orderWhere['StoreOrder.status'] = $where['status'];
  183. }
  184. $query = $storeOrderRepository->getSearch([])->alias('StoreOrder')
  185. ->leftJoin('User USER', 'StoreOrder.uid = USER.uid')
  186. ->leftJoin('StoreOrderProduct ORDERB', 'StoreOrder.order_id = ORDERB.order_id')
  187. ->when(isset($where['keyword']) && $where['keyword'], function ($query) use ($where) {
  188. $query->whereLike('USER.uid|USER.nickname|USER.phone', "%{$where['keyword']}%");
  189. })
  190. ->when($merId, function ($query) use ($merId) {
  191. $query->where('StoreOrder.mer_id', '=', $merId);
  192. })
  193. ->when(isset($where['date']) && $where['date'], function ($query) use ($where) {
  194. getModelTime($query, $where['date'], 'StoreOrder.create_time');
  195. })
  196. ->where($orderWhere)->where($statusWhere);
  197. $count = $query->count();
  198. $list = $query->page($page, $limit)
  199. ->field('StoreOrder.order_sn,USER.nickname,USER.uid,StoreOrder.status,StoreOrder.pay_price,StoreOrder.total_num,StoreOrder.create_time,StoreOrder.pay_time,StoreOrder.is_del,StoreOrder.paid,StoreOrder.order_type')
  200. ->order('StoreOrder.order_id desc')
  201. ->select();
  202. return compact('count', 'list');
  203. }
  204. /**
  205. * 活动商品统计列表
  206. * @param $activeId
  207. * @param $merId
  208. * @param $where
  209. * @param int $page
  210. * @param int $limit
  211. * @return void
  212. * FerryZhao 2024/4/28
  213. */
  214. public function chartProduct($activeId, $merId = null, $where, int $page = 1, int $limit = 10)
  215. {
  216. $productRepository = app()->make(ProductRepository::class);
  217. $storeOrderRepository = app()->make(StoreOrderRepository::class);
  218. $storeOrderProductRepository = app()->make(StoreOrderProductRepository::class);
  219. $productAttrValueRepository = app()->make(ProductAttrValueRepository::class);
  220. $productWhere = [
  221. 'product_type' => 1,
  222. 'seckill_active_id' => $activeId
  223. ];
  224. if (isset($where['keyword']) && $where['keyword']) {
  225. $productWhere['keyword'] = $where['keyword'];
  226. }
  227. $with = ['attr', 'attrValue', 'merCateId.category', 'storeCategory', 'content', 'seckillActive',
  228. 'merchant' => function ($query) {
  229. $query->with(['typeName', 'categoryName'])->field('mer_id,category_id,type_id,mer_avatar,mer_name,is_trader');
  230. },
  231. ];
  232. $query = $productRepository->search($merId, $productWhere)->with($with);
  233. $count = $query->count();
  234. $filed = 'Product.product_id,Product.active_id,Product.mer_id,brand_id,unit_name,spec_type,mer_status,rate,reply_count,store_info,cate_id,Product.image,slider_image,Product.store_name,Product.keyword,Product.sort,Product.is_show,Product.sales,Product.price,extension_type,refusal,cost,U.ot_price,stock,is_gift_bag,Product.care_count,Product.status,is_used,Product.create_time,Product.product_type,old_product_id,integral_total,integral_price_total,mer_labels,Product.is_good,Product.is_del,type,param_temp_id,mer_svip_status,svip_price,svip_price_type';
  235. $list = $query->page($page, $limit)->setOption('field', [])->where(['Product.is_del' => 0, 'Product.delete' => 0])->field($filed)->select();
  236. foreach ($list as &$item){
  237. $item['sales'] = $storeOrderRepository->seckillOrderCounut($item['active_id'], $item['product_id']);
  238. $item['stock'] = $productAttrValueRepository->getSearch([])->where(['product_id'=>$item['product_id']])->sum('stock');
  239. }
  240. return compact('count', 'list');
  241. }
  242. }