SystemStoreMember.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. namespace app\models\system;
  3. use app\models\user\User;
  4. use crmeb\basic\BaseModel;
  5. use crmeb\traits\ModelTrait;
  6. class SystemStoreMember extends BaseModel
  7. {
  8. use ModelTrait;
  9. /**
  10. * 获取门店会员卡
  11. * @param int $reg_store_id
  12. * @return string
  13. */
  14. private static function getnextcardno($reg_store_id = 1)
  15. {
  16. $max = self::where('reg_store_id', $reg_store_id)->value('max(card_no)');
  17. $id = bcadd(substr($max, 4), 1);
  18. return str_pad($reg_store_id, "4", "0", STR_PAD_LEFT) . str_pad($id, 8, "0", STR_PAD_LEFT);
  19. }
  20. /**
  21. * 设置会员卡
  22. * @param $uid
  23. * @param int $reg_store_id
  24. * @return SystemStoreMember|\think\Model
  25. */
  26. public static function setstorecardno($uid, $reg_store_id = 1)
  27. {
  28. $card_no = self::getnextcardno($reg_store_id);
  29. $stock_rights = 1;
  30. User::where('uid', $uid)->update(['reg_store_id' => $reg_store_id]);
  31. return self::create(compact('uid', 'reg_store_id', 'card_no', 'stock_rights'));
  32. }
  33. /**
  34. * 获取用户会员卡
  35. * @param $uid
  36. * @return mixed
  37. */
  38. public static function getcardno($uid)
  39. {
  40. return self::where('uid', $uid)->value('card_no');
  41. }
  42. /**
  43. * 增加消费
  44. * @param $uid
  45. * @param $total_price
  46. */
  47. public static function add_consume($uid, $total_price)
  48. {
  49. $info = self::where('uid', $uid)->find();
  50. if (date("Y", time()) != $info['year']) {
  51. $consume_rights = bcsub(floor(bcdiv(bcadd($total_price, 0, 2), 1000, 2)), $info['use_consume_rights']);
  52. $data['year'] = date("Y");
  53. $data['consume_year'] = $total_price;
  54. return self::where('uid', $uid)->inc('consume_sum', $total_price)->inc('consume_rights', $consume_rights)->update($data);
  55. } else {
  56. $consume_rights = bcsub(floor(bcdiv(bcadd($total_price, $info['consume_sum'], 2), 1000, 2)), bcadd($info['use_consume_rights'], $info['consume_rights']));
  57. return self::where('uid', $uid)->inc('consume_sum', $total_price)->inc('consume_year', $total_price)->inc('consume_rights', $consume_rights)->update();
  58. }
  59. }
  60. public static function lst($where)
  61. {
  62. $model = new self;
  63. $model = $model->alias("a")->join("user b", "a.uid=b.uid", "left");
  64. if ($where['store_id'] > 0) $model = $model->where('a.reg_store_id', $where['store_id']);
  65. if ($where['key']) $model = $model->wherelike('b.phone|a.card_no', "%" . $where['key'] . "%");
  66. $model = $model->field('a.*,b.nickname,b.phone,b.avatar,b.now_money,b.consumer,b.integral');
  67. $count = $model->count();
  68. $data = $model->page($where['page'], $where['limit'])->order("a.uid desc")->select()->toarray();
  69. return compact('count', 'data');
  70. }
  71. }