StoreCouponUser.php 4.1 KB

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