StoreCouponUser.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. namespace app\admin\model\store;
  3. use basic\ModelBasic;
  4. use traits\ModelTrait;
  5. class StoreCouponUser extends ModelBasic
  6. {
  7. use ModelTrait;
  8. public static function tidyCouponList($couponList)
  9. {
  10. $time = time();
  11. foreach ($couponList as &$coupon){
  12. $coupon['_add_time'] = date('Y/m/d',$coupon['add_time']);
  13. $coupon['_end_time'] = date('Y/m/d',$coupon['end_time']);
  14. $coupon['use_min_price'] = floatval($coupon['use_min_price']);
  15. $coupon['coupon_price'] = floatval($coupon['coupon_price']);
  16. if($coupon['is_fail']){
  17. $coupon['_type'] = 0;
  18. $coupon['_msg'] = '已失效';
  19. }else if ($coupon['status'] == 1){
  20. $coupon['_type'] = 0;
  21. $coupon['_msg'] = '已使用';
  22. }else if ($coupon['status'] == 2){
  23. $coupon['_type'] = 0;
  24. $coupon['_msg'] = '已过期';
  25. }else if($coupon['add_time'] > $time || $coupon['end_time'] < $time){
  26. $coupon['_type'] = 0;
  27. $coupon['_msg'] = '已过期';
  28. }else{
  29. if($coupon['add_time']+ 3600*24 > $time){
  30. $coupon['_type'] = 2;
  31. $coupon['_msg'] = '可使用';
  32. }else{
  33. $coupon['_type'] = 1;
  34. $coupon['_msg'] = '可使用';
  35. }
  36. }
  37. $coupon['integral']=db('store_coupon')->where(['id'=>$coupon['cid']])->value('integral');
  38. }
  39. return $couponList;
  40. }
  41. //获取个人优惠券列表
  42. public static function getOneCouponsList($where){
  43. $list=self::where(['uid'=>$where['uid']])->page((int)$where['page'],(int)$where['limit'])->select();
  44. return self::tidyCouponList($list);
  45. }
  46. //获取优惠劵头部信息
  47. public static function getCouponBadgeList($where){
  48. return [
  49. [
  50. 'name'=>'总发放优惠券',
  51. 'field'=>'张',
  52. 'count'=>self::getModelTime($where,db('store_coupon_issue'))->where('status',1)->sum('total_count'),
  53. 'background_color'=>'layui-bg-blue',
  54. 'col'=>6,
  55. ],
  56. [
  57. 'name'=>'总使用优惠券',
  58. 'field'=>'张',
  59. 'count'=>self::getModelTime($where,new self())->where('status',1)->count(),
  60. 'background_color'=>'layui-bg-blue',
  61. 'col'=>6,
  62. ]
  63. ];
  64. }
  65. //获取优惠劵图表
  66. public static function getConponCurve($where,$limit=20){
  67. //优惠劵发放记录
  68. $list=self::getModelTime($where,db('store_coupon_issue')
  69. ->where('status',1)
  70. ->field(['FROM_UNIXTIME(add_time,"%Y-%m-%d") as _add_time','sum(total_count) as total_count'])->group('_add_time')->order('_add_time asc'))->select();
  71. $date=[];
  72. $seriesdata=[];
  73. $zoom='';
  74. foreach ($list as $item){
  75. $date[]=$item['_add_time'];
  76. $seriesdata[]=$item['total_count'];
  77. }
  78. unset($item);
  79. if(count($date)>$limit){
  80. $zoom=$date[$limit-5];
  81. }
  82. //优惠劵使用记录
  83. $componList=self::getModelTime($where,self::where('status',1)->field(['FROM_UNIXTIME(add_time,"%Y-%m-%d") as _add_time','sum(coupon_price) as coupon_price'])
  84. ->group('_add_time')->order('_add_time asc'))->select();
  85. count($componList) && $componList=$componList->toArray();
  86. $compon_date=[];
  87. $compon_data=[];
  88. $compon_zoom='';
  89. foreach($componList as $item){
  90. $compon_date[]=$item['_add_time'];
  91. $compon_data[]=$item['coupon_price'];
  92. }
  93. if(count($compon_date)>$limit){
  94. $compon_zoom=$compon_date[$limit-5];
  95. }
  96. return compact('date','seriesdata','zoom','compon_date','compon_data','compon_zoom');
  97. }
  98. }