StoreProduct.php 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  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\store;
  12. use app\admin\model\system\RecommendRelation;
  13. use app\admin\model\wechat\WechatUser;
  14. use think\Db;
  15. use traits\ModelTrait;
  16. use basic\ModelBasic;
  17. use app\admin\model\store\StoreCategory as CategoryModel;
  18. use service\SystemConfigService;
  19. use app\admin\model\questions\Relation;
  20. use app\admin\model\special\SpecialBuy;
  21. use app\admin\model\special\Special;
  22. use app\admin\model\special\SpecialSource;
  23. use app\admin\model\order\StoreOrder;
  24. use app\admin\model\system\SystemConfig;
  25. use service\PhpSpreadsheetService;
  26. use app\admin\model\questions\TestPaperObtain;
  27. use app\admin\model\download\DataDownloadBuy;
  28. use app\admin\model\merchant\Merchant;
  29. use service\WechatTemplateService;
  30. use app\wap\model\routine\RoutineTemplate;
  31. /**
  32. * 产品管理 model
  33. * Class StoreProduct
  34. * @package app\admin\model\store
  35. */
  36. class StoreProduct extends ModelBasic
  37. {
  38. use ModelTrait;
  39. /**
  40. * 获取连表查询条件
  41. * @param $type
  42. * @return array
  43. */
  44. public static function setData($type)
  45. {
  46. $store_stock = SystemConfig::getValue('store_stock');
  47. switch ((int)$type) {
  48. case 1:
  49. $data = ['p.is_show' => 1, 'p.is_del' => 0];
  50. break;
  51. case 2:
  52. $data = ['p.is_show' => 0, 'p.is_del' => 0];
  53. break;
  54. case 3:
  55. $data = ['p.is_del' => 0];
  56. break;
  57. case 4:
  58. $data = ['p.is_show' => 1, 'p.is_del' => 0, 'p.stock' => 0];
  59. break;
  60. case 5:
  61. $data = ['p.is_show' => 1, 'p.is_del' => 0, 'p.stock' => ['elt', $store_stock]];
  62. break;
  63. case 6:
  64. $data = ['p.is_del' => 1];
  65. break;
  66. };
  67. return isset($data) ? $data : [];
  68. }
  69. public static function PreWhere($alert = '')
  70. {
  71. $alert = $alert ? $alert . '.' : '';
  72. return self::where([$alert . 'is_show' => 1, $alert . 'status' => 1, $alert . 'is_del' => 0]);
  73. }
  74. /**
  75. * 获取连表MOdel
  76. * @param $model
  77. * @return object
  78. */
  79. public static function getModelObject($where = [])
  80. {
  81. $model = new self();
  82. $model = $model->alias('p');
  83. if (!empty($where)) {
  84. $model = $model->group('p.id');
  85. if (isset($where['type']) && $where['type'] != '' && ($data = self::setData($where['type']))) {
  86. $model = $model->where($data);
  87. }
  88. if (isset($where['store_name']) && $where['store_name'] != '') {
  89. $model = $model->where('p.store_name|p.keyword|p.id', 'LIKE', "%$where[store_name]%");
  90. }
  91. if (isset($where['cate_id']) && trim($where['cate_id']) != 0) {
  92. $model = $model->where('p.cate_id', $where['cate_id']);
  93. }
  94. if (isset($where['mer_id']) && trim($where['mer_id']) != '') {
  95. $model = $model->where('p.mer_id', $where['mer_id']);
  96. }
  97. if (isset($where['order']) && $where['order'] != '') {
  98. $model = $model->order(self::setOrder($where['order']));
  99. } else {
  100. $model = $model->order('p.sort DESC,p.add_time DESC');
  101. }
  102. }
  103. $model = $model->join('StoreCategory c', 'p.cate_id=c.id', 'left');
  104. return $model->where('p.status', 1);
  105. }
  106. /**
  107. * 获取商品
  108. */
  109. public static function storeProductList($where, $special_source)
  110. {
  111. $where['store_name'] = $where['title'];
  112. $model = self::getModelObject($where)->where('p.id', 'not in', $special_source)->field(['p.*']);
  113. $model = $model->page((int)$where['page'], (int)$where['limit']);
  114. $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
  115. $count = self::getModelObject($where)->where('p.id', 'not in', $special_source)->count();
  116. return compact('count', 'data');
  117. }
  118. /*
  119. * 获取产品列表
  120. * @param $where array
  121. * @return array
  122. *
  123. */
  124. public static function ProductList($where)
  125. {
  126. $model = self::getModelObject($where)->field('p.*,c.cate_name');
  127. if ($where['excel'] == 0) $model = $model->page((int)$where['page'], (int)$where['limit']);
  128. $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
  129. foreach ($data as &$item) {
  130. $item['recommend'] = RecommendRelation::where('a.link_id', $item['id'])->where('a.type', 4)->alias('a')
  131. ->join('__RECOMMEND__ r', 'a.recommend_id=r.id')->column('a.id,r.title');
  132. if ($item['mer_id']) {
  133. $item['mer_name'] = Merchant::where('id', $item['mer_id'])->value('mer_name');
  134. } else {
  135. $item['mer_name'] = '总平台';
  136. }
  137. }
  138. if ($where['excel'] == 1) {
  139. self::SaveExcel($data);
  140. }
  141. $count = self::getModelObject($where)->count();
  142. return compact('count', 'data');
  143. }
  144. public static function SaveExcel($data)
  145. {
  146. $export = [];
  147. foreach ($data as $index => $item) {
  148. $export[] = [
  149. $item['store_name'],
  150. $item['store_info'],
  151. $item['cate_name'],
  152. '¥' . $item['price'],
  153. $item['stock'],
  154. $item['sales'],
  155. ];
  156. }
  157. $filename = '产品导出' . time() . '.xlsx';
  158. $head = ['产品名称', '产品简介', '产品分类', '价格', '库存', '销量'];
  159. PhpSpreadsheetService::outdata($filename, $export, $head);
  160. }
  161. /**
  162. * 获取连表MOdel
  163. * @param $model
  164. * @return object
  165. */
  166. public static function getModelExamine($where = [])
  167. {
  168. $model = new self();
  169. $model = $model->alias('p');
  170. $model = $model->group('p.id');
  171. if (isset($where['store_name']) && $where['store_name'] != '') {
  172. $model = $model->where('p.store_name|p.keyword|p.id', 'LIKE', "%$where[store_name]%");
  173. }
  174. if (isset($where['cate_id']) && trim($where['cate_id']) != 0) {
  175. $model = $model->where('p.cate_id', $where['cate_id']);
  176. }
  177. if (isset($where['mer_id']) && trim($where['mer_id']) != '') {
  178. $model = $model->where('p.mer_id', $where['mer_id']);
  179. }
  180. if (isset($where['order']) && $where['order'] != '') {
  181. $model = $model->order(self::setOrder($where['order']));
  182. } else {
  183. $model = $model->order('p.sort DESC,p.add_time DESC');
  184. }
  185. $model = $model->join('StoreCategory c', 'p.cate_id=c.id', 'left');
  186. if (isset($where['status']) && $where['status'] != '') {
  187. $model = $model->where('status', $where['status']);
  188. } else {
  189. $model = $model->where('status', 'in', [-1, 0]);
  190. }
  191. return $model;
  192. }
  193. /*
  194. * 获取产品列表
  195. * @param $where array
  196. * @return array
  197. *
  198. */
  199. public static function productExamineList($where)
  200. {
  201. $model = self::getModelExamine($where)->field('p.*,c.cate_name');
  202. $model = $model->page((int)$where['page'], (int)$where['limit']);
  203. $data = ($data = $model->select()) && count($data) ? $data->toArray() : [];
  204. foreach ($data as &$item) {
  205. $item['fail_time'] = date('Y-m-d H:i:s', $item['fail_time']);
  206. if ($item['mer_id']) {
  207. $item['mer_name'] = Merchant::where('id', $item['mer_id'])->value('mer_name');
  208. } else {
  209. $item['mer_name'] = '总平台';
  210. }
  211. }
  212. $count = self::getModelExamine($where)->count();
  213. return compact('count', 'data');
  214. }
  215. /**审核失败
  216. * @param $id
  217. * @param $fail_msg
  218. * @return bool
  219. * @throws \think\exception\DbException
  220. */
  221. public static function changeFail($id, $mer_id, $fail_message)
  222. {
  223. $fail_time = time();
  224. $status = -1;
  225. $uid = Merchant::where('id', $mer_id)->value('uid');
  226. try {
  227. $wechat_notification_message = SystemConfigService::get('wechat_notification_message');
  228. if ($wechat_notification_message == 1) {
  229. WechatTemplateService::sendTemplate(WechatUser::uidToOpenid($uid), WechatTemplateService::EXAMINE_RESULT, [
  230. 'first' => '尊敬的讲师,您添加的商品审核结果已出。',
  231. 'keyword1' => '审核失败',
  232. 'keyword2' => date('Y-m-d H:i:s', time()),
  233. 'remark' => '失败原因:' . $fail_message
  234. ], '');
  235. } else {
  236. $dat['phrase5']['value'] = '商品审核失败';
  237. $dat['time24']['value'] = date('Y-m-d H:i:s', time());
  238. $dat['thing4']['value'] = '商品失败原因:' . $fail_message;
  239. RoutineTemplate::sendExamineResult($dat, $uid, '');
  240. }
  241. } catch (\Exception $e) {
  242. }
  243. return self::edit(compact('fail_time', 'fail_message', 'status'), $id);
  244. }
  245. /**审核成功
  246. * @param $id
  247. * @return bool
  248. */
  249. public static function changeSuccess($id, $mer_id)
  250. {
  251. $success_time = time();
  252. $status = 1;
  253. $uid = Merchant::where('id', $mer_id)->value('uid');
  254. try {
  255. $wechat_notification_message = SystemConfigService::get('wechat_notification_message');
  256. if ($wechat_notification_message == 1) {
  257. WechatTemplateService::sendTemplate(WechatUser::uidToOpenid($uid), WechatTemplateService::EXAMINE_RESULT, [
  258. 'first' => '尊敬的讲师,您添加的商品审核结果已出。',
  259. 'keyword1' => '审核成功',
  260. 'keyword2' => date('Y-m-d H:i:s', time()),
  261. 'remark' => '感恩您的努力付出,谢谢!'
  262. ], '');
  263. } else {
  264. $dat['phrase5']['value'] = '商品审核成功';
  265. $dat['time24']['value'] = date('Y-m-d H:i:s', time());
  266. $dat['thing4']['value'] = '您添加的商品审核结果已出!';
  267. RoutineTemplate::sendExamineResult($dat, $uid, '');
  268. }
  269. } catch (\Exception $e) {
  270. }
  271. return self::edit(compact('status', 'success_time'), $id);
  272. }
  273. public static function getChatrdata($type, $data)
  274. {
  275. $legdata = ['销量', '数量', '点赞', '收藏'];
  276. $model = self::setWhereType(self::order('id desc'), $type);
  277. $list = self::getModelTime(compact('data'), $model)
  278. ->field('FROM_UNIXTIME(add_time,"%Y-%c-%d") as un_time,count(id) as count,sum(sales) as sales')
  279. ->group('un_time')
  280. ->distinct(true)
  281. ->select()
  282. ->each(function ($item) use ($data) {
  283. $item['collect'] = self::getModelTime(compact('data'), new StoreProductRelation)->where(['type' => 'collect'])->count();
  284. $item['like'] = self::getModelTime(compact('data'), new StoreProductRelation)->where(['type' => 'like'])->count();
  285. })->toArray();
  286. $chatrList = [];
  287. $datetime = [];
  288. $data_item = [];
  289. $itemList = [0 => [], 1 => [], 2 => [], 3 => []];
  290. foreach ($list as $item) {
  291. $itemList[0][] = $item['sales'];
  292. $itemList[1][] = $item['count'];
  293. $itemList[2][] = $item['like'];
  294. $itemList[3][] = $item['collect'];
  295. array_push($datetime, $item['un_time']);
  296. }
  297. foreach ($legdata as $key => $leg) {
  298. $data_item['name'] = $leg;
  299. $data_item['type'] = 'line';
  300. $data_item['data'] = $itemList[$key];
  301. $chatrList[] = $data_item;
  302. unset($data_item);
  303. }
  304. unset($leg);
  305. $badge = self::getbadge(compact('data'), $type);
  306. $count = self::setWhereType(self::getModelTime(compact('data'), new self()), $type)->count();
  307. return compact('datetime', 'chatrList', 'legdata', 'badge', 'count');
  308. }
  309. //获取 badge 内容
  310. public static function getbadge($where, $type)
  311. {
  312. $StoreOrderModel = new StoreOrder;
  313. $replenishment_num = SystemConfig::getValue('replenishment_num');
  314. $replenishment_num = $replenishment_num > 0 ? $replenishment_num : 20;
  315. $stock1 = self::getModelTime($where, new self())->where('stock', '<', $replenishment_num)->column('stock');
  316. $sum_stock = self::where('stock', '<', $replenishment_num)->column('stock');
  317. $stk = [];
  318. foreach ($stock1 as $item) {
  319. $stk[] = $replenishment_num - $item;
  320. }
  321. $lack = array_sum($stk);
  322. $sum = [];
  323. foreach ($sum_stock as $val) {
  324. $sum[] = $replenishment_num - $val;
  325. }
  326. return [
  327. [
  328. 'name' => '商品数量',
  329. 'field' => '件',
  330. 'count' => self::setWhereType(new self(), $type)->where('add_time', '<', mktime(0, 0, 0, date('m'), date('d'), date('Y')))->sum('stock'),
  331. 'content' => '商品数量总数',
  332. 'background_color' => 'layui-bg-blue',
  333. 'sum' => self::sum('stock'),
  334. 'class' => 'fa fa fa-ioxhost',
  335. ],
  336. [
  337. 'name' => '新增商品',
  338. 'field' => '件',
  339. 'count' => self::setWhereType(self::getModelTime($where, new self), $type)->where('is_new', 1)->sum('stock'),
  340. 'content' => '新增商品总数',
  341. 'background_color' => 'layui-bg-cyan',
  342. 'sum' => self::where('is_new', 1)->sum('stock'),
  343. 'class' => 'fa fa-line-chart',
  344. ],
  345. [
  346. 'name' => '活动商品',
  347. 'field' => '件',
  348. 'count' => self::getModelTime($where, $StoreOrderModel)->sum('total_num'),
  349. 'content' => '活动商品总数',
  350. 'background_color' => 'layui-bg-green',
  351. 'sum' => $StoreOrderModel->sum('total_num'),
  352. 'class' => 'fa fa-bar-chart',
  353. ],
  354. [
  355. 'name' => '缺货商品',
  356. 'field' => '件',
  357. 'count' => $lack,
  358. 'content' => '总商品数量',
  359. 'background_color' => 'layui-bg-orange',
  360. 'sum' => array_sum($sum),
  361. 'class' => 'fa fa-cube',
  362. ],
  363. ];
  364. }
  365. public static function setWhereType($model, $type)
  366. {
  367. switch ($type) {
  368. case 1:
  369. $data = ['is_show' => 1, 'is_del' => 0];
  370. break;
  371. case 2:
  372. $data = ['is_show' => 0, 'is_del' => 0];
  373. break;
  374. case 3:
  375. $data = ['is_del' => 0];
  376. break;
  377. case 4:
  378. $data = ['is_show' => 1, 'is_del' => 0, 'stock' => 0];
  379. break;
  380. case 5:
  381. $data = ['is_show' => 1, 'is_del' => 0, 'stock' => ['elt', 1]];
  382. break;
  383. case 6:
  384. $data = ['is_del' => 1];
  385. break;
  386. }
  387. if (isset($data)) $model = $model->where($data);
  388. return $model;
  389. }
  390. /*
  391. * layui-bg-red 红 layui-bg-orange 黄 layui-bg-green 绿 layui-bg-blue 蓝 layui-bg-cyan 黑
  392. * 销量排行 top 10
  393. */
  394. public static function getMaxList($where)
  395. {
  396. $classs = ['layui-bg-red', 'layui-bg-orange', 'layui-bg-green', 'layui-bg-blue', 'layui-bg-cyan'];
  397. $model = StoreOrder::alias('a')->join('StoreOrderCartInfo c', 'a.id=c.oid')->join('store_product b', 'b.id=c.product_id');
  398. $list = self::getModelTime($where, $model, 'a.add_time')->group('c.product_id')->order('p_count desc')->limit(10)
  399. ->field(['count(c.product_id) as p_count', 'b.store_name', 'sum(b.price) as sum_price'])->select();
  400. if (count($list)) $list = $list->toArray();
  401. $maxList = [];
  402. $sum_count = 0;
  403. $sum_price = 0;
  404. foreach ($list as $item) {
  405. $sum_count += $item['p_count'];
  406. $sum_price = bcadd($sum_price, $item['sum_price'], 2);
  407. }
  408. unset($item);
  409. foreach ($list as $key => &$item) {
  410. $item['w'] = bcdiv($item['p_count'], $sum_count, 2) * 100;
  411. $item['class'] = isset($classs[$key]) ? $classs[$key] : (isset($classs[$key - count($classs)]) ? $classs[$key - count($classs)] : '');
  412. $item['store_name'] = self::getSubstrUTf8($item['store_name']);
  413. }
  414. $maxList['sum_count'] = $sum_count;
  415. $maxList['sum_price'] = $sum_price;
  416. $maxList['list'] = $list;
  417. return $maxList;
  418. }
  419. //获取利润
  420. public static function ProfityTop10($where)
  421. {
  422. $classs = ['layui-bg-red', 'layui-bg-orange', 'layui-bg-green', 'layui-bg-blue', 'layui-bg-cyan'];
  423. $model = StoreOrder::alias('a')->join('StoreOrderCartInfo c', 'a.id=c.oid')->join('store_product b', 'b.id=c.product_id');
  424. $list = self::getModelTime($where, $model, 'a.add_time')->group('c.product_id')->order('profity desc')->limit(10)
  425. ->field(['count(c.product_id) as p_count', 'b.store_name', 'sum(b.price) as sum_price', '(b.price-b.cost) as profity'])
  426. ->select();
  427. if (count($list)) $list = $list->toArray();
  428. $maxList = [];
  429. $sum_count = 0;
  430. $sum_price = 0;
  431. foreach ($list as $item) {
  432. $sum_count += $item['p_count'];
  433. $sum_price = bcadd($sum_price, $item['sum_price'], 2);
  434. }
  435. foreach ($list as $key => &$item) {
  436. $item['w'] = bcdiv($item['sum_price'], $sum_price, 2) * 100;
  437. $item['class'] = isset($classs[$key]) ? $classs[$key] : (isset($classs[$key - count($classs)]) ? $classs[$key - count($classs)] : '');
  438. $item['store_name'] = self::getSubstrUTf8($item['store_name'], 30);
  439. }
  440. $maxList['sum_count'] = $sum_count;
  441. $maxList['sum_price'] = $sum_price;
  442. $maxList['list'] = $list;
  443. return $maxList;
  444. }
  445. //获取缺货
  446. public static function getLackList($where)
  447. {
  448. $replenishment_num = SystemConfig::getValue('replenishment_num');
  449. $replenishment_num = $replenishment_num > 0 ? $replenishment_num : 20;
  450. $list = self::where('stock', '<', $replenishment_num)->field(['id', 'store_name', 'stock', 'price'])->page((int)$where['page'], (int)$where['limit'])->order('stock asc')->select();
  451. if (count($list)) $list = $list->toArray();
  452. $count = self::where('stock', '<', $replenishment_num)->count();
  453. return ['count' => $count, 'data' => $list];
  454. }
  455. //获取差评
  456. public static function getnegativelist($where)
  457. {
  458. $list = self::alias('s')->join('StoreProductReply r', 's.id=r.product_id')
  459. ->field('s.id,s.store_name,s.price,count(r.product_id) as count')
  460. ->page((int)$where['page'], (int)$where['limit'])
  461. ->where('r.product_score', 1)
  462. ->order('count desc')
  463. ->group('r.product_id')
  464. ->select();
  465. if (count($list)) $list = $list->toArray();
  466. $count = self::alias('s')->join('StoreProductReply r', 's.id=r.product_id')->group('r.product_id')->where('r.product_score', 1)->count();
  467. return ['count' => $count, 'data' => $list];
  468. }
  469. public static function TuiProductList()
  470. {
  471. $perd = StoreOrder::alias('s')->join('StoreOrderCartInfo c', 's.id=c.oid')
  472. ->field('count(c.product_id) as count,c.product_id as id')
  473. ->group('c.product_id')
  474. ->where('s.status', -1)
  475. ->order('count desc')
  476. ->limit(10)
  477. ->select();
  478. if (count($perd)) $perd = $perd->toArray();
  479. foreach ($perd as &$item) {
  480. $item['store_name'] = self::where(['id' => $item['id']])->value('store_name');
  481. $item['price'] = self::where(['id' => $item['id']])->value('price');
  482. }
  483. return $perd;
  484. }
  485. //编辑库存
  486. public static function changeStock($stock, $productId)
  487. {
  488. return self::edit(compact('stock'), $productId);
  489. }
  490. public static function getTierList($model = null)
  491. {
  492. if ($model === null) $model = new self();
  493. return $model->field('id,store_name')->where('is_del', 0)->select()->toArray();
  494. }
  495. /**
  496. * 设置查询条件
  497. * @param array $where
  498. * @return array
  499. */
  500. public static function setWhere($where)
  501. {
  502. $time['data'] = '';
  503. if (isset($where['start_time']) && $where['start_time'] != '' && isset($where['end_time']) && $where['end_time'] != '') {
  504. $time['data'] = $where['start_time'] . ' - ' . $where['end_time'];
  505. } else {
  506. $time['data'] = isset($where['data']) ? $where['data'] : '';
  507. }
  508. $model = self::getModelTime($time, db('store_cart')->alias('a')->join('store_product b', 'a.product_id=b.id'), 'a.add_time');
  509. if (isset($where['title']) && $where['title'] != '') {
  510. $model = $model->where('b.store_name|b.id', 'like', "%$where[title]%");
  511. }
  512. return $model;
  513. }
  514. /**
  515. * 获取真实销量排行
  516. * @param array $where
  517. * @return array
  518. */
  519. public static function getSaleslists($where)
  520. {
  521. $data = self::setWhere($where)->where('a.is_pay', 1)
  522. ->group('a.product_id')
  523. ->field(['sum(a.cart_num) as num_product', 'b.store_name', 'b.image', 'b.price', 'b.id'])
  524. ->order('num_product desc')
  525. ->page((int)$where['page'], (int)$where['limit'])
  526. ->select();
  527. $count = self::setWhere($where)->where('a.is_pay', 1)->group('a.product_id')->count();
  528. foreach ($data as &$item) {
  529. $item['sum_price'] = bcdiv($item['num_product'], $item['price'], 2);
  530. }
  531. return compact('data', 'count');
  532. }
  533. /*
  534. * 单个商品详情的头部查询
  535. * $id 商品id
  536. * $where 条件
  537. */
  538. public static function getProductBadgeList($id, $where)
  539. {
  540. $data['data'] = $where;
  541. $list = self::setWhere($data)
  542. ->field(['sum(a.cart_num) as num_product', 'b.id', 'b.price'])
  543. ->where('a.is_pay', 1)
  544. ->group('a.product_id')
  545. ->order('num_product desc')
  546. ->select();
  547. //排名
  548. $ranking = 0;
  549. //销量
  550. $xiaoliang = 0;
  551. //销售额 数组
  552. $list_price = [];
  553. foreach ($list as $key => $item) {
  554. if ($item['id'] == $id) {
  555. $ranking = $key + 1;
  556. $xiaoliang = $item['num_product'];
  557. }
  558. $value['sum_price'] = $item['price'] * $item['num_product'];
  559. $value['id'] = $item['id'];
  560. $list_price[] = $value;
  561. }
  562. //排序
  563. $list_price = self::my_sort($list_price, 'sum_price', SORT_DESC);
  564. //销售额排名
  565. $rank_price = 0;
  566. //当前销售额
  567. $num_price = 0;
  568. if ($list_price !== false && is_array($list_price)) {
  569. foreach ($list_price as $key => $item) {
  570. if ($item['id'] == $id) {
  571. $num_price = $item['sum_price'];
  572. $rank_price = $key + 1;
  573. continue;
  574. }
  575. }
  576. }
  577. return [
  578. [
  579. 'name' => '销售额排名',
  580. 'field' => '名',
  581. 'count' => $rank_price,
  582. 'background_color' => 'layui-bg-blue',
  583. ],
  584. [
  585. 'name' => '销量排名',
  586. 'field' => '名',
  587. 'count' => $ranking,
  588. 'background_color' => 'layui-bg-blue',
  589. ],
  590. [
  591. 'name' => '商品销量',
  592. 'field' => '名',
  593. 'count' => $xiaoliang,
  594. 'background_color' => 'layui-bg-blue',
  595. ],
  596. [
  597. 'name' => '点赞次数',
  598. 'field' => '个',
  599. 'count' => db('store_product_relation')->where('product_id', $id)->where('type', 'like')->count(),
  600. 'background_color' => 'layui-bg-blue',
  601. ],
  602. [
  603. 'name' => '销售总额',
  604. 'field' => '元',
  605. 'count' => $num_price,
  606. 'background_color' => 'layui-bg-blue',
  607. 'col' => 12,
  608. ],
  609. ];
  610. }
  611. /*
  612. * 处理二维数组排序
  613. * $arrays 需要处理的数组
  614. * $sort_key 需要处理的key名
  615. * $sort_order 排序方式
  616. * $sort_type 类型 可不填写
  617. */
  618. public static function my_sort($arrays, $sort_key, $sort_order = SORT_ASC, $sort_type = SORT_NUMERIC)
  619. {
  620. if (is_array($arrays)) {
  621. foreach ($arrays as $array) {
  622. if (is_array($array)) {
  623. $key_arrays[] = $array[$sort_key];
  624. } else {
  625. return false;
  626. }
  627. }
  628. }
  629. if (isset($key_arrays)) {
  630. array_multisort($key_arrays, $sort_order, $sort_type, $arrays);
  631. return $arrays;
  632. }
  633. return false;
  634. }
  635. /*
  636. * 查询单个商品的销量曲线图
  637. *
  638. */
  639. public static function getProductCurve($where)
  640. {
  641. $list = self::setWhere($where)
  642. ->where('a.product_id', $where['id'])
  643. ->where('a.is_pay', 1)
  644. ->field(['FROM_UNIXTIME(a.add_time,"%Y-%m-%d") as _add_time', 'sum(a.cart_num) as num'])
  645. ->group('_add_time')
  646. ->order('_add_time asc')
  647. ->select();
  648. $seriesdata = [];
  649. $date = [];
  650. $zoom = '';
  651. foreach ($list as $item) {
  652. $date[] = $item['_add_time'];
  653. $seriesdata[] = $item['num'];
  654. }
  655. if (count($date) > $where['limit']) $zoom = $date[$where['limit'] - 5];
  656. return compact('seriesdata', 'date', 'zoom');
  657. }
  658. /*
  659. * 查询单个商品的销售列表
  660. *
  661. */
  662. public static function getSalelList($where)
  663. {
  664. return self::setWhere($where)
  665. ->where(['a.product_id' => $where['id'], 'a.is_pay' => 1])
  666. ->join('user c', 'c.uid=a.uid')
  667. ->field(['FROM_UNIXTIME(a.add_time,"%Y-%m-%d") as _add_time', 'c.nickname', 'b.price', 'a.id', 'a.cart_num as num'])
  668. ->page((int)$where['page'], (int)$where['limit'])
  669. ->select();
  670. }
  671. /**获取关联专题
  672. * @param int $id
  673. */
  674. public static function setAssociatedTopics($order)
  675. {
  676. if ($order['type'] != 2) return true;
  677. $product_id = Db::name('store_order_cart_info')->where('oid', $order['id'])->value('product_id');
  678. if (!$product_id) return false;
  679. $special_source = Relation::setWhere(5, $product_id)->column('relation_id');
  680. foreach ($special_source as $key => $special_id) {
  681. if (SpecialBuy::be(['uid' => $order['uid'], 'special_id' => $special_id, 'is_del' => 0, 'type' => 5])) continue;
  682. $special = Special::PreWhere()->where(['id'=>$special_id])->find();
  683. if (!$special) continue;
  684. if ($special['type'] == 5) {
  685. $special_source = SpecialSource::getSpecialSource($special['id']);
  686. if ($special_source) {
  687. foreach ($special_source as $k => $v) {
  688. $task_special = Special::PreWhere()->where(['id'=>$v['source_id']])->find();
  689. if (!$task_special) continue;
  690. if ($task_special['is_show'] == 1) {
  691. SpecialBuy::setBuySpecial('', $order['uid'], $v['source_id'], 5, $task_special['validity'], $special_id);
  692. }
  693. }
  694. }
  695. }
  696. SpecialBuy::setBuySpecial('', $order['uid'], $special_id, 5, $special['validity']);
  697. TestPaperObtain::setTestPaper('', $order['uid'], $special_id, 3);
  698. DataDownloadBuy::setDataDownload('', $order['uid'], $special_id, 2);
  699. }
  700. return true;
  701. }
  702. /**商品退款清除赠送专题
  703. * @param $oid
  704. * @return bool
  705. * @throws \think\db\exception\DataNotFoundException
  706. * @throws \think\db\exception\ModelNotFoundException
  707. * @throws \think\exception\DbException
  708. */
  709. public static function returnAssociatedTopics($oid)
  710. {
  711. $order = StoreOrder::where('id', $oid)->where('type', 2)->find();
  712. if (!$order) return true;
  713. $product_id = Db::name('store_order_cart_info')->where('oid', $oid)->value('product_id');
  714. if (!$product_id) return false;
  715. $special_source = Relation::setWhere(5, $product_id)->column('relation_id');
  716. foreach ($special_source as $key => $special_id) {
  717. $special = Special::PreWhere()->where(['id'=>$special_id])->find();
  718. if (!$special) continue;
  719. if ($special['type'] == SPECIAL_COLUMN) {
  720. $special_source = SpecialSource::getSpecialSource($special['id']);
  721. if ($special_source) {
  722. foreach ($special_source as $k => $v) {
  723. SpecialBuy::where(['uid' => $order['uid'], 'special_id' => $v['source_id'], 'type' => 5, 'is_del' => 0, 'column_id' => $special_id])->update(['is_del' => 1]);
  724. }
  725. }
  726. }
  727. SpecialBuy::where(['uid' => $order['uid'], 'special_id' => $special_id, 'type' => 5, 'is_del' => 0])->update(['is_del' => 1]);
  728. TestPaperObtain::delTestPaper('', $order['uid'], $special_id, 3);
  729. DataDownloadBuy::delDataDownload('', $order['uid'], $special_id, 2);
  730. }
  731. }
  732. }