IntegralModel.class.php 11 KB

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