截流自动化的商城平台
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

SeckillGoodsLogic.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. <?php
  2. namespace app\shop\logic\seckill;
  3. use app\common\basics\Logic;
  4. use app\common\model\seckill\SeckillGoods;
  5. use app\common\model\seckill\SeckillTime;
  6. use app\common\model\goods\Goods;
  7. use app\common\server\UrlServer;
  8. use think\facade\Db;
  9. class SeckillGoodsLogic extends Logic
  10. {
  11. /**
  12. * 统计
  13. */
  14. public static function statistics($shop_id)
  15. {
  16. // 秒杀中商品
  17. $where = [
  18. ['sg.del', '=', 0],
  19. ['sg.review_status', '=', 1],
  20. ['shop_id', '=', $shop_id],
  21. ];
  22. $lists = SeckillGoods::alias('sg')
  23. ->leftJoin('seckill_time st', 'st.id=sg.seckill_id')
  24. ->field('sg.goods_id,sg.start_date,sg.end_date,st.start_time,st.end_time')
  25. ->where($where)
  26. ->group('sg.goods_id,sg.start_date,sg.end_date,st.start_time,st.end_time')
  27. ->order(['sg.id' => 'desc'])
  28. ->select()
  29. ->toArray();
  30. $now = time();
  31. $now_date = date('Y-m-d', $now);
  32. $unSeckillCount = 0;
  33. foreach($lists as $key => $item) { // 检验是否在秒杀中
  34. $start_date_time = strtotime($item['start_date']. ' 00:00:00');
  35. $end_date_time = strtotime($item['end_date']. ' 23:59:59');
  36. // 日期校验
  37. if($now >= $start_date_time && $now <= $end_date_time) {
  38. $start_time = strtotime($now_date. ' '. $item['start_time']);
  39. $end_time = strtotime($now_date. ' '. $item['end_time']);
  40. if($now >= $start_time && $now <= $end_time) {
  41. // 秒杀中的时段,无需处理
  42. }else{
  43. unset($lists[$key]); // 未在秒杀时段
  44. ++$unSeckillCount ;
  45. }
  46. }else{
  47. unset($lists[$key]); // 未在秒杀时间日期
  48. ++$unSeckillCount;
  49. }
  50. }
  51. $seckillCount = count($lists);
  52. // 待审核
  53. $waitReview = SeckillGoods::where(['del'=>0, 'review_status'=>0, 'shop_id'=>$shop_id])->group('seckill_id,goods_id,start_date,end_date')->count();
  54. // 审核拒绝
  55. $refuseReview = SeckillGoods::where(['del'=>0, 'review_status'=>2, 'shop_id'=>$shop_id])->group('seckill_id,goods_id,start_date,end_date')->count();
  56. //
  57. return [
  58. 'unSeckillCount' => $unSeckillCount,
  59. 'seckillCount' => $seckillCount,
  60. 'waitReview' => $waitReview,
  61. 'refuseReview' => $refuseReview,
  62. ];
  63. }
  64. public static function getTimeAll(){
  65. $time_list = SeckillTime::where(['del'=>0])->order('start_time asc')->select()->toArray();
  66. foreach ($time_list as &$item){
  67. $item['time'] = $item['start_time'].' ~ '.$item['end_time'];
  68. }
  69. return $time_list;
  70. }
  71. public static function addGoods($post){
  72. try{
  73. $now = time();
  74. $add_data = [];
  75. // 开始日期结束日期处理
  76. $start_end = explode('~', $post['start_end']);
  77. foreach ($post['item'] as $item){
  78. $add_data[] =[
  79. 'seckill_id' => $post['seckill_id'],
  80. 'goods_id' => $item['goods_id'],
  81. 'item_id' => $item['item_id'],
  82. 'price' => $item['price'],
  83. 'create_time' => $now,
  84. 'sales_sum' => 0,
  85. 'update_time' => $now,
  86. 'del' => 0,
  87. 'shop_id' => $post['shop_id'],
  88. 'start_date' => trim($start_end[0]),
  89. 'end_date' => trim($start_end[1]),
  90. ];
  91. }
  92. $seckillGoods = new SeckillGoods();
  93. $seckillGoods->saveAll($add_data);
  94. return true;
  95. }catch(\Exception $e) {
  96. self::$error = $e->getMessage();
  97. return false;
  98. }
  99. }
  100. public static function goodsList($get)
  101. {
  102. $where = [
  103. ['sg.del', '=', 0],
  104. ['sg.shop_id', '=', $get['shop_id']]
  105. ];
  106. // 商品名称
  107. if(isset($get['name']) && !($get['name'] == '')) {
  108. $where[] = ['g.name', 'like', '%'.trim($get['name']).'%'];
  109. }
  110. // 参与日期
  111. if(isset($get['start_end']) && !empty($get['start_end'])) {
  112. $start_end = explode('~', $get['start_end']);
  113. $where[] = ['sg.start_date', '>=', trim($start_end[0])];
  114. $where[] = ['sg.end_date', '<=', trim($start_end[1])];
  115. }
  116. // 参与时段
  117. if(isset($get['seckill_id']) && !empty($get['seckill_id'])) {
  118. $where[] = ['sg.seckill_id', '=', $get['seckill_id']];
  119. }
  120. $lists = SeckillGoods::alias('sg')
  121. ->leftJoin('seckill_time st', 'st.id=sg.seckill_id')
  122. ->leftJoin('goods g', 'sg.goods_id=g.id')
  123. ->field('sg.seckill_id,sg.goods_id,review_status,review_desc,start_date,end_date,start_time,end_time,g.name,g.image,g.min_price,g.max_price')
  124. ->where($where)
  125. ->group('sg.seckill_id,sg.goods_id,review_status,review_desc,start_date,end_date,start_time,end_time,g.name,g.image,g.min_price,g.max_price')
  126. ->select()
  127. ->toArray();
  128. // 按类型提取数据
  129. $unSeckill = [];
  130. $seckill = [];
  131. $waitReview = [];
  132. $refuseReview = [];
  133. $now = time();
  134. $now_date = date('Y-m-d', $now);
  135. foreach($lists as $key => $item) {
  136. if($item['review_status'] == 0) { // 待审核
  137. $waitReview[] = $item;
  138. continue;
  139. }else if($item['review_status'] == 2){ // 审核拒绝
  140. $refuseReview[] = $item;
  141. continue;
  142. }else if($item['review_status'] == 1) { // 审核通过
  143. $start_date_time = strtotime($item['start_date']. ' 00:00:00');
  144. $end_date_time = strtotime($item['end_date']. ' 23:59:59');
  145. // 日期校验
  146. if($now >= $start_date_time && $now <= $end_date_time) {
  147. $start_time = strtotime($now_date. ' '. $item['start_time']);
  148. $end_time = strtotime($now_date. ' '. $item['end_time']);
  149. if($now >= $start_time && $now <= $end_time) {
  150. $seckill[] = $item;
  151. continue;
  152. }else{
  153. // 未在秒杀时段
  154. $unSeckill[] = $item;
  155. continue;
  156. }
  157. }else{
  158. // 未在秒杀时间日期
  159. $unSeckill[] = $item;
  160. continue;
  161. }
  162. }
  163. }
  164. switch($get['type']) {
  165. case 'seckill':
  166. $lists = $seckill;
  167. break;
  168. case 'un_seckill':
  169. $lists = $unSeckill;
  170. break;
  171. case 'wait_review':
  172. $lists = $waitReview;
  173. break;
  174. case 'refuse_review':
  175. $lists = $refuseReview;
  176. break;
  177. }
  178. // 组装信息
  179. $review_status_desc = ['待审核','审核通过','审核拒绝'];
  180. foreach($lists as &$item) {
  181. // 秒杀价格
  182. $price = SeckillGoods::where([
  183. 'del' => 0,
  184. 'shop_id' => $get['shop_id'],
  185. 'seckill_id' => $item['seckill_id'],
  186. 'goods_id' => $item['goods_id'],
  187. 'start_date' => $item['start_date'],
  188. 'end_date' => $item['end_date'],
  189. ])->column('price', 'id');
  190. $seckill_min_price = min($price);
  191. $seckill_max_price = max($price);
  192. $item['seckill_price'] = $seckill_min_price == $seckill_max_price ? '¥ ' .$seckill_min_price : '¥ '. $seckill_min_price . ' ~ ¥ ' . $seckill_max_price;
  193. // 商品价格
  194. $item['goods_price'] = $item['min_price'] == $item['max_price'] ? '¥ ' .$item['min_price'] : '¥ '. $item['min_price'] .' ~ ¥ '. $item['max_price'];
  195. // 参与日期
  196. $item['date'] = $item['start_date'] . ' ~ ' . $item['end_date'];
  197. // 参与时段
  198. $item['time'] = $item['start_time'] . ' ~ ' . $item['end_time'];
  199. // 审核状态
  200. $item['review_status_desc'] = $review_status_desc[$item['review_status']];
  201. }
  202. // 分页
  203. $count = count($lists);
  204. $index = ($get['page'] -1) * $get['limit'];
  205. $lists = array_slice($lists, $index, $get['limit']);
  206. // 返回
  207. return [
  208. 'count' => $count,
  209. 'lists' => $lists,
  210. ];
  211. }
  212. public static function getSeckillGoods($id,$seckill_id, $start_date, $end_date){
  213. $skill_goods = SeckillGoods::alias('sg')
  214. ->join('goods_item gi','sg.item_id = gi.id')
  215. ->where(['del'=>0,'sg.goods_id'=>$id,'sg.seckill_id'=>$seckill_id, 'sg.start_date'=>$start_date,'sg.end_date'=>$end_date])
  216. ->field('sg.*,gi.image,gi.spec_value_str,gi.price as goods_price')
  217. ->select()
  218. ->toArray();
  219. $goods_id = $skill_goods[0]['goods_id'];
  220. $goods = Goods::where(['del'=>0,'id'=>$goods_id])->field('image,name')->find();
  221. foreach ($skill_goods as &$item){
  222. $item['name'] = $goods['name'];
  223. if(!$item['image']){
  224. $item['image'] = $goods['image'];
  225. }
  226. $item['date'] = $item['start_date'] . ' ~ ' . $item['end_date'];
  227. }
  228. return $skill_goods;
  229. }
  230. public static function editGoods($post){
  231. Db::startTrans();
  232. try{
  233. // 开始日期结束日期处理
  234. $start_end = explode('~', $post['start_end']);
  235. $now = time();
  236. foreach ($post['item'] as $goods){
  237. $review_status = SeckillGoods::where('id', $goods['id'])->value('review_status');
  238. $review_status = $review_status == 2 ? 0 : $review_status;
  239. $update_data = [
  240. 'start_date' => trim($start_end[0]),
  241. 'end_date' => trim($start_end[1]),
  242. 'seckill_id' => $post['seckill_id'],
  243. 'price' => $goods['price'],
  244. 'update_time' => $now,
  245. 'review_status' => $review_status,
  246. ];
  247. SeckillGoods::where(['id'=>$goods['id']])->update($update_data);
  248. }
  249. Db::commit();
  250. return true;
  251. }catch(\Exception $e) {
  252. self::$error = $e->getMessage();
  253. Db::rollback();
  254. return false;
  255. }
  256. }
  257. public static function delGoods($id,$seckill_id,$start_date,$end_date,$shop_id){
  258. try{
  259. $update_data = [
  260. 'update_time' => time(),
  261. 'del' => 1,
  262. ];
  263. SeckillGoods::where([
  264. 'del'=>0,
  265. 'goods_id'=>$id,
  266. 'seckill_id'=>$seckill_id,
  267. 'shop_id' => $shop_id,
  268. 'start_date' => $start_date,
  269. 'end_date' => $end_date,
  270. ])->update($update_data);
  271. return true;
  272. }catch(\Exception $e) {
  273. self::$error = $e->getMessage();
  274. return false;
  275. }
  276. }
  277. }