LiveGoods.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\model\live;
  12. /**
  13. * 直播带货商品
  14. */
  15. use app\admin\model\order\StoreOrder;
  16. use app\admin\model\store\StoreProduct;
  17. use basic\ModelBasic;
  18. use traits\ModelTrait;
  19. class LiveGoods extends ModelBasic
  20. {
  21. use ModelTrait;
  22. /*
  23. * 查询直播间用户列表
  24. * @param array $where
  25. * */
  26. public static function getLiveGoodsList($where)
  27. {
  28. $data = self::alias('g');
  29. $data = $data->where('g.is_delete',0)->where('g.type',0);
  30. if ($where['store_name'] && isset($where['store_name'])){
  31. $data = $data->whereLike('g.special_name',"%".$where['store_name']."%");
  32. }
  33. if ($where['is_show'] != "" && isset($where['is_show'])){
  34. $data = $data->where('g.is_show',$where['is_show']);
  35. }
  36. if ($where['live_id'] != 0 && isset($where['live_id'])){
  37. $data = $data->where('g.live_id',$where['live_id']);
  38. }
  39. $data = $data->join('special s','g.special_id=s.id')->join('__SPECIAL_SUBJECT__ J', 'J.id=s.subject_id')->field('g.id as live_goods_id, g.sort as gsort, g.fake_sales as gfake_sales, g.is_show as gis_show, g.sales as gsales, s.*, J.name as subject_name');
  40. $data = $data->page((int)$where['page'],(int)$where['limit'])->select();
  41. $data = count($data) ? $data->toArray() : [];
  42. foreach ($data as &$item){
  43. $item['_add_time'] = date('Y-m-d H:i:s',$item['add_time']);
  44. $item['pink_end_time'] = $item['pink_end_time'] ? strtotime($item['pink_end_time']) : 0;
  45. $item['sales'] = StoreOrder::where(['paid' => 1,'type'=>0, 'cart_id' => $item['id'], 'refund_status' => 0])->count();
  46. //查看拼团状态,如果已结束关闭拼团
  47. if ($item['is_pink'] && $item['pink_end_time'] < time()) {
  48. self::update(['is_pink' => 0], ['id' => $item['id']]);
  49. $item['is_pink'] = 0;
  50. }
  51. }
  52. $count = self::alias('g')->join('special s','g.special_id=s.id')->count();
  53. return compact('data','count');
  54. }
  55. public static function getLiveStoreProductList($where)
  56. {
  57. $data = self::alias('g');
  58. $data = $data->where('g.is_delete',0)->where('g.type',1);
  59. if ($where['store_name'] && isset($where['store_name'])){
  60. $data = $data->whereLike('g.special_name',"%".$where['store_name']."%");
  61. }
  62. if ($where['is_show'] != "" && isset($where['is_show'])){
  63. $data = $data->where('g.is_show',$where['is_show']);
  64. }
  65. if ($where['live_id'] != 0 && isset($where['live_id'])){
  66. $data = $data->where('g.live_id',$where['live_id']);
  67. }
  68. $data = $data->join('StoreProduct s','g.special_id=s.id')->where(['s.is_del'=>0,'s.is_show'=>1])->join('StoreCategory c', 'c.id=s.cate_id')->field('g.live_id as live_id, g.id as live_goods_id, g.sort as gsort, g.fake_sales as gfake_sales, g.is_show as gis_show, g.sales as gsales, s.*,c.cate_name');
  69. $data = $data->page((int)$where['page'],(int)$where['limit'])->select();
  70. $data = count($data) ? $data->toArray() : [];
  71. foreach ($data as &$item){
  72. $item['_add_time'] = date('Y-m-d H:i:s',$item['add_time']);
  73. }
  74. $count = self::alias('g')->join('StoreProduct s','g.special_id=s.id')->where(['s.is_del'=>0,'s.is_show'=>1])->count();
  75. return compact('data','count');
  76. }
  77. /**直播带课
  78. * @param $live_id
  79. * @param int $type
  80. * @return LiveGoods|array|false|\PDOStatement|string|\think\Collection
  81. * @throws \think\Exception
  82. * @throws \think\db\exception\DataNotFoundException
  83. * @throws \think\db\exception\ModelNotFoundException
  84. * @throws \think\exception\DbException
  85. */
  86. public static function getLiveGoodsLists($live_id,$type=0)
  87. {
  88. $data = self::alias('g');
  89. $data = $data->where('g.is_delete',0);
  90. $data = $data->where('g.live_id',$live_id);
  91. $data = $data->where('g.type',$type);
  92. $data = $data->join('special s','g.special_id=s.id')->where(['s.is_del'=>0,'s.is_show'=>1])->join('__SPECIAL_SUBJECT__ J', 'J.id=s.subject_id')->field('g.live_id as live_id, g.id as live_goods_id, g.sort, g.fake_sales as gfake_sales, g.is_show as gis_show, g.sales as gsales,
  93. s.id,s.title,s.image,s.add_time,s.pink_end_time,s.is_pink,J.name as subject_name');
  94. $data = $data->order('g.sort DESC')->select();
  95. $data = count($data) ? $data->toArray() : [];
  96. foreach ($data as &$item){
  97. $item['_add_time'] = date('Y-m-d H:i:s',$item['add_time']);
  98. $item['pink_end_time'] = $item['pink_end_time'] ? strtotime($item['pink_end_time']) : 0;
  99. $item['sales'] = StoreOrder::where(['paid' => 1, 'cart_id' => $item['id'], 'refund_status' => 0])->count();
  100. //查看拼团状态,如果已结束关闭拼团
  101. if ($item['is_pink'] && $item['pink_end_time'] < time()) {
  102. self::update(['is_pink' => 0], ['id' => $item['id']]);
  103. $item['is_pink'] = 0;
  104. }
  105. }
  106. return $data;
  107. }
  108. /**直播带货
  109. * @param $live_id
  110. * @param int $type
  111. * @return LiveGoods|array|false|\PDOStatement|string|\think\Collection
  112. * @throws \think\db\exception\DataNotFoundException
  113. * @throws \think\db\exception\ModelNotFoundException
  114. * @throws \think\exception\DbException
  115. */
  116. public static function getLiveProductLists($live_id,$type=0)
  117. {
  118. $data = self::alias('g');
  119. $data = $data->where('g.is_delete',0);
  120. $data = $data->where('g.live_id',$live_id);
  121. $data = $data->where('g.type',$type);
  122. $data = $data->join('StoreProduct s','g.special_id=s.id')->where(['s.is_del'=>0,'s.is_show'=>1])->field('g.live_id as live_id, g.id as live_goods_id, g.sort, g.fake_sales as gfake_sales, g.is_show as gis_show, g.sales as gsales,
  123. s.id,s.store_name,s.image,s.price,s.sales,s.add_time');
  124. $data = $data->order('g.sort DESC')->select();
  125. $data = count($data) ? $data->toArray() : [];
  126. foreach ($data as &$item){
  127. $item['_add_time'] = date('Y-m-d H:i:s',$item['add_time']);
  128. }
  129. return $data;
  130. }
  131. /**插入带货商品
  132. * @param array $data
  133. * @return bool|int|string
  134. */
  135. public static function insterLiveGoods(array $data)
  136. {
  137. if (!$data) return false;
  138. return self::insertGetId($data);
  139. }
  140. /**获取单个
  141. * @param array $where
  142. * @return array|bool|false|\PDOStatement|string|\think\Model
  143. * @throws \think\db\exception\DataNotFoundException
  144. * @throws \think\db\exception\ModelNotFoundException
  145. * @throws \think\exception\DbException
  146. */
  147. public static function getOne(array $where)
  148. {
  149. if (!$where) return false;
  150. return self::where($where)->find();
  151. }
  152. /**添加带货专题
  153. * @param $source_list_ids 一维数组,素材id
  154. * @param int $special_id 专题id
  155. * @return bool
  156. */
  157. public static function saveLiveGoods($source_list_ids, int $special_id,$type=0)
  158. {
  159. if (!$special_id || !is_numeric($special_id)) {
  160. return false;
  161. }
  162. $live_id = LiveStudio::where('special_id', $special_id)->field('id')->find();
  163. if (!$live_id) return false;
  164. if (!$source_list_ids) {
  165. self::where(['live_id' => $live_id->id,'type'=>$type])->delete();
  166. return true;
  167. }
  168. try {
  169. $where['live_id'] = $live_id->id;
  170. $liveGoodsAll = self::getOne($where);
  171. if ($liveGoodsAll) {
  172. self::where(['live_id' => $live_id->id,'type'=>$type])->delete();
  173. }
  174. $inster['live_id'] = $live_id->id;
  175. foreach ($source_list_ids as $sk => $sv) {
  176. $inster['special_id'] = $sv->id;
  177. $inster['type'] = $type;
  178. if($type==1){
  179. $inster['special_name'] = $sv->store_name;
  180. }else{
  181. $inster['special_name'] = $sv->title;
  182. }
  183. $inster['sort'] = $sv->sort;
  184. $inster['add_time'] = time();
  185. self::set($inster);
  186. }
  187. return true;
  188. } catch (\Exception $e) {
  189. return false;
  190. }
  191. }
  192. }