StoreExchange.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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' => self::getModelTime($where, $StoreOrderModel)->where('exchange_id', '<>', 0)->sum('total_num'),
  150. 'content' => '兑换成功商品总件数',
  151. 'background_color' => 'layui-bg-green',
  152. 'sum' => $StoreOrderModel->where('exchange_id', '<>', 0)->sum('total_num'),
  153. 'class' => 'fa fa-bar-chart',
  154. ],
  155. [
  156. 'name' => '缺货商品',
  157. 'field' => '件',
  158. 'count' => $lack,
  159. 'content' => '总商品数量',
  160. 'background_color' => 'layui-bg-orange',
  161. 'sum' => array_sum($sum),
  162. 'class' => 'fa fa-cube',
  163. ],
  164. ];
  165. }
  166. /**
  167. * 销量排行 top 10
  168. * layui-bg-red 红 layui-bg-orange 黄 layui-bg-green 绿 layui-bg-blue 蓝 layui-bg-cyan 黑
  169. */
  170. public static function getMaxList($where)
  171. {
  172. $classs = ['layui-bg-red', 'layui-bg-orange', 'layui-bg-green', 'layui-bg-blue', 'layui-bg-cyan'];
  173. $model = StoreOrder::alias('a')->join('store_exchange b', 'b.id=a.exchange_id')->where('a.paid', 1);
  174. $list = self::getModelTime($where, $model, 'a.add_time')->group('a.exchange_id')->order('p_count desc')->limit(10)
  175. ->field(['count(a.exchange_id) as p_count', 'b.title as store_name', 'sum(b.price) as sum_price'])->select();
  176. if (count($list)) $list = $list->toArray();
  177. $maxList = [];
  178. $sum_count = 0;
  179. $sum_price = 0;
  180. foreach ($list as $item) {
  181. $sum_count += $item['p_count'];
  182. $sum_price = bcadd($sum_price, $item['sum_price'], 2);
  183. }
  184. unset($item);
  185. foreach ($list as $key => &$item) {
  186. $item['w'] = bcdiv($item['p_count'], $sum_count, 2) * 100;
  187. $item['class'] = isset($classs[$key]) ? $classs[$key] : (isset($classs[$key - count($classs)]) ? $classs[$key - count($classs)] : '');
  188. $item['store_name'] = self::getSubstrUTf8($item['store_name']);
  189. }
  190. $maxList['sum_count'] = $sum_count;
  191. $maxList['sum_price'] = $sum_price;
  192. $maxList['list'] = $list;
  193. return $maxList;
  194. }
  195. /**
  196. * 获取秒杀利润
  197. * @param $where
  198. * @return array
  199. */
  200. public static function ProfityTop10($where)
  201. {
  202. $classs = ['layui-bg-red', 'layui-bg-orange', 'layui-bg-green', 'layui-bg-blue', 'layui-bg-cyan'];
  203. $model = StoreOrder::alias('a')->join('store_exchange b', 'b.id = a.exchange_id')->where('a.paid', 1);
  204. $list = self::getModelTime($where, $model, 'a.add_time')->group('a.exchange_id')->order('profity desc')->limit(10)
  205. ->field(['count(a.exchange_id) as p_count', 'b.title as store_name', 'sum(b.price) as sum_price', '(b.price-b.cost) as profity'])
  206. ->select();
  207. if (count($list)) $list = $list->toArray();
  208. $maxList = [];
  209. $sum_count = 0;
  210. $sum_price = 0;
  211. foreach ($list as $item) {
  212. $sum_count += $item['p_count'];
  213. $sum_price = bcadd($sum_price, $item['sum_price'], 2);
  214. }
  215. foreach ($list as $key => &$item) {
  216. $item['w'] = bcdiv($item['sum_price'], $sum_price, 2) * 100;
  217. $item['class'] = isset($classs[$key]) ? $classs[$key] : (isset($classs[$key - count($classs)]) ? $classs[$key - count($classs)] : '');
  218. $item['store_name'] = self::getSubstrUTf8($item['store_name'], 30);
  219. }
  220. $maxList['sum_count'] = $sum_count;
  221. $maxList['sum_price'] = $sum_price;
  222. $maxList['list'] = $list;
  223. return $maxList;
  224. }
  225. /**
  226. * 获取秒杀缺货
  227. * @param $where
  228. * @return array
  229. */
  230. public static function getLackList($where)
  231. {
  232. $replenishment_num = sys_config('replenishment_num');
  233. $replenishment_num = $replenishment_num > 0 ? $replenishment_num : 20;
  234. $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();
  235. if (count($list)) $list = $list->toArray();
  236. $count = self::where('stock', '<', $replenishment_num)->count();
  237. return ['count' => $count, 'data' => $list];
  238. }
  239. /**
  240. * 秒杀产品评价
  241. * @param array $where
  242. * @return array
  243. */
  244. public static function getNegativeList($where = array())
  245. {
  246. $replenishment_num = 3;
  247. return [];
  248. }
  249. /**
  250. * 秒杀产品退货
  251. * @param array $where
  252. * @return mixed
  253. */
  254. public static function getBargainRefundList($where = array())
  255. {
  256. $model = StoreOrder::alias('a')->join('store_exchange b', 'b.id=a.exchange_id');
  257. $list = self::getModelTime($where, $model, 'a.add_time')->where('a.refund_status', '<>', 0)->group('a.exchange_id')->order('count desc')->page((int)$where['page'], (int)$where['limit'])
  258. ->field(['count(a.exchange_id) as count', 'b.title as store_name', 'sum(b.price) as sum_price'])->select();
  259. if (count($list)) $list = $list->toArray();
  260. return $list;
  261. }
  262. /**
  263. * @param $where
  264. * @return array
  265. */
  266. public static function systemPage($where)
  267. {
  268. $model = new self;
  269. $model = $model->alias('s');
  270. // $model = $model->join('StoreProduct p','p.id=s.product_id');
  271. if ($where['status'] != '') $model = $model->where('s.status', $where['status']);
  272. if ($where['store_name'] != '') $model = $model->where('s.title|s.id', 'LIKE', "%$where[store_name]%");
  273. $model = $model->page(bcmul($where['page'], $where['limit'], 0), $where['limit']);
  274. $model = $model->order('s.id desc');
  275. $model = $model->where('s.is_del', 0);
  276. return self::page($model, function ($item) {
  277. $item['store_name'] = StoreProduct::where('id', $item['product_id'])->value('store_name');
  278. $item['start_name'] = $item['status'] ? '开启' : '关闭';
  279. $item['money_type'] = get_money_name($item['money_type']);
  280. }, $where, $where['limit']);
  281. }
  282. public static function SaveExcel($where)
  283. {
  284. $model = new self;
  285. if ($where['status'] != '') $model = $model->where('status', $where['status']);
  286. if ($where['store_name'] != '') $model = $model->where('title|id', 'LIKE', "%$where[store_name]%");
  287. $list = $model->order('id desc')->where('is_del', 0)->select();
  288. count($list) && $list = $list->toArray();
  289. $excel = [];
  290. foreach ($list as $item) {
  291. $item['store_name'] = StoreProduct::where('id', $item['product_id'])->value('store_name');
  292. $excel[] = [
  293. $item['id'],
  294. $item['title'],
  295. $item['info'],
  296. $item['ot_price'],
  297. $item['price'],
  298. $item['stock'],
  299. $item['money'],
  300. get_money_name($item['money_type']),
  301. $item['status'] ? '开启' : '关闭',
  302. ];
  303. }
  304. PHPExcelService::setExcelHeader(['编号', '活动标题', '活动简介', '原价', '兑换价', '库存', '活动状态', '兑换代币', '兑换币种', '结束时间', '状态'])
  305. ->setExcelTile('兑换产品导出', ' ', ' 生成时间:' . date('Y-m-d H:i:s', time()))
  306. ->setExcelContent($excel)
  307. ->ExcelSave();
  308. }
  309. /**
  310. * 获取秒杀产品id
  311. * @return array
  312. */
  313. public static function getExchangeIdAll()
  314. {
  315. return self::where('is_del', 0)->column('id', 'id');
  316. }
  317. /**
  318. * 获取秒杀的所有产品
  319. * @return int|string
  320. */
  321. public static function getExchangeCount()
  322. {
  323. return self::where('is_del', 0)->count();
  324. }
  325. /**
  326. * TODO 获取某个字段值
  327. * @param $id
  328. * @param string $field
  329. * @return mixed
  330. */
  331. public static function getExchangeField($id, $field = 'title')
  332. {
  333. return self::where('id', $id)->value($field);
  334. }
  335. }