StoreBargain.php 14 KB

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