Points.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. <?php
  2. /**
  3. * 积分
  4. */
  5. namespace app\common\model;
  6. use think\facade\Db;
  7. /**
  8. *
  9. *
  10. * ----------------------------------------------------------------------------
  11. *
  12. * 数据层模型
  13. */
  14. class Points extends BaseModel
  15. {
  16. public $page_info;
  17. /**
  18. * 操作积分
  19. * @access public
  20. * @author csdeshang
  21. * @param string $stage 操作阶段 regist(注册),login(登录),comments(评论),order(下单),system(系统),other(其他),pointorder(积分礼品兑换),app(同步积分兑换)
  22. * @param array $insertarr 该数组可能包含信息 array('pl_memberid'=>'会员编号','pl_membername'=>'会员名称','pl_adminid'=>'管理员编号','pl_adminname'=>'管理员名称','pl_points'=>'积分','pl_desc'=>'描述','orderprice'=>'订单金额','order_sn'=>'订单编号','order_id'=>'订单序号','point_ordersn'=>'积分兑换订单编号');
  23. * @param bool $if_repeat 是否可以重复记录的信息,true可以重复记录,false不可以重复记录,默认为true
  24. * @return bool
  25. */
  26. function savePointslog($stage, $insertarr, $if_repeat = true)
  27. {
  28. if (!$insertarr['pl_memberid']) {
  29. return false;
  30. }
  31. //记录原因文字
  32. switch ($stage) {
  33. case 'regist':
  34. if (!isset($insertarr['pl_desc'])) {
  35. $insertarr['pl_desc'] = '注册会员';
  36. }
  37. $insertarr['pl_points'] = intval(config('ds_config.points_reg'));
  38. break;
  39. case 'login':
  40. if (!isset($insertarr['pl_desc'])) {
  41. $insertarr['pl_desc'] = '会员登录';
  42. }
  43. $insertarr['pl_points'] = intval(config('ds_config.points_login'));
  44. break;
  45. case 'comments':
  46. if (!isset($insertarr['pl_desc'])) {
  47. $insertarr['pl_desc'] = '评论商品';
  48. }
  49. $insertarr['pl_points'] = intval(config('ds_config.points_comments'));
  50. break;
  51. case 'order':
  52. if (!isset($insertarr['pl_desc'])) {
  53. $insertarr['pl_desc'] = '订单' . $insertarr['order_sn'] . '购物消费';
  54. }
  55. $insertarr['pl_points'] = 0;
  56. if ($insertarr['orderprice']) {
  57. $insertarr['pl_points'] = @intval($insertarr['orderprice'] / config('ds_config.points_orderrate'));
  58. if ($insertarr['pl_points'] > intval(config('ds_config.points_ordermax'))) {
  59. $insertarr['pl_points'] = intval(config('ds_config.points_ordermax'));
  60. }
  61. }
  62. //订单添加赠送积分列
  63. $obj_order = model('order');
  64. $data = array();
  65. $data['order_pointscount'] = Db::raw('order_pointscount+' . $insertarr['pl_points']);
  66. $obj_order->editOrdercommon($data, array('order_id' => $insertarr['order_id']));
  67. break;
  68. case 'system':
  69. case 'gift':
  70. break;
  71. case 'pointorder':
  72. if (!isset($insertarr['pl_desc'])) {
  73. $insertarr['pl_desc'] = '兑换礼品信息' . $insertarr['point_ordersn'] . '消耗积分';
  74. }
  75. break;
  76. case 'app':
  77. if (!isset($insertarr['pl_desc'])) {
  78. $insertarr['pl_desc'] = lang('points_pointorderdesc_app');
  79. }
  80. break;
  81. case 'signin':
  82. if (!isset($insertarr['pl_desc'])) {
  83. $insertarr['pl_desc'] = '签到得到积分';
  84. }
  85. break;
  86. case 'inviter':
  87. if (!isset($insertarr['pl_desc'])) {
  88. $insertarr['pl_desc'] = '邀请新会员[' . $insertarr['invited'] . ']注册';
  89. }
  90. $insertarr['pl_points'] = intval(config('ds_config.points_invite'));
  91. break;
  92. case 'rebate':
  93. if (!isset($insertarr['pl_desc'])) {
  94. $insertarr['pl_desc'] = '邀请人消费得到积分';
  95. }
  96. break;
  97. case 'marketmanage':
  98. break;
  99. case 'other':
  100. break;
  101. }
  102. $save_sign = true;
  103. if ($if_repeat == false) {
  104. //检测是否有相关信息存在,如果没有,入库
  105. $condition = array();
  106. $condition[] = array('pl_memberid', '=', $insertarr['pl_memberid']);
  107. $condition[] = array('pl_stage', '=', $stage);
  108. $log_array = self::getPointsInfo($condition);
  109. if (!empty($log_array)) {
  110. $save_sign = false;
  111. }
  112. }
  113. if ($save_sign == false) {
  114. return true;
  115. }
  116. //新增日志
  117. $value_array = array();
  118. $value_array['pl_memberid'] = $insertarr['pl_memberid'];
  119. $value_array['pl_membername'] = $insertarr['pl_membername'];
  120. if (isset($insertarr['pl_adminid'])) {
  121. $value_array['pl_adminid'] = $insertarr['pl_adminid'];
  122. }
  123. if (isset($insertarr['pl_adminname'])) {
  124. $value_array['pl_adminname'] = $insertarr['pl_adminname'];
  125. }
  126. $value_array['pl_points'] = $insertarr['pl_points'];
  127. $value_array['pl_addtime'] = TIMESTAMP;
  128. $value_array['pl_desc'] = $insertarr['pl_desc'];
  129. $value_array['pl_stage'] = $stage;
  130. $result = false;
  131. if ($value_array['pl_points'] != '0') {
  132. $result = self::addPointslog($value_array);
  133. }
  134. if ($result) {
  135. //更新member内容
  136. $obj_member = model('member');
  137. $upmember_array = array();
  138. $upmember_array['member_points'] = Db::raw('member_points+' . $insertarr['pl_points']);
  139. $obj_member->editMember(array('member_id' => $insertarr['pl_memberid']), $upmember_array, $insertarr['pl_memberid']);
  140. return true;
  141. } else {
  142. return false;
  143. }
  144. }
  145. /**
  146. * 添加积分日志信息
  147. * @access public
  148. * @author csdeshang
  149. * @param type $data 数据
  150. * @return boolean
  151. */
  152. public function addPointslog($data)
  153. {
  154. if (empty($data)) {
  155. return false;
  156. }
  157. $result = Db::name('pointslog')->insertGetId($data);
  158. return $result;
  159. }
  160. /**
  161. * 积分日志列表
  162. * @access public
  163. * @author csdeshang
  164. * @param type $condition
  165. * @param type $pagesize
  166. * @param type $field
  167. * @return type
  168. */
  169. public function getPointslogList($condition, $pagesize = '', $field = '*', $limit = 0, $order = 'pl_addtime desc')
  170. {
  171. if ($pagesize) {
  172. $result = Db::name('pointslog')->where($condition)->field($field)->order($order)->paginate(['list_rows' => $pagesize, 'query' => request()->param()], false);
  173. $this->page_info = $result;
  174. return $result->items();
  175. } else {
  176. return Db::name('pointslog')->where($condition)->field($field)->limit($limit)->order($order)->select()->toArray();
  177. }
  178. }
  179. /**
  180. * 积分日志详细信息
  181. * @access public
  182. * @author csdeshang
  183. * @param type $condition
  184. * @param type $field
  185. * @return type
  186. */
  187. public function getPointsInfo($condition, $field = '*')
  188. {
  189. //得到条件语句
  190. return Db::name('pointslog')->field($field)->where($condition)->find();
  191. }
  192. /**
  193. *
  194. */
  195. public function getPointsCount($condition)
  196. {
  197. return Db::name('pointslog')->where($condition)->count();
  198. }
  199. }