StoreExchange.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. <?php
  2. /**
  3. * @author: xaboy<365615158@qq.com>
  4. * @day: 2017/11/11
  5. */
  6. namespace app\admin\model\ump;
  7. use app\admin\model\order\StoreOrder;
  8. use app\admin\model\store\StoreProductRelation;
  9. use app\admin\model\system\SystemGroupData;
  10. use crmeb\traits\ModelTrait;
  11. use crmeb\basic\BaseModel;
  12. use app\admin\model\store\StoreProduct;
  13. use crmeb\services\PHPExcelService;
  14. /**
  15. * Class StoreSeckill
  16. * @package app\admin\model\store
  17. */
  18. class StoreExchange extends BaseModel
  19. {
  20. /**
  21. * 数据表主键
  22. * @var string
  23. */
  24. protected $pk = 'id';
  25. /**
  26. * 模型名称
  27. * @var string
  28. */
  29. protected $name = 'store_exchange';
  30. use ModelTrait;
  31. public function getDescriptionAttr($value)
  32. {
  33. return htmlspecialchars_decode($value);
  34. }
  35. /**
  36. * 秒杀产品过滤条件
  37. * @param $model
  38. * @param $type
  39. * @return mixed
  40. */
  41. public static function setWhereType($model, $type)
  42. {
  43. switch ($type) {
  44. case 1:
  45. $data = ['status' => 0, 'is_del' => 0];
  46. break;
  47. case 2:
  48. $data = ['status' => 1, 'is_del' => 0];
  49. break;
  50. case 3:
  51. $data = ['status' => 1, 'is_del' => 0, 'stock' => 0];
  52. break;
  53. case 4:
  54. $data = ['status' => 1, 'is_del' => 0, 'stock' => ['elt', 1]];
  55. break;
  56. case 5:
  57. $data = ['is_del' => 1];
  58. break;
  59. }
  60. if (isset($data)) $model = $model->where($data);
  61. return $model;
  62. }
  63. /**
  64. * 秒杀产品数量 图标展示
  65. * @param $type
  66. * @param $data
  67. * @return array
  68. */
  69. public static function getChatrdata($type, $data)
  70. {
  71. $legdata = ['销量', '数量', '点赞', '收藏'];
  72. $model = self::setWhereType(self::order('id desc'), $type);
  73. $list = self::getModelTime(compact('data'), $model)
  74. ->field('FROM_UNIXTIME(add_time,"%Y-%c-%d") as un_time,count(id) as count,sum(sales) as sales')
  75. ->group('un_time')
  76. ->distinct(true)
  77. ->select()
  78. ->each(function ($item) use ($data) {
  79. $item['collect'] = self::getModelTime(compact('data'), new StoreProductRelation)->where('type', 'collect')->count();
  80. $item['like'] = self::getModelTime(compact('data'), new StoreProductRelation())->where('type', 'like')->count();
  81. })->toArray();
  82. $chatrList = [];
  83. $datetime = [];
  84. $data_item = [];
  85. $itemList = [0 => [], 1 => [], 2 => [], 3 => []];
  86. foreach ($list as $item) {
  87. $itemList[0][] = $item['sales'];
  88. $itemList[1][] = $item['count'];
  89. $itemList[2][] = $item['like'];
  90. $itemList[3][] = $item['collect'];
  91. array_push($datetime, $item['un_time']);
  92. }
  93. foreach ($legdata as $key => $leg) {
  94. $data_item['name'] = $leg;
  95. $data_item['type'] = 'line';
  96. $data_item['data'] = $itemList[$key];
  97. $chatrList[] = $data_item;
  98. unset($data_item);
  99. }
  100. unset($leg);
  101. $badge = self::getbadge(compact('data'), $type);
  102. $count = self::setWhereType(self::getModelTime(compact('data'), new self()), $type)->count();
  103. return compact('datetime', 'chatrList', 'legdata', 'badge', 'count');
  104. }
  105. /**
  106. * 秒杀产品数量
  107. * @param $where
  108. * @param $type
  109. * @return array
  110. */
  111. public static function getbadge($where, $type)
  112. {
  113. $StoreOrderModel = new StoreOrder();
  114. $replenishment_num = sys_config('replenishment_num');
  115. $replenishment_num = $replenishment_num > 0 ? $replenishment_num : 20;
  116. $stock1 = self::getModelTime($where, new self())->where('stock', '<', $replenishment_num)->column('stock', 'id');
  117. $sum_stock = self::where('stock', '<', $replenishment_num)->column('stock', 'id');
  118. $stk = [];
  119. foreach ($stock1 as $item) {
  120. $stk[] = $replenishment_num - $item;
  121. }
  122. $lack = array_sum($stk);
  123. $sum = [];
  124. foreach ($sum_stock as $val) {
  125. $sum[] = $replenishment_num - $val;
  126. }
  127. return [
  128. [
  129. 'name' => '商品种类',
  130. 'field' => '件',
  131. 'count' => self::setWhereType(new self(), $type)->where('add_time', '<', mktime(0, 0, 0, date('m'), date('d'), date('Y')))->count(),
  132. 'content' => '商品种类总数',
  133. 'background_color' => 'layui-bg-blue',
  134. 'sum' => self::count(),
  135. 'class' => 'fa fa fa-ioxhost',
  136. ],
  137. [
  138. 'name' => '新增商品',
  139. 'field' => '件',
  140. 'count' => self::setWhereType(self::getModelTime($where, new self), $type)->sum('stock'),
  141. 'content' => '新增商品总数',
  142. 'background_color' => 'layui-bg-cyan',
  143. 'sum' => self::where('status', 1)->sum('stock'),
  144. 'class' => 'fa fa-line-chart',
  145. ],
  146. [
  147. 'name' => '缺货商品',
  148. 'field' => '件',
  149. 'count' => $lack,
  150. 'content' => '总商品数量',
  151. 'background_color' => 'layui-bg-orange',
  152. 'sum' => array_sum($sum),
  153. 'class' => 'fa fa-cube',
  154. ],
  155. ];
  156. }
  157. /**
  158. * 销量排行 top 10
  159. * layui-bg-red 红 layui-bg-orange 黄 layui-bg-green 绿 layui-bg-blue 蓝 layui-bg-cyan 黑
  160. */
  161. public static function getMaxList($where)
  162. {
  163. $classs = ['layui-bg-red', 'layui-bg-orange', 'layui-bg-green', 'layui-bg-blue', 'layui-bg-cyan'];
  164. $model = StoreOrder::alias('a')->join('store_seckill b', 'b.id=a.seckill_id')->where('a.paid', 1);
  165. $list = self::getModelTime($where, $model, 'a.add_time')->group('a.seckill_id')->order('p_count desc')->limit(10)
  166. ->field(['count(a.seckill_id) as p_count', 'b.title as store_name', 'sum(b.price) as sum_price'])->select();
  167. if (count($list)) $list = $list->toArray();
  168. $maxList = [];
  169. $sum_count = 0;
  170. $sum_price = 0;
  171. foreach ($list as $item) {
  172. $sum_count += $item['p_count'];
  173. $sum_price = bcadd($sum_price, $item['sum_price'], 2);
  174. }
  175. unset($item);
  176. foreach ($list as $key => &$item) {
  177. $item['w'] = bcdiv($item['p_count'], $sum_count, 2) * 100;
  178. $item['class'] = isset($classs[$key]) ? $classs[$key] : (isset($classs[$key - count($classs)]) ? $classs[$key - count($classs)] : '');
  179. $item['store_name'] = self::getSubstrUTf8($item['store_name']);
  180. }
  181. $maxList['sum_count'] = $sum_count;
  182. $maxList['sum_price'] = $sum_price;
  183. $maxList['list'] = $list;
  184. return $maxList;
  185. }
  186. /**
  187. * 获取秒杀利润
  188. * @param $where
  189. * @return array
  190. */
  191. public static function ProfityTop10($where)
  192. {
  193. $classs = ['layui-bg-red', 'layui-bg-orange', 'layui-bg-green', 'layui-bg-blue', 'layui-bg-cyan'];
  194. $model = StoreOrder::alias('a')->join('store_seckill b', 'b.id = a.seckill_id')->where('a.paid', 1);
  195. $list = self::getModelTime($where, $model, 'a.add_time')->group('a.seckill_id')->order('profity desc')->limit(10)
  196. ->field(['count(a.seckill_id) as p_count', 'b.title as store_name', 'sum(b.price) as sum_price', '(b.price-b.cost) as profity'])
  197. ->select();
  198. if (count($list)) $list = $list->toArray();
  199. $maxList = [];
  200. $sum_count = 0;
  201. $sum_price = 0;
  202. foreach ($list as $item) {
  203. $sum_count += $item['p_count'];
  204. $sum_price = bcadd($sum_price, $item['sum_price'], 2);
  205. }
  206. foreach ($list as $key => &$item) {
  207. $item['w'] = bcdiv($item['sum_price'], $sum_price, 2) * 100;
  208. $item['class'] = isset($classs[$key]) ? $classs[$key] : (isset($classs[$key - count($classs)]) ? $classs[$key - count($classs)] : '');
  209. $item['store_name'] = self::getSubstrUTf8($item['store_name'], 30);
  210. }
  211. $maxList['sum_count'] = $sum_count;
  212. $maxList['sum_price'] = $sum_price;
  213. $maxList['list'] = $list;
  214. return $maxList;
  215. }
  216. /**
  217. * 获取秒杀缺货
  218. * @param $where
  219. * @return array
  220. */
  221. public static function getLackList($where)
  222. {
  223. $replenishment_num = sys_config('replenishment_num');
  224. $replenishment_num = $replenishment_num > 0 ? $replenishment_num : 20;
  225. $list = self::where('stock', '<', $replenishment_num)->field(['id', 'title as store_name', 'stock', 'price'])->page((int)$where['page'], (int)$where['limit'])->order('stock asc')->select();
  226. if (count($list)) $list = $list->toArray();
  227. $count = self::where('stock', '<', $replenishment_num)->count();
  228. return ['count' => $count, 'data' => $list];
  229. }
  230. /**
  231. * 秒杀产品评价
  232. * @param array $where
  233. * @return array
  234. */
  235. public static function getNegativeList($where = array())
  236. {
  237. $replenishment_num = 3;
  238. return [];
  239. }
  240. /**
  241. * 秒杀产品退货
  242. * @param array $where
  243. * @return mixed
  244. */
  245. public static function getBargainRefundList($where = array())
  246. {
  247. $model = StoreOrder::alias('a')->join('store_seckill b', 'b.id=a.seckill_id');
  248. $list = self::getModelTime($where, $model, 'a.add_time')->where('a.refund_status', '<>', 0)->group('a.seckill_id')->order('count desc')->page((int)$where['page'], (int)$where['limit'])
  249. ->field(['count(a.seckill_id) as count', 'b.title as store_name', 'sum(b.price) as sum_price'])->select();
  250. if (count($list)) $list = $list->toArray();
  251. return $list;
  252. }
  253. /**
  254. * @param $where
  255. * @return array
  256. */
  257. public static function systemPage($where)
  258. {
  259. $model = new self;
  260. $model = $model->alias('s');
  261. // $model = $model->join('StoreProduct p','p.id=s.product_id');
  262. if ($where['status'] != '') $model = $model->where('s.status', $where['status']);
  263. if ($where['store_name'] != '') $model = $model->where('s.title|s.id', 'LIKE', "%$where[store_name]%");
  264. $model = $model->page(bcmul($where['page'], $where['limit'], 0), $where['limit']);
  265. $model = $model->order('s.id desc');
  266. $model = $model->where('s.is_del', 0);
  267. return self::page($model, function ($item) {
  268. $item['store_name'] = StoreProduct::where('id', $item['product_id'])->value('store_name');
  269. if ($item['status']) {
  270. $item['start_name'] = '开启';
  271. } else $item['start_name'] = '关闭';
  272. }, $where, $where['limit']);
  273. }
  274. public static function SaveExcel($where)
  275. {
  276. $model = new self;
  277. if ($where['status'] != '') $model = $model->where('status', $where['status']);
  278. if ($where['store_name'] != '') $model = $model->where('title|id', 'LIKE', "%$where[store_name]%");
  279. $list = $model->order('id desc')->where('is_del', 0)->select();
  280. count($list) && $list = $list->toArray();
  281. $excel = [];
  282. foreach ($list as $item) {
  283. $item['store_name'] = StoreProduct::where('id', $item['product_id'])->value('store_name');
  284. $excel[] = [
  285. $item['id'],
  286. $item['title'],
  287. $item['info'],
  288. $item['ot_price'],
  289. $item['price'],
  290. $item['stock'],
  291. $item['status'] ? '开启' : '关闭',
  292. ];
  293. }
  294. PHPExcelService::setExcelHeader(['编号', '活动标题', '活动简介', '原价', '价格', '库存', '状态'])
  295. ->setExcelTile('兑换券产品导出', ' ', ' 生成时间:' . date('Y-m-d H:i:s', time()))
  296. ->setExcelContent($excel)
  297. ->ExcelSave();
  298. }
  299. /**
  300. * 获取秒杀产品id
  301. * @return array
  302. */
  303. public static function getSeckillIdAll()
  304. {
  305. return self::where('is_del', 0)->column('id', 'id');
  306. }
  307. /**
  308. * 获取秒杀的所有产品
  309. * @return int|string
  310. */
  311. public static function getSeckillCount()
  312. {
  313. return self::where('is_del', 0)->count();
  314. }
  315. /**
  316. * TODO 获取某个字段值
  317. * @param $id
  318. * @param string $field
  319. * @return mixed
  320. */
  321. public static function getSeckillField($id, $field = 'title')
  322. {
  323. return self::where('id', $id)->value($field);
  324. }
  325. /**
  326. * 判断产品当前时间段是否有秒杀活动
  327. * @param $product_id
  328. * @param $time_id
  329. * @return array|null|\think\Model
  330. * @throws \think\db\exception\DataNotFoundException
  331. * @throws \think\db\exception\DbException
  332. * @throws \think\db\exception\ModelNotFoundException
  333. */
  334. public static function checkSeckill($product_id, $time_id)
  335. {
  336. return self::where('product_id', $product_id)->where('time_id', $time_id)->where('is_del', 0)->find();
  337. }
  338. }