LiveGoods.php 11 KB

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