StoreCombination.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. <?php
  2. /**
  3. *
  4. * @author: xaboy<365615158@qq.com>
  5. * @day: 2017/11/11
  6. */
  7. namespace app\models\store;
  8. use crmeb\services\UtilService;
  9. use crmeb\traits\ModelTrait;
  10. use crmeb\basic\BaseModel;
  11. /**
  12. * TODO 拼团商品Model
  13. * Class StoreCombination
  14. * @package app\models\store
  15. */
  16. class StoreCombination extends BaseModel
  17. {
  18. /**
  19. * 数据表主键
  20. * @var string
  21. */
  22. protected $pk = 'id';
  23. /**
  24. * 模型名称
  25. * @var string
  26. */
  27. protected $name = 'store_combination';
  28. use ModelTrait;
  29. public static function merSet($mer_id, $alias = '')
  30. {
  31. return $mer_id ? self::where($alias ? $alias . '.mer_id' : 'mer_id', $mer_id) : new self;
  32. }
  33. protected function getAddTimeAttr($value)
  34. {
  35. if ($value) return date('Y-m-d H:i:s', $value);
  36. return '';
  37. }
  38. /**
  39. * @param $where
  40. * @return array
  41. */
  42. public static function get_list($length = 10)
  43. {
  44. if ($post = input('post.')) {
  45. $where = $post['where'];
  46. $model = new self();
  47. $model = $model->alias('c');
  48. $model = $model->join('StoreProduct s', 's.id=c.product_id');
  49. $model = $model->where('c.is_show', 1)->where('c.is_del', 0)->where('c.start_time', '<', time())->where('c.stop_time', '>', time());
  50. if (!empty($where['search'])) {
  51. $model = $model->where('c.title', 'like', "%{$where['search']}%");
  52. $model = $model->whereOr('s.keyword', 'like', "{$where['search']}%");
  53. }
  54. $model = $model->field('c.*,s.price as product_price');
  55. if ($where['key']) {
  56. if ($where['sales'] == 1) {
  57. $model = $model->order('c.sales desc');
  58. } else if ($where['sales'] == 2) {
  59. $model = $model->order('c.sales asc');
  60. }
  61. if ($where['price'] == 1) {
  62. $model = $model->order('c.price desc');
  63. } else if ($where['price'] == 2) {
  64. $model = $model->order('c.price asc');
  65. }
  66. if ($where['people'] == 1) {
  67. $model = $model->order('c.people asc');
  68. }
  69. if ($where['default'] == 1) {
  70. $model = $model->order('c.sort desc,c.id desc');
  71. }
  72. } else {
  73. $model = $model->order('c.sort desc,c.id desc');
  74. }
  75. $page = is_string($where['page']) ? (int)$where['page'] + 1 : $where['page'] + 1;
  76. $list = $model->page($page, $length)->select()->toArray();
  77. return ['list' => $list, 'page' => $page];
  78. }
  79. }
  80. /**
  81. * 获取拼团数据
  82. * @param int $page
  83. * @param int $limit
  84. * @return mixed
  85. */
  86. public static function getAll($page = 0, $limit = 20, $mer_id = false)
  87. {
  88. $model = self::merSet($mer_id, 'c');
  89. $model = $model->alias('c');
  90. $model = $model->join('StoreProduct s', 's.id=c.product_id');
  91. $model = $model->field('c.*,s.price as product_price');
  92. $model = $model->order('c.sort desc,c.id desc');
  93. $model = $model->where('c.is_show', 1);
  94. $model = $model->where('c.is_del', 0);
  95. $model = $model->where('c.start_time', '<', time());
  96. $model = $model->where('c.stop_time', '>', time());
  97. if ($page) $model = $model->page($page, $limit);
  98. return $model->select()->each(function ($item) {
  99. $item['image'] = set_file_url($item['image']);
  100. $item['price'] = floatval($item['price']);
  101. $item['product_price'] = floatval($item['product_price']);
  102. });
  103. }
  104. /**
  105. * 获取是否有拼团商品
  106. * */
  107. public static function getPinkIsOpen($mer_id = false)
  108. {
  109. return self::merSet($mer_id, 'c')->alias('c')->join('StoreProduct s', 's.id=c.product_id')->where('c.is_show', 1)->where('c.is_del', 0)
  110. ->where('c.start_time', '<', time())->where('c.stop_time', '>', time())->count();
  111. }
  112. /**
  113. * 获取一条拼团数据
  114. * @param $id
  115. * @return mixed
  116. */
  117. public static function getCombinationOne($id, $mer_id = false)
  118. {
  119. $model = self::merSet($mer_id, 'c');
  120. $model = $model->alias('c');
  121. $model = $model->join('StoreProduct s', 's.id=c.product_id');
  122. $model = $model->field('c.*,s.price as product_price,SUM(s.sales+s.ficti) as total');
  123. $model = $model->where('c.is_show', 1);
  124. $model = $model->where('c.is_del', 0);
  125. $model = $model->where('c.id', $id);
  126. $model = $model->where('c.start_time', '<', time());
  127. $model = $model->where('c.stop_time', '>', time() - 86400);
  128. $info = $model->find();
  129. if ($info['id']) {
  130. return $info;
  131. } else {
  132. return [];
  133. }
  134. }
  135. /**
  136. * 获取指定id拼团数据
  137. */
  138. public static function getCombinationById($id, $mer_id = false)
  139. {
  140. $model = self::merSet($mer_id, 'c');
  141. $model = $model->alias('c');
  142. $model = $model->join('StoreProduct s', 's.id=c.product_id');
  143. $model = $model->field('c.*,s.price as product_price,SUM(s.sales+s.ficti) as total');
  144. $model = $model->where('c.is_del', 0);
  145. $model = $model->where('c.id', $id);
  146. $info = $model->find();
  147. if ($info['id']) {
  148. return $info;
  149. } else {
  150. return [];
  151. }
  152. }
  153. /**
  154. * 获取推荐的拼团商品
  155. * @return mixed
  156. */
  157. public static function getCombinationHost($limit = 0, $mer_id = false)
  158. {
  159. $model = self::merSet($mer_id, 'c');
  160. $model = $model->alias('c');
  161. $model = $model->join('StoreProduct s', 's.id=c.product_id');
  162. $model = $model->field('c.id,c.image,c.price,c.sales,c.title,c.people,s.price as product_price');
  163. $model = $model->where('c.is_del', 0);
  164. $model = $model->where('c.is_host', 1);
  165. $model = $model->where('c.start_time', '<', time());
  166. $model = $model->where('c.stop_time', '>', time());
  167. if ($limit) $model = $model->limit($limit);
  168. return $model->select();
  169. }
  170. /**
  171. * 修改销量和库存
  172. * @param $num
  173. * @param $CombinationId
  174. * @return bool
  175. */
  176. public static function decCombinationStock($num, $CombinationId, $unique)
  177. {
  178. $product_id = self::where('id', $CombinationId)->value('product_id');
  179. if ($unique) {
  180. $res = false !== StoreProductAttrValue::decProductAttrStock($CombinationId, $unique, $num, 3);
  181. $res = $res && self::where('id', $CombinationId)->dec('stock', $num)->inc('sales', $num)->update();
  182. $sku = StoreProductAttrValue::where('product_id', $CombinationId)->where('unique', $unique)->where('type', 3)->value('suk');
  183. $res = $res && StoreProductAttrValue::where('product_id', $product_id)->where('suk', $sku)->where('type', 0)->dec('stock', $num)->inc('sales', $num)->update();
  184. } else {
  185. $res = false !== self::where('id', $CombinationId)->dec('stock', $num)->inc('sales', $num)->update();
  186. }
  187. $res = $res && StoreProduct::where('id', $product_id)->dec('stock', $num)->inc('sales', $num)->update();
  188. return $res;
  189. }
  190. /**
  191. * 增加库存,减少销量
  192. * @param $num
  193. * @param $CombinationId
  194. * @return bool
  195. */
  196. public static function incCombinationStock($num, $CombinationId)
  197. {
  198. $combination = self::where('id', $CombinationId)->field(['stock', 'sales'])->find();
  199. if (!$combination) return true;
  200. if ($combination->sales > 0) $combination->sales = bcsub($combination->sales, $num, 0);
  201. if ($combination->sales < 0) $combination->sales = 0;
  202. $combination->stock = bcadd($combination->stock, $num, 0);
  203. return $combination->save();
  204. }
  205. /**
  206. * 判断库存是否足够
  207. * @param $id
  208. * @param $cart_num
  209. * @return int|mixed
  210. */
  211. public static function getCombinationStock($id, $cart_num)
  212. {
  213. $stock = self::where('id', $id)->value('stock');
  214. return $stock > $cart_num ? $stock : 0;
  215. }
  216. /**
  217. * 获取字段值
  218. * @param $id
  219. * @param $field
  220. * @return mixed
  221. */
  222. public static function getCombinationField($id, $field = 'title')
  223. {
  224. return self::where('id', $id)->value($field);
  225. }
  226. /**
  227. * 获取商品状态
  228. * @param $id
  229. * @return mixed
  230. */
  231. public static function isValidCombination($id)
  232. {
  233. $model = new self();
  234. $model = $model->where('id', $id);
  235. $model = $model->where('is_del', 0);
  236. $model = $model->where('is_show', 1);
  237. return $model->count();
  238. }
  239. /**
  240. * 增加浏览量
  241. * @param int $id
  242. * @return bool
  243. */
  244. public static function editIncBrowse($id = 0)
  245. {
  246. if (!$id) return false;
  247. $browse = self::where('id', $id)->value('browse');
  248. $browse = bcadd($browse, 1, 0);
  249. self::edit(['browse' => $browse], $id);
  250. }
  251. /**
  252. * 获取查看拼团统计
  253. * @return array
  254. * @throws \think\db\exception\DataNotFoundException
  255. * @throws \think\db\exception\ModelNotFoundException
  256. * @throws \think\exception\DbException
  257. */
  258. public static function getStatistics($mer_id = '')
  259. {
  260. $statistics = array();
  261. $statistics['browseCount'] = self::where('mer_id', $mer_id)->value('sum(browse) as browse');//总展现量
  262. $statistics['browseCount'] = $statistics['browseCount'] ? $statistics['browseCount'] : 0;
  263. $statistics['visitCount'] = StoreVisit::where('mer_id', $mer_id)->where('product_type', 'combination')->count();//访客人数
  264. $statistics['partakeCount'] = StorePink::getCountPeopleAll(0, $mer_id);//参与人数
  265. $statistics['pinkCount'] = StorePink::getCountPeoplePink(0, $mer_id);//成团数量
  266. $res = [
  267. ['col' => 6, 'count' => $statistics['browseCount'], 'name' => '总展现量(次)', 'className' => 'md-trending-up'],
  268. ['col' => 6, 'count' => $statistics['visitCount'], 'name' => '访客人数(人)', 'className' => 'md-stats'],
  269. ['col' => 6, 'count' => $statistics['partakeCount'], 'name' => '参与人数(人)', 'className' => 'ios-speedometer-outline'],
  270. ['col' => 6, 'count' => $statistics['pinkCount'], 'name' => '成团数量(个)', 'className' => 'md-rose'],
  271. ];
  272. return compact('res');
  273. }
  274. /**
  275. * 拼团列表
  276. * @param $where
  277. * @return array
  278. */
  279. public static function systemPage($where)
  280. {
  281. $model = self::setWhere($where);
  282. $count = $model->count();
  283. $list = $model->page((int)$where['page'], (int)$where['limit'])
  284. ->select()
  285. ->each(function ($item) {
  286. $item['count_people_all'] = StorePink::getCountPeopleAll($item['id']);//参与人数
  287. $item['count_people_pink'] = StorePink::getCountPeoplePink($item['id']);//成团人数
  288. $item['count_people_browse'] = StoreVisit::getVisitPeople($item['id']);//访问人数
  289. });
  290. return compact('count', 'list');
  291. }
  292. /**
  293. * 设置拼团 where 条件
  294. * @param $where
  295. * @param null $model
  296. * @return mixed
  297. */
  298. public static function setWhere($where, $model = null)
  299. {
  300. $model = $model === null ? new self() : $model;
  301. $model = $model->alias('c');
  302. $model = $model->field('c.*,p.store_name,p.price as ot_price');
  303. $model = $model->where('c.mer_id', $where['mer_id']);
  304. $model = $model->join('StoreProduct p', 'p.id=c.product_id', 'LEFT');
  305. if (isset($where['is_show']) && $where['is_show'] != '') $model = $model->where('c.is_show', $where['is_show']);
  306. // if(isset($where['is_host']) && $where['is_host'] != '') $model = $model->where('c.is_host',$where['is_host']);
  307. if (isset($where['store_name']) && $where['store_name'] != '') $model = $model->where('p.store_name|p.id|c.id|c.title', 'LIKE', "%$where[store_name]%");
  308. return $model->order('c.id desc')->where('c.is_del', 0);
  309. }
  310. /**
  311. *查出导出数据
  312. * @param $where
  313. */
  314. public static function exportData($where)
  315. {
  316. $list = self::setWhere($where)->select();
  317. count($list) && $list = $list->toArray();
  318. foreach ($list as &$item) {
  319. $item['count_people_all'] = StorePink::getCountPeopleAll($item['id']);//参与人数
  320. $item['count_people_pink'] = StorePink::getCountPeoplePink($item['id']);//成团人数
  321. $item['count_people_browse'] = StoreVisit::getVisitPeople($item['id']);//访问人数
  322. $item['_stop_time'] = date('Y/m/d H:i:s', $item['stop_time']);
  323. }
  324. return $list;
  325. }
  326. /**
  327. * 详情
  328. */
  329. public static function getOne($id)
  330. {
  331. $info = self::get($id);
  332. if ($info) {
  333. if ($info['start_time'])
  334. $start_time = date('Y-m-d H:i:s', $info['start_time']);
  335. if ($info['stop_time'])
  336. $stop_time = date('Y-m-d H:i:s', $info['stop_time']);
  337. if (isset($start_time) && isset($stop_time))
  338. $info['section_time'] = [$start_time, $stop_time];
  339. else
  340. $info['section_time'] = [];
  341. unset($info['start_time'], $info['stop_time']);
  342. }
  343. if ($info['images'])
  344. $info['images'] = json_decode($info['images'], true);
  345. else
  346. $info['images'] = [];
  347. $info['price'] = floatval($info['price']);
  348. $info['postage'] = floatval($info['postage']);
  349. $info['weight'] = floatval($info['weight']);
  350. $info['volume'] = floatval($info['volume']);
  351. $info['description'] = StoreDescription::getDescription($id, 3);
  352. $info['attrs'] = self::attr_list($id);
  353. return $info;
  354. }
  355. public static function attr_list($id)
  356. {
  357. $productId = self::where('id', $id)->value('product_id');
  358. $combinationResult = StoreProductAttrResult::where('product_id', $id)->where('type', 3)->value('result');
  359. $items = json_decode($combinationResult, true)['attr'];
  360. $productAttr = self::get_attr($items, $productId, 0);
  361. $seckillAttr = self::get_attr($items, $id, 3);
  362. foreach ($productAttr as $pk => $pv) {
  363. foreach ($seckillAttr as &$sv) {
  364. if ($pv['detail'] == $sv['detail']) {
  365. $productAttr[$pk] = $sv;
  366. }
  367. }
  368. $productAttr[$pk]['detail'] = json_decode($productAttr[$pk]['detail']);
  369. }
  370. $attrs['items'] = $items;
  371. $attrs['value'] = $productAttr;
  372. foreach ($items as $key => $item) {
  373. $header[] = ['title' => $item['value'], 'key' => 'value' . ($key + 1), 'align' => 'center', 'minWidth' => 80];
  374. }
  375. $header[] = ['title' => '图片', 'slot' => 'pic', 'align' => 'center', 'minWidth' => 120];
  376. $header[] = ['title' => '拼团价', 'key' => 'price', 'type' => 1, 'align' => 'center', 'minWidth' => 80];
  377. $header[] = ['title' => '成本价', 'key' => 'cost', 'align' => 'center', 'minWidth' => 80];
  378. $header[] = ['title' => '原价', 'key' => 'ot_price', 'align' => 'center', 'minWidth' => 80];
  379. $header[] = ['title' => '库存', 'key' => 'stock', 'align' => 'center', 'minWidth' => 80];
  380. $header[] = ['title' => '限量', 'key' => 'quota', 'type' => 1, 'align' => 'center', 'minWidth' => 80];
  381. $header[] = ['title' => '重量(KG)', 'key' => 'weight', 'align' => 'center', 'minWidth' => 80];
  382. $header[] = ['title' => '体积(m³)', 'key' => 'volume', 'align' => 'center', 'minWidth' => 80];
  383. $header[] = ['title' => '商品编号', 'key' => 'bar_code', 'align' => 'center', 'minWidth' => 80];
  384. $attrs['header'] = $header;
  385. return $attrs;
  386. }
  387. public static function get_attr($attr, $id, $type)
  388. {
  389. $value = attr_format($attr)[1];
  390. $valueNew = [];
  391. $count = 0;
  392. foreach ($value as $key => $item) {
  393. $detail = $item['detail'];
  394. sort($item['detail'], SORT_STRING);
  395. $suk = implode(',', $item['detail']);
  396. $sukValue = StoreProductAttrValue::where('product_id', $id)->where('type', $type)->where('suk', $suk)->column('bar_code,cost,price,ot_price,stock,image as pic,weight,volume,brokerage,brokerage_two,quota', 'suk');
  397. if (count($sukValue)) {
  398. foreach (array_values($detail) as $k => $v) {
  399. $valueNew[$count]['value' . ($k + 1)] = $v;
  400. }
  401. $valueNew[$count]['detail'] = json_encode($detail);
  402. $valueNew[$count]['pic'] = $sukValue[$suk]['pic'] ?? '';
  403. $valueNew[$count]['price'] = $sukValue[$suk]['price'] ? floatval($sukValue[$suk]['price']) : 0;
  404. $valueNew[$count]['cost'] = $sukValue[$suk]['cost'] ? floatval($sukValue[$suk]['cost']) : 0;
  405. $valueNew[$count]['ot_price'] = isset($sukValue[$suk]['ot_price']) ? floatval($sukValue[$suk]['ot_price']) : 0;
  406. $valueNew[$count]['stock'] = $sukValue[$suk]['stock'] ? intval($sukValue[$suk]['stock']) : 0;
  407. $valueNew[$count]['quota'] = $sukValue[$suk]['quota'] ? intval($sukValue[$suk]['quota']) : 0;
  408. $valueNew[$count]['bar_code'] = $sukValue[$suk]['bar_code'] ?? '';
  409. $valueNew[$count]['weight'] = $sukValue[$suk]['weight'] ? floatval($sukValue[$suk]['weight']) : 0;
  410. $valueNew[$count]['volume'] = $sukValue[$suk]['volume'] ? floatval($sukValue[$suk]['volume']) : 0;
  411. $valueNew[$count]['brokerage'] = $sukValue[$suk]['brokerage'] ? floatval($sukValue[$suk]['brokerage']) : 0;
  412. $valueNew[$count]['brokerage_two'] = $sukValue[$suk]['brokerage_two'] ? floatval($sukValue[$suk]['brokerage_two']) : 0;
  413. $valueNew[$count]['_checked'] = $type != 0 ? true : false;
  414. $count++;
  415. }
  416. }
  417. return $valueNew;
  418. }
  419. }