StoreBargain.php 13 KB

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