LiveGoods.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2022 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 app\admin\model\special\Special;
  18. use basic\ModelBasic;
  19. use traits\ModelTrait;
  20. class LiveGoods extends ModelBasic
  21. {
  22. use ModelTrait;
  23. public static function setSpecialWhere($where)
  24. {
  25. $model = self::alias('g');
  26. $model = $model->where('g.is_delete', 0)->where('g.type', 0);
  27. $model = $model->join('special s', 'g.special_id=s.id')->join('__SPECIAL_SUBJECT__ J', 'J.id=s.subject_id');
  28. if ($where['store_name'] && isset($where['store_name'])) {
  29. $model = $model->whereLike('g.special_name', "%" . $where['store_name'] . "%");
  30. }
  31. if ($where['is_show'] != "" && isset($where['is_show'])) {
  32. $model = $model->where('g.is_show', $where['is_show']);
  33. }
  34. if ($where['live_id'] != 0 && isset($where['live_id'])) {
  35. $model = $model->where('g.live_id', $where['live_id']);
  36. }
  37. return $model;
  38. }
  39. /*
  40. * 查询直播间用户列表
  41. * @param array $where
  42. * */
  43. public static function getLiveGoodsList($where)
  44. {
  45. $data = self::setSpecialWhere($where);
  46. $data = $data->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');
  47. $data = $data->page((int)$where['page'], (int)$where['limit'])->select();
  48. $data = count($data) ? $data->toArray() : [];
  49. foreach ($data as &$item) {
  50. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  51. $item['pink_end_time'] = $item['pink_end_time'] ? strtotime($item['pink_end_time']) : 0;
  52. $item['sales'] = StoreOrder::where(['paid' => 1, 'type' => 0, 'cart_id' => $item['id'], 'refund_status' => 0])->count();
  53. //查看拼团状态,如果已结束关闭拼团
  54. if ($item['is_pink'] && $item['pink_end_time'] < time()) {
  55. self::update(['is_pink' => 0], ['id' => $item['id']]);
  56. $item['is_pink'] = 0;
  57. }
  58. }
  59. $count = self::setSpecialWhere($where)->count();
  60. return compact('data', 'count');
  61. }
  62. public static function setStoreWhere($where)
  63. {
  64. $model = self::alias('g');
  65. $model = $model->where('g.is_delete', 0)->where('g.type', 1);
  66. if ($where['store_name'] && isset($where['store_name'])) {
  67. $model = $model->whereLike('g.special_name', "%" . $where['store_name'] . "%");
  68. }
  69. if ($where['is_show'] != "" && isset($where['is_show'])) {
  70. $model = $model->where('g.is_show', $where['is_show']);
  71. }
  72. if ($where['live_id'] != 0 && isset($where['live_id'])) {
  73. $model = $model->where('g.live_id', $where['live_id']);
  74. }
  75. $model = $model->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');
  76. return $model;
  77. }
  78. public static function getLiveStoreProductList($where)
  79. {
  80. $model = self::setStoreWhere($where)->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');
  81. $data = $model->page((int)$where['page'], (int)$where['limit'])->select();
  82. $data = count($data) ? $data->toArray() : [];
  83. foreach ($data as &$item) {
  84. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  85. }
  86. $count = self::setStoreWhere($where)->count();
  87. return compact('data', 'count');
  88. }
  89. /**直播带课
  90. * @param $live_id
  91. * @param int $type
  92. * @return LiveGoods|array|false|\PDOStatement|string|\think\Collection
  93. * @throws \think\Exception
  94. * @throws \think\db\exception\DataNotFoundException
  95. * @throws \think\db\exception\ModelNotFoundException
  96. * @throws \think\exception\DbException
  97. */
  98. public static function getLiveGoodsLists($live_id, $type = 0)
  99. {
  100. $data = self::alias('g');
  101. $data = $data->where('g.is_delete', 0);
  102. $data = $data->where('g.live_id', $live_id);
  103. $data = $data->where('g.type', $type);
  104. $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,
  105. s.id,s.title,s.image,s.add_time,s.pink_end_time,s.is_pink,J.name as subject_name');
  106. $data = $data->order('g.sort DESC')->select();
  107. $data = count($data) ? $data->toArray() : [];
  108. foreach ($data as &$item) {
  109. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  110. $item['pink_end_time'] = $item['pink_end_time'] ? strtotime($item['pink_end_time']) : 0;
  111. $item['sales'] = StoreOrder::where(['paid' => 1, 'cart_id' => $item['id'], 'refund_status' => 0])->count();
  112. //查看拼团状态,如果已结束关闭拼团
  113. if ($item['is_pink'] && $item['pink_end_time'] < time()) {
  114. self::update(['is_pink' => 0], ['id' => $item['id']]);
  115. $item['is_pink'] = 0;
  116. }
  117. }
  118. return $data;
  119. }
  120. /**直播带货
  121. * @param $live_id
  122. * @param int $type
  123. * @return LiveGoods|array|false|\PDOStatement|string|\think\Collection
  124. * @throws \think\db\exception\DataNotFoundException
  125. * @throws \think\db\exception\ModelNotFoundException
  126. * @throws \think\exception\DbException
  127. */
  128. public static function getLiveProductLists($live_id, $type = 0)
  129. {
  130. $data = self::alias('g');
  131. $data = $data->where('g.is_delete', 0);
  132. $data = $data->where('g.live_id', $live_id);
  133. $data = $data->where('g.type', $type);
  134. $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,
  135. s.id,s.store_name,s.image,s.price,s.sales,s.add_time');
  136. $data = $data->order('g.sort DESC')->select();
  137. $data = count($data) ? $data->toArray() : [];
  138. foreach ($data as &$item) {
  139. $item['_add_time'] = date('Y-m-d H:i:s', $item['add_time']);
  140. }
  141. return $data;
  142. }
  143. /**插入带货商品
  144. * @param array $data
  145. * @return bool|int|string
  146. */
  147. public static function insterLiveGoods(array $data)
  148. {
  149. if (!$data) return false;
  150. return self::insertGetId($data);
  151. }
  152. /**获取单个
  153. * @param array $where
  154. * @return array|bool|false|\PDOStatement|string|\think\Model
  155. * @throws \think\db\exception\DataNotFoundException
  156. * @throws \think\db\exception\ModelNotFoundException
  157. * @throws \think\exception\DbException
  158. */
  159. public static function getOne(array $where)
  160. {
  161. if (!$where) return false;
  162. return self::where($where)->find();
  163. }
  164. /**添加带货专题
  165. * @param $source_list_ids 一维数组,素材id
  166. * @param int $special_id 专题id
  167. * @return bool
  168. */
  169. public static function saveLiveGoods($source_list_ids, $special_id = 0, $type = 0)
  170. {
  171. if (!$special_id || !$source_list_ids) return false;
  172. $source_list_ids = explode(',', $source_list_ids);
  173. $live_id = LiveStudio::where('special_id', $special_id)->value('id');
  174. if (!$live_id) return false;
  175. try {
  176. $inster['live_id'] = $live_id;
  177. foreach ($source_list_ids as $sk => $sv) {
  178. $inster['special_id'] = $sv;
  179. $inster['type'] = $type;
  180. if ($type == 1) {
  181. $inster['special_name'] = StoreProduct::where('id', $sv)->value('store_name');
  182. } else {
  183. $inster['special_name'] = Special::where('id', $sv)->value('title');
  184. }
  185. $inster['sort'] = 0;
  186. $inster['add_time'] = time();
  187. self::set($inster);
  188. }
  189. return true;
  190. } catch (\Exception $e) {
  191. return false;
  192. }
  193. }
  194. /**添加带货
  195. * @param $source_list_ids 一维数组,素材id
  196. * @param int $special_id 专题id
  197. * @return bool
  198. */
  199. public static function saveAddLiveGoods($source_list_ids, int $special_id, $type = 0)
  200. {
  201. if (!$special_id || !is_numeric($special_id)) {
  202. return false;
  203. }
  204. $live_id = LiveStudio::where('special_id', $special_id)->field('id')->find();
  205. if (!$live_id) return false;
  206. if (!$source_list_ids) {
  207. self::where(['live_id' => $live_id->id, 'type' => $type])->delete();
  208. return true;
  209. }
  210. try {
  211. $where['live_id'] = $live_id->id;
  212. $liveGoodsAll = self::getOne($where);
  213. if ($liveGoodsAll) {
  214. self::where(['live_id' => $live_id->id, 'type' => $type])->delete();
  215. }
  216. $inster['live_id'] = $live_id->id;
  217. foreach ($source_list_ids as $sk => $sv) {
  218. $inster['special_id'] = $sv->id;
  219. $inster['type'] = $type;
  220. if ($type == 1) {
  221. $inster['special_name'] = $sv->store_name;
  222. } else {
  223. $inster['special_name'] = $sv->title;
  224. }
  225. $inster['sort'] = $sv->sort;
  226. $inster['add_time'] = time();
  227. self::set($inster);
  228. }
  229. return true;
  230. } catch (\Exception $e) {
  231. return false;
  232. }
  233. }
  234. /**清空带货
  235. * @param int $special_id 专题id
  236. * @return bool
  237. */
  238. public static function delLiveGoods($special_id, $type = 0)
  239. {
  240. if (!$special_id || !is_numeric($special_id)) {
  241. return false;
  242. }
  243. $live_id = LiveStudio::where('special_id', $special_id)->field('id')->find();
  244. if (!$live_id) return false;
  245. self::where(['live_id' => $live_id->id, 'type' => $type])->delete();
  246. return true;
  247. }
  248. }