IntegralModel.class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. <?php
  2. /**
  3. * lionfish 商城系统
  4. *
  5. *
  6. * @author fish
  7. *
  8. */
  9. namespace Seller\Model;
  10. class IntegralModel{
  11. /**
  12. *显示订单状态单位分页
  13. */
  14. public function show_order_page($search){
  15. $sql = "select p.pin_id,p.is_jiqi,og.goods_id,og.name,p.state,p.need_count,p.end_time,p.begin_time from ".C('DB_PREFIX')."pin as p,".C('DB_PREFIX')."pin_order as o,".C('DB_PREFIX')."order_goods as og
  16. where p.order_id= o.order_id and p.order_id = og.order_id
  17. ";
  18. if(isset($search['store_id'])){
  19. $sql.=" and og.store_id=".$search['store_id'];
  20. }
  21. if(isset($search['name']) && !empty($search['name']) ){
  22. $sql.=" and og.name like '%".$search['name']."%'";
  23. }
  24. if(isset($search['state'])){
  25. if($search['state'] == -1)
  26. {
  27. } else if($search['state'] == 0) {
  28. $sql.=" and p.state=0 and p.end_time > ".time();
  29. } else if($search['state'] == 1) {
  30. $sql.=" and p.state=1";
  31. } else if($search['state'] == 2) {
  32. $sql.=" and (p.state=2 or (p.state = 0 and p.end_time <".time()." ) )";
  33. }
  34. }
  35. $count=count(M()->query($sql));
  36. $Page = new \Think\Page($count,C('BACK_PAGE_NUM'));
  37. $show = $Page->show();// 分页显示输出
  38. $sql.=' ORDER BY p.pin_id desc LIMIT '.$Page->firstRow.','.$Page->listRows;
  39. $list=M()->query($sql);
  40. foreach($list as $key => $val)
  41. {
  42. $sql = "select count(o.order_id) as count from ".C('DB_PREFIX')."pin_order as po,".C('DB_PREFIX')."order as o
  43. where po.order_id= o.order_id and po.pin_id = ".$val['pin_id']." and o.order_status_id in(1,2,4,6,7,8,9,10) ";
  44. $count_arr = M()->query($sql);
  45. $pin_buy_count = $count_arr[0]['count'];
  46. $pin_jia_count = M('jiapinorder')->where( array('pin_id' => $val['pin_id']) )->count();
  47. if($val['state'] == 0 && $val['end_time'] <time()) {
  48. $val['state'] = 2;
  49. }
  50. $val['buy_count'] = $pin_buy_count + $pin_jia_count;
  51. $list[$key] = $val;
  52. }
  53. return array(
  54. 'empty'=>'<tr><td colspan="20">~~暂无数据</td></tr>',
  55. 'list'=>$list,
  56. 'page'=>$show
  57. );
  58. }
  59. /**
  60. 检测会员积分是否足够支付订单
  61. **/
  62. public function check_user_score_can_pay($member_id, $sku_str ='', $goods_id)
  63. {
  64. $member_info = M('member')->field('score')->where( array('member_id' => $member_id) )->find();
  65. if( !empty($sku_str) )
  66. {
  67. $mult_value_info = $goods_option_mult_value = M('goods_option_mult_value')->where( array('rela_goodsoption_valueid' => $sku_str,'goods_id'=>$goods_id) )->find();
  68. //pin_price
  69. if($mult_value_info['pin_price'] > $member_info['score'])
  70. {
  71. return array('code' => 1,'cur_score' => $member_info['score'],'pay_score' => $mult_value_info['pin_price']);
  72. }else{
  73. return array('code' => 0);
  74. }
  75. }else{
  76. $intgral_goods_info = M('intgral_goods')->field('score')->where( array('goods_id' => $goods_id) )->find();
  77. if($intgral_goods_info['score'] > $member_info['score'])
  78. {
  79. return array('code' => 1,'cur_score' => $member_info['score'],'pay_score' => $intgral_goods_info['score']);
  80. }else{
  81. return array('code' => 0);
  82. }
  83. }
  84. }
  85. /**
  86. 积分兑换记录
  87. **/
  88. public function show_exchange_integral_page($search)
  89. {
  90. //out' orderbuy order_id
  91. $sql = "select i.*,og.name,og.goods_images,og.quantity from ".C('DB_PREFIX')."integral_flow as i ,".C('DB_PREFIX')."order_goods as og
  92. where i.order_id=og.order_id and i.state = 1 and i.type ='orderbuy' ";
  93. $sql_count = "select count(i.id) as count from ".C('DB_PREFIX')."integral_flow as i ,".C('DB_PREFIX')."order_goods as og
  94. where i.order_id=og.order_id and i.state = 1 and i.type ='orderbuy' ";
  95. if(isset($search['goods_name'])){
  96. $sql.= " and og.name like '%".$search['goods_name']."%' ";
  97. $sql_count .= " and og.name like '%".$search['goods_name']."%' ";
  98. }
  99. //$count = count(M()->query($sql)); array(1) { [0]=> array(1) { ["count"]=> string(2) "21" } }
  100. $count_arr = M()->query($sql_count);
  101. $count = $count_arr[0]['count'];
  102. $Page = new \Think\Page($count,C('BACK_PAGE_NUM'));
  103. $show = $Page->show();// 分页显示输出
  104. $sql.=' ORDER BY i.id desc LIMIT '.$Page->firstRow.','.$Page->listRows;
  105. $list=M()->query($sql);
  106. foreach($list as $key => $val)
  107. {
  108. $member_info = M('member')->where( array('member_id' => $val['member_id']) )->find();
  109. $val['uname'] = $member_info['uname'];
  110. $val['avatar'] = $member_info['avatar'];
  111. $list[$key] = $val;
  112. }
  113. return array(
  114. 'empty'=>'<tr><td colspan="20">~~暂无数据</td></tr>',
  115. 'list'=>$list,
  116. 'page'=>$show
  117. );
  118. }
  119. public function show_integra_list_page($search)
  120. {
  121. $sql = "select * from ".C('DB_PREFIX')."integral_flow
  122. where 1=1 and state = 1 ";
  123. $sql_count = "select count(id) as count from ".C('DB_PREFIX')."integral_flow
  124. where 1=1 and state = 1 ";
  125. if(isset($search['member_id'])){
  126. $sql.= " and member_id= ".$search['member_id'];
  127. $sql_count .= " and member_id= ".$search['member_id'];
  128. }
  129. //$count = count(M()->query($sql)); array(1) { [0]=> array(1) { ["count"]=> string(2) "21" } }
  130. $count_arr = M()->query($sql_count);
  131. $count = $count_arr[0]['count'];
  132. $Page = new \Think\Page($count,C('BACK_PAGE_NUM'));
  133. $show = $Page->show();// 分页显示输出
  134. $sql.=' ORDER BY id desc LIMIT '.$Page->firstRow.','.$Page->listRows;
  135. $list=M()->query($sql);
  136. foreach($list as $key => $val)
  137. {
  138. //$val['buy_count'] = $pin_buy_count + $pin_jia_count;
  139. $list[$key] = $val;
  140. }
  141. return array(
  142. 'empty'=>'<tr><td colspan="20">~~暂无数据</td></tr>',
  143. 'list'=>$list,
  144. 'page'=>$show
  145. );
  146. }
  147. /**
  148. 系统奖励或者扣除会员积分
  149. CREATE TABLE IF NOT EXISTS `oscshop_integral_flow` (
  150. `id` int(10) NOT NULL COMMENT '自增id',
  151. `member_id` int(10) NOT NULL COMMENT '会员id',
  152. `in_out` enum('in','out') NOT NULL COMMENT '增加积分,还是减少积分',
  153. `type` enum('goodsbuy','refundorder','system_add','system_del') NOT NULL COMMENT '积分获赠/减少 类型',
  154. `order_id` int(10) DEFAULT NULL COMMENT '订单id',
  155. `addtime` int(10) NOT NULL COMMENT '添加时间'
  156. )
  157. 2 $User->where('id=5')->setInc('score',3); // 用户的积分加3
  158. 3 $User->where('id=5')->setInc('score'); // 用户的积分加1
  159. 4 $User->where('id=5')->setDec('score',5); // 用户的积分减5
  160. **/
  161. private function _do_charge_score($member_id, $score,$type=0)
  162. {
  163. if($type ==0 )
  164. {
  165. //增加
  166. M('member')->where( array('member_id' => $member_id) )->setInc('score',$score);
  167. }else if($type == 1){
  168. //减少
  169. M('member')->where( array('member_id' => $member_id) )->setDec('score',$score);
  170. }
  171. }
  172. /**
  173. 检测当前订单是否送过积分,退回积分
  174. **/
  175. public function check_refund_order_score($order_id)
  176. {
  177. //type orderbuy
  178. $flow_info = M('integral_flow')->where( array('order_id' => $order_id, 'state' => 1,'type' => 'orderbuy') )->find();
  179. $refund_flow_info = M('integral_flow')->where( array('order_id' => $order_id, 'state' => 1,'type' => 'refundorder') )->find();
  180. if( !empty($flow_info) && empty($refund_flow_info))
  181. {
  182. $this->charge_member_score( $flow_info['member_id'], $flow_info['score'],'out', 'refundorder', $order_id );
  183. }
  184. }
  185. //6
  186. /**
  187. 赠送订单积分
  188. **/
  189. public function send_order_score_dr($order_id)
  190. {
  191. $integral_flow_info = M('integral_flow')->where( array('order_id' => $order_id, 'state' => 0,'type' =>'goodsbuy') )->find();
  192. if( !empty($integral_flow_info) )
  193. {
  194. $this->_do_charge_score($integral_flow_info['member_id'], $integral_flow_info['score'],0);
  195. M('integral_flow')->where( array('id' => $integral_flow_info['id']) )->save( array('state' => 1) );
  196. }
  197. }
  198. public function show_integral_page($search)
  199. {
  200. $sql='SELECT p.goods_id,p.name,p.seller_count,p.quantity,p.type as goods_type,p.status,pg.id,p.price,p.image,pg.score FROM '
  201. .C('DB_PREFIX').'intgral_goods as pg left join '.C('DB_PREFIX').'goods as p on pg.goods_id=p.goods_id where 1=1 ';
  202. if(isset($search['customer_id'])){
  203. $sql.=" and p.store_id = ".$search['customer_id'];
  204. }
  205. //name
  206. if(isset($search['name'])){
  207. $sql.=" and p.name like '%".$search['name']."%'";
  208. }
  209. //'customer_id' => UID
  210. $count=count(M()->query($sql));
  211. $Page = new \Think\Page($count,C('BACK_PAGE_NUM'));
  212. $show = $Page->show();// 分页显示输出
  213. $sql.=' order by pg.id desc LIMIT '.$Page->firstRow.','.$Page->listRows;
  214. $list=M()->query($sql);
  215. foreach ($list as $key => $value) {
  216. $list[$key]['image']=resize($value['image'], 50, 50);
  217. }
  218. return array(
  219. 'empty'=>'<tr><td colspan="20">~~暂无数据</td></tr>',
  220. 'list'=>$list,
  221. 'page'=>$show
  222. );
  223. }
  224. public function charge_member_score( $member_id, $score,$in_out, $type, $order_id=0)
  225. {
  226. $log_data = array();
  227. $log_data['member_id'] = $member_id;
  228. $log_data['in_out'] = $in_out;
  229. $log_data['score'] = $score;
  230. $log_data['type'] = $type;
  231. $log_data['order_id'] = $order_id;
  232. $log_data['addtime'] = time();
  233. $member_score_info = M('member')->field('score')->where( array('member_id' => $member_id) )->find();
  234. $last_score = ' 原积分:'.$member_score_info['score'];
  235. if($in_out == 'in')
  236. {
  237. if($type == 'goodsbuy')
  238. {
  239. //增加积分
  240. $log_data['state'] = 0;
  241. $log_data['remark'] = "商品购买,增加积分";
  242. }else if($type == 'system_add'){
  243. //系统奖励
  244. //增加积分
  245. $log_data['state'] = 1;
  246. $log_data['remark'] = "系统奖励,增加积分".$last_score;
  247. $this->_do_charge_score($member_id, $score,0);
  248. }else if($type == 'invitegift'){
  249. //邀请者赠送积分
  250. $log_data['state'] = 1;
  251. $log_data['remark'] = "邀请者邀请成功,增加积分".$score;
  252. $this->_do_charge_score($member_id, $score,0);
  253. }else if($type == 'invitegift_new'){
  254. //被邀请者赠送积分
  255. $log_data['state'] = 1;
  256. $log_data['remark'] = "被邀请者邀请成功,增加积分".$score;
  257. $this->_do_charge_score($member_id, $score,0);
  258. }else if($type == 'pintuan_rebate'){
  259. //拼团返利
  260. $log_data['state'] = 1;
  261. $log_data['remark'] = "拼团返利,赠送积分".$score;
  262. $this->_do_charge_score($member_id, $score,0);
  263. }
  264. }else if($in_out == 'out'){
  265. if( $type =='refundorder' )
  266. {
  267. $log_data['state'] = 1;
  268. $log_data['remark'] = "订单退款,扣除积分".$last_score;
  269. $this->_do_charge_score($member_id, $score,1);
  270. }else if($type == 'system_del')
  271. {
  272. $log_data['state'] = 1;
  273. $log_data['remark'] = "系统扣除积分".$last_score;
  274. $this->_do_charge_score($member_id, $score,1);
  275. }else if($type == 'orderbuy')
  276. {
  277. $log_data['state'] = 1;
  278. $log_data['remark'] = "支付订单".$last_score;
  279. $this->_do_charge_score($member_id, $score,1);
  280. ////integral_order
  281. $integral_order_data = array();
  282. $integral_order_data['order_id'] = $order_id;
  283. $integral_order_data['score'] = $score;
  284. $integral_order_data['addtime'] = time();
  285. M('integral_order')->add($integral_order_data);
  286. }
  287. }
  288. M('integral_flow')->add($log_data);
  289. return true;
  290. }
  291. }
  292. ?>