StoreSeckillActiveRepository.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  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\repositories\store;
  12. use app\common\dao\store\StoreSeckillActiveDao;
  13. use app\common\repositories\BaseRepository;
  14. use app\common\repositories\store\order\StoreOrderProductRepository;
  15. use app\common\repositories\store\order\StoreOrderRepository;
  16. use app\common\repositories\store\product\ProductRepository;
  17. use app\common\repositories\store\product\SpuRepository;
  18. use app\common\repositories\user\UserRepository;
  19. use app\controller\api\store\product\StoreSpu;
  20. use think\db\exception\DataNotFoundException;
  21. use think\db\exception\DbException;
  22. use think\db\exception\ModelNotFoundException;
  23. use think\exception\ValidateException;
  24. use think\facade\Db;
  25. class StoreSeckillActiveRepository extends BaseRepository
  26. {
  27. /**
  28. * @var StoreSeckillActiveDao
  29. */
  30. protected $dao;
  31. protected $filed = 'seckill_active_id,name,seckill_time_ids,start_day,end_day,once_pay_count,all_pay_count,product_category_ids,status,active_status,product_count,merchant_count,create_time,update_time';
  32. /**
  33. * StoreSeckillActiveDao constructor.
  34. * @param StoreSeckillActiveDao $dao
  35. */
  36. public function __construct(StoreSeckillActiveDao $dao)
  37. {
  38. $this->dao = $dao;
  39. }
  40. /**
  41. * 获取秒杀活动列表
  42. * @param array $where
  43. * @param int $page
  44. * @param int $limit
  45. * @return array
  46. * FerryZhao 2024/4/11
  47. */
  48. public function getList(array $where, int $page, int $limit, array $append = [])
  49. {
  50. $query = $this->dao->search($where)->append($append);
  51. $count = $query->count();
  52. $list = $query->page($page, $limit)->setOption('field', [])->field($this->filed)->select();
  53. return compact('count', 'list');
  54. }
  55. /**
  56. * 返回所有列表
  57. * @return
  58. * FerryZhao 2024/4/12
  59. */
  60. public function getActiveAll()
  61. {
  62. return $this->dao->getSearch([])->where('status', '=', 1)->where('active_status', '<>', -1)->field('name as lable,seckill_active_id as value,active_status')->select();
  63. }
  64. /**
  65. * 创建秒杀活动
  66. * @param $data
  67. * @return object
  68. * FerryZhao 2024/4/11
  69. */
  70. public function create($data)
  71. {
  72. //场次ID
  73. if (isset($data['seckill_time_ids']) && !empty($data['seckill_time_ids'])) {
  74. $data['seckill_time_ids'] = implode(',', $data['seckill_time_ids']);
  75. }
  76. //活动平台商品一级分类
  77. if (isset($data['product_category_ids']) && !empty($data['product_category_ids'])) {
  78. $data['product_category_ids'] = implode(',', $data['product_category_ids']);
  79. }
  80. $activity = app()->make(StoreActivityRepository::class);
  81. $result = $this->dao->create($data);
  82. if (!$result) {
  83. throw new ValidateException('活动添加失败');
  84. }
  85. if (isset($data['border_pic']) && !empty($data['border_pic'])) {
  86. //边框图
  87. $activity->saveByType([
  88. 'activity_name' => $data['name'],
  89. 'start_time' => $data['start_day'],
  90. 'end_time' => $data['end_day'],
  91. 'pic' => $data['border_pic'],
  92. 'link_id' => $result->seckill_active_id,
  93. 'activity_type' => $activity::ACTIVITY_TYPE_BORDER
  94. ], 1);
  95. }
  96. return $result;
  97. }
  98. /**
  99. * 添加商品修改商家数量和商品数量
  100. * @return true|void
  101. * FerryZhao 2024/4/24
  102. */
  103. public function updateActiveChart($activeId)
  104. {
  105. $productRepository = app()->make(ProductRepository::class);
  106. $productCount = $productRepository->getSearch([])->where(['active_id' => $activeId, 'is_del' => 0])->count();
  107. $merchantCount = $productRepository->getSearch([])->where(['active_id' => $activeId, 'is_del' => 0])->group('mer_id')->count();
  108. $this->dao->update($activeId, ['product_count' => $productCount, 'merchant_count' => $merchantCount]);
  109. }
  110. /**
  111. * 编辑秒杀活动
  112. * @param int $activeId 活动ID
  113. * @param $data
  114. * @return void
  115. * FerryZhao 2024/4/12
  116. */
  117. public function updateActive(int $activeId, $data)
  118. {
  119. if (!$activeId) {
  120. throw new ValidateException('活动ID参数错误');
  121. }
  122. $activeInfo = $this->dao->get($activeId);
  123. if (!$activeInfo) {
  124. throw new ValidateException('数据不存在');
  125. }
  126. //场次ID
  127. if (isset($data['seckill_time_ids']) && !empty($data['seckill_time_ids'])) {
  128. $data['seckill_time_ids'] = implode(',', $data['seckill_time_ids']);
  129. }
  130. //活动平台商品一级分类
  131. if (isset($data['product_category_ids']) && !empty($data['product_category_ids'])) {
  132. $data['product_category_ids'] = implode(',', $data['product_category_ids']);
  133. }
  134. $result = $activeInfo->save($data);
  135. if (!$result) {
  136. throw new ValidateException('编辑失败');
  137. }
  138. $activity = app()->make(StoreActivityRepository::class);
  139. if (isset($data['border_pic']) && !empty($data['border_pic'])) {
  140. //边框图
  141. $activity->saveByType([
  142. 'activity_name' => $data['name'],
  143. 'start_time' => $data['start_day'],
  144. 'end_time' => $data['end_day'],
  145. 'pic' => $data['border_pic'],
  146. 'link_id' => $activeId,
  147. 'activity_type' => $activity::ACTIVITY_TYPE_BORDER
  148. ], 1);
  149. } else {
  150. //删除边框
  151. $activity->deleteSeckll($activeId);
  152. }
  153. }
  154. /**
  155. * 编辑秒杀活动状态
  156. * @param $id
  157. * @param $status
  158. * @return void
  159. * FerryZhao 2024/4/12
  160. */
  161. public function updateStatus($id, $status)
  162. {
  163. if (!$this->dao->get($id)) {
  164. throw new ValidateException('数据不存在');
  165. }
  166. $productRepository = app()->make(ProductRepository::class);
  167. $storeSpu = app()->make(SpuRepository::class);
  168. Db::transaction(function () use ($productRepository, $storeSpu,$id,$status) {
  169. $result = $this->dao->update($id, ['status' => $status]);
  170. $updateSpu = $productRepository->getSearch([])->where(['active_id'=>$id])->update(['is_used' => $status]);
  171. $updateProduct = $storeSpu->getSearch([])->where(['activity_id'=>$id])->update(['status' => $status]);
  172. });
  173. return true;
  174. }
  175. /**
  176. * 删除秒杀活动
  177. * @param $activeId 秒杀活动ID
  178. * @return void
  179. * FerryZhao 2024/4/12
  180. * @throws DataNotFoundException
  181. * @throws DbException
  182. * @throws ModelNotFoundException
  183. */
  184. public function deleteActive($activeId)
  185. {
  186. $activeInfo = $this->dao->get($activeId);
  187. if (!$activeInfo) {
  188. throw new ValidateException('数据不存在');
  189. }
  190. $deleteActive = $this->dao->update($activeId, ['delete_time' => time()]);
  191. $deleteProduct = app()->make(ProductRepository::class)->getSearch([])->where('active_id', $activeId)->update(['is_del' => 1]);
  192. app()->make(StoreActivityRepository::class)->deleteSeckll($activeId);
  193. return compact('deleteActive', 'deleteProduct');
  194. }
  195. /**
  196. * 平台管理端统计面板
  197. * @param $id
  198. * @param $merId
  199. * @return array[]
  200. * FerryZhao 2024/4/22
  201. */
  202. public function chartPanel($id, $merId = null): array
  203. {
  204. $merchantWhere = [
  205. 'paid' => 1,
  206. ];
  207. if ($merId) {
  208. $merchantWhere['mer_id'] = $merId;
  209. }
  210. //初始化
  211. $data = [
  212. 'orders_people_count' => 0,
  213. 'pay_order_money' => 0,
  214. 'pay_order_people_count' => 0,
  215. 'pay_order_count' => 0
  216. ];
  217. $storeOrderRepository = app()->make(StoreOrderRepository::class);
  218. $storeOrderProductRepository = app()->make(StoreOrderProductRepository::class);
  219. //活动对应的订单商品表
  220. $orderProductOrderIds = $storeOrderProductRepository->getSearch([])->where([
  221. 'activity_id' => $id,
  222. 'product_type' => 1
  223. ])->column('order_id');
  224. if (!empty($orderProductOrderIds)) {
  225. $data['orders_people_count'] = $storeOrderRepository->getSearch([])->where($merchantWhere)->where('status','>',-1)->whereIn('order_id', $orderProductOrderIds)->group('uid')->count();//下单人数
  226. $data['pay_order_money'] = $storeOrderRepository->getSearch([])->where($merchantWhere)->where('status','>',-1)->where(['paid' => 1])->whereIn('order_id', $orderProductOrderIds)->sum('pay_price');;//支付订单金额
  227. $data['pay_order_people_count'] = $storeOrderRepository->getSearch([])->where($merchantWhere)->where('status','>',-1)->where(['paid' => 1])->whereIn('order_id', $orderProductOrderIds)->group('uid')->count();//支付人数
  228. $data['pay_order_count'] = $storeOrderRepository->getSearch([])->where($merchantWhere)->where('status','>',-1)->where(['paid' => 1])->whereIn('order_id', $orderProductOrderIds)->group('order_id')->count();//支付订单数
  229. }
  230. return [
  231. [
  232. 'className' => 'el-icon-user-solid',
  233. 'count' => $data['orders_people_count'],
  234. 'field' => '人',
  235. 'name' => '下单人数'
  236. ],
  237. [
  238. 'className' => 'el-icon-s-order',
  239. 'count' => (float)$data['pay_order_money'],
  240. 'field' => '元',
  241. 'name' => '支付订单额'
  242. ],
  243. [
  244. 'className' => 'el-icon-s-check',
  245. 'count' => $data['pay_order_people_count'],
  246. 'field' => '人',
  247. 'name' => '支付人数'
  248. ],
  249. [
  250. 'className' => 'el-icon-s-order',
  251. 'count' => $data['pay_order_count'],
  252. 'field' => '笔',
  253. 'name' => '支付订单数'
  254. ]
  255. ];
  256. }
  257. /**
  258. * 活动参与人列表统计
  259. * @param $activeId
  260. * @param $merId
  261. * @param $where
  262. * @param int $page
  263. * @param int $limit
  264. * @return array
  265. * FerryZhao 2024/4/28
  266. */
  267. public function chartPeople($activeId, $merId = null, $where, int $page = 1, int $limit = 10): array
  268. {
  269. $result = $this->validateProduct($activeId, $merId);
  270. if (!$result) {
  271. return ['count' => 0, 'list' => []];
  272. }
  273. return $this->dao->chartPeople($activeId, $merId, $where, $page, $limit);
  274. }
  275. /**
  276. * 活动订单统计列表
  277. * @param $activeId
  278. * @param $merId
  279. * @param $where
  280. * @param int $page
  281. * @param int $limit
  282. * @return array|null
  283. * FerryZhao 2024/4/28
  284. */
  285. public function chartOrder($activeId, $merId = null, $where, int $page = 1, int $limit = 10): ?array
  286. {
  287. $result = $this->validateProduct($activeId, $merId);
  288. if (!$result) {
  289. return ['count' => 0, 'list' => []];
  290. }
  291. $statusWhere = app()->make(StoreOrderRepository::class)->getOrderType($where['status']);
  292. unset($where['status']);
  293. return $this->dao->chartOrder($activeId, $merId, $where, $statusWhere, $page, $limit);
  294. }
  295. /**
  296. * 活动商品统计列表
  297. * @param $activeId
  298. * @param $merId
  299. * @param $where
  300. * @param int $page
  301. * @param int $limit
  302. * @return array|null
  303. * FerryZhao 2024/4/28
  304. */
  305. public function chartProduct($activeId, $merId = null, $where, int $page = 1, int $limit = 10): ?array
  306. {
  307. $result = $this->validateProduct($activeId, $merId);
  308. if (!$result) {
  309. return ['count' => 0, 'list' => []];
  310. }
  311. return $this->dao->chartProduct($activeId, $merId, $where, $page, $limit);
  312. }
  313. /**
  314. * 公用校验商品
  315. * @param $activeId
  316. * @param $merId
  317. * @return false
  318. * FerryZhao 2024/4/28
  319. */
  320. public function validateProduct($activeId, $merId): bool
  321. {
  322. $productWhere = [
  323. 'active_id' => $activeId,
  324. 'product_type' => 1
  325. ];
  326. if ($merId) {
  327. $productWhere['mer_id'] = $merId;
  328. }
  329. $productIds = app()->make(ProductRepository::class)->getSearch([])->where($productWhere)->whereNotNull('active_id')->find();
  330. if (empty($productIds)) {
  331. return false;
  332. } else {
  333. return true;
  334. }
  335. }
  336. }