GoodsModel.class.php 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919
  1. <?php
  2. namespace Home\Model;
  3. use Think\Model;
  4. /**
  5. * 商品模块
  6. * @author fish
  7. *
  8. */
  9. class GoodsModel extends Model{
  10. public function check_goods_fav($goods_id, $member_id)
  11. {
  12. $user_favgoods = M('user_favgoods')->where( array('member_id' => $member_id, 'goods_id' => $goods_id) )->find();
  13. if(!empty($user_favgoods))
  14. {
  15. return true;
  16. } else {
  17. return false;
  18. }
  19. }
  20. public function check_goods_new_manbug($member_id )
  21. {
  22. //order_status_id in (1,2,4,6,7,8,9,10,11,12,13)
  23. $sql ="select count(o.id) as count from ".C('DB_PREFIX')."order as o ";
  24. $order_count = M('order')->where( array('member_id' => $member_id, 'order_status_id' =>array('in', '1,2,4,6,7,8,9,10,11,12,13')) )->count();
  25. return $order_count;
  26. }
  27. public function check_store_fav($store_id, $member_id)
  28. {
  29. $user_favstore = M('user_favstore')->where( array('member_id' => $member_id, 'store_id' => $store_id) )->find();
  30. if(!empty($user_favstore))
  31. {
  32. return true;
  33. } else {
  34. return false;
  35. }
  36. }
  37. /**
  38. 获取商品列表
  39. **/
  40. public function get_goods_list($fields='*', $where='1=1',$order='index_sort desc ,seller_count desc,goods_id asc',$offset=0,$perpage=10)
  41. {
  42. $list = M('goods')->where($where)->order($order)->limit($offset,$perpage)->select();
  43. // var_dump(M('goods')->getLastSql(), $list);
  44. //die();
  45. return $list;
  46. }
  47. /**
  48. 获取会员等级信息,某个商品的折扣
  49. **/
  50. public function get_member_level_info($member_id, $goods_id = 0)
  51. {
  52. $result = array();
  53. $member_info = M('member')->field('level_id')->where( array('member_id' => $member_id) )->find();
  54. $result['level_id'] = $member_info['level_id'];
  55. if( empty($member_info['level_id']) || $member_info['level_id'] ==0)
  56. {
  57. $default_discount_info = M('config')->where( array('name' => 'member_defualt_discount') )->find();
  58. $default_levelname_info = M('config')->where( array('name' => 'member_default_levelname') )->find();
  59. $result['member_discount'] = $default_discount_info['value'];
  60. $result['level_name'] = $default_levelname_info['value'];
  61. $result['level'] = 0;
  62. }else{
  63. $level_info = M('member_level')->where( array('id' => $member_info['level_id']) )->find();
  64. $result['member_discount'] = $level_info['discount'];
  65. $result['level_name'] = $level_info['levelname'];
  66. $result['level'] = $level_info['level'];
  67. }
  68. if(!empty($goods_id) && $goods_id > 0)
  69. {
  70. $desc_info = M('goods_description')->field('is_untake_level,level_discount')->where( array('goods_id' => $goods_id) )->find();
  71. if(!empty($desc_info))
  72. {
  73. if($desc_info['is_untake_level'] == 1)
  74. {
  75. $result['member_discount'] = 100;
  76. }else{
  77. $level_discount_arr = unserialize($desc_info['level_discount']);
  78. if( array_key_exists($member_info['level_id'],$level_discount_arr) && $level_discount_arr[$member_info['level_id']] > 0)
  79. {
  80. $result['member_discount'] = $level_discount_arr[$member_info['level_id']];
  81. }
  82. }
  83. }
  84. }
  85. $level_info = M('config')->where( array('name' => 'member_level_is_open') )->find();
  86. if($level_info['value'] == 0)
  87. {
  88. $result['member_discount'] = 100;
  89. }
  90. return $result;
  91. }
  92. /**
  93. * 获取商品的不同价格,分普通商品,拼团商品
  94. * @param unknown $goods_id
  95. */
  96. public function get_goods_price($goods_id)
  97. {
  98. $price_arr = array();
  99. //if (!$price_arr = S('goods_price_cache'.$goods_id)) {
  100. //$this->customer_id
  101. $goods_info = M('goods')->field('type,danprice')->where( array('goods_id' =>$goods_id) )->find();
  102. if($goods_info['type'] =='pintuan' || $goods_info['type'] =='newman' || $goods_info['type'] =='lottery')
  103. {
  104. $pin_goods_info = M('pin_goods')->field('pin_price,pin_count')->where( array('goods_id' =>$goods_id) )->find();
  105. if(!empty($pin_goods_info))
  106. {
  107. $price_arr = array('price' =>$pin_goods_info['pin_price'],'danprice' =>$goods_info['danprice'], 'pin_price' =>$pin_goods_info['pin_price'],'pin_count' => $pin_goods_info['pin_count']);
  108. //pin_price
  109. $option_price_arr = M('goods_option_mult_value')->field('dan_price')->where( array('goods_id' => $goods_id) )->order('dan_price asc')->find();
  110. if( !empty($option_price_arr) )
  111. {
  112. //if($option_price_arr['dan_price'] < $price_arr['danprice'])
  113. //{
  114. $price_arr['danprice'] = $option_price_arr['dan_price'];
  115. //}
  116. }
  117. $option_pinprice_arr = M('goods_option_mult_value')->field('pin_price')->where( array('goods_id' => $goods_id) )->order('pin_price asc')->find();
  118. if( !empty($option_pinprice_arr) )
  119. {
  120. //if($option_pinprice_arr['pin_price'] < $pin_goods_info['pin_price'])
  121. //{
  122. $price_arr['price'] = $option_pinprice_arr['pin_price'];
  123. $price_arr['pin_price'] = $option_pinprice_arr['pin_price'];
  124. //}
  125. //
  126. }
  127. //S('goods_price_cache'.$goods_id, $price_arr);
  128. }
  129. }
  130. else if($goods_info['type'] =='integral')
  131. {
  132. $pin_goods_info = M('intgral_goods')->field('score')->where( array('goods_id' =>$goods_id) )->find();
  133. if(!empty($pin_goods_info))
  134. {
  135. $price_arr = array('price' =>$pin_goods_info['score'],'danprice' =>$pin_goods_info['score'], 'pin_price' =>$pin_goods_info['score']);
  136. //pin_price
  137. $option_price_arr = M('goods_option_mult_value')->field('dan_price')->where( array('goods_id' => $goods_id) )->order('dan_price asc')->find();
  138. if( !empty($option_price_arr) )
  139. {
  140. $price_arr['danprice'] = $option_price_arr['dan_price'];
  141. }
  142. $option_pinprice_arr = M('goods_option_mult_value')->field('pin_price')->where( array('goods_id' => $goods_id) )->order('pin_price asc')->find();
  143. if( !empty($option_pinprice_arr) )
  144. {
  145. $price_arr['price'] = $option_pinprice_arr['pin_price'];
  146. $price_arr['pin_price'] = $option_pinprice_arr['pin_price'];
  147. }
  148. }
  149. }
  150. else if($goods_info['type'] =='bargain')
  151. {
  152. $pin_goods_info = M('bargain_goods')->field('bargain_price,bargain_count')->where( array('goods_id' =>$goods_id) )->find();
  153. if(!empty($pin_goods_info))
  154. {
  155. $price_arr = array('price' =>$pin_goods_info['bargain_price'],'danprice' =>$goods_info['danprice'], 'pin_price' =>$pin_goods_info['bargain_price'],'pin_count' => $pin_goods_info['bargain_count']);
  156. //pin_price
  157. $option_price_arr = M('goods_option_mult_value')->field('dan_price')->where( array('goods_id' => $goods_id) )->order('dan_price asc')->find();
  158. if( !empty($option_price_arr) )
  159. {
  160. $price_arr['danprice'] = $option_price_arr['dan_price'];
  161. }
  162. $option_pinprice_arr = M('goods_option_mult_value')->field('pin_price')->where( array('goods_id' => $goods_id) )->order('pin_price asc')->find();
  163. if( !empty($option_pinprice_arr) )
  164. {
  165. $price_arr['price'] = $option_pinprice_arr['pin_price'];
  166. $price_arr['pin_price'] = $option_pinprice_arr['pin_price'];
  167. }
  168. }
  169. }
  170. else{
  171. //获取最低价格
  172. //dan_price
  173. $option_price_arr = M('goods_option_mult_value')->field('dan_price')->where( array('goods_id' => $goods_id) )->order('dan_price asc')->find();
  174. if( !empty($option_price_arr) && $option_price_arr['dan_price'] >= 0.01)
  175. {
  176. $price_arr = array('price' => $option_price_arr['dan_price'],'danprice' => $option_price_arr['dan_price']);
  177. //if($price_arr['price'] > $goods_info['dan_price'])
  178. //{
  179. // $price_arr = array('price' => $goods_info['dan_price'],'danprice' => $goods_info['dan_price']);
  180. //}
  181. }else{
  182. //danprice
  183. $goods_info = M('goods')->field('danprice')->where( array('goods_id' => $goods_id) )->find();
  184. $price_arr = array('price' => $goods_info['danprice'],'danprice' => $goods_info['danprice']);
  185. }
  186. }
  187. //未来还有更多类型
  188. // }
  189. return $price_arr;
  190. }
  191. /**
  192. 砍价
  193. **/
  194. public function _zan_bargain_order($zan_order_id)
  195. {
  196. $zan_order_info = M('bargain_order')->where( array('id' => $zan_order_id) )->find();
  197. //goods_id
  198. $goods_info = M('goods')->field('name,store_id,model,image')->where( array('goods_id' => $zan_order_info['goods_id']) )->find();
  199. $member_info = M('member')->where( array('member_id' => $zan_order_info['member_id']) )->find();
  200. $address_info = M('address')->where( array('address_id' => $zan_order_info['address_id']) )->find();
  201. $order = array();
  202. $order['member_id']=$zan_order_info['member_id'];
  203. $order['order_num_alias']= build_order_no($zan_order_info['member_id']);
  204. $order['name']=$goods_info['name'];
  205. $order['email']=$member_info['email'];
  206. $order['store_id']= $goods_info['store_id'];
  207. $order['telephone']=$address_info['telephone'];
  208. $order['shipping_name']=$address_info['name'];
  209. $order['shipping_address']=$address_info['address'];
  210. $order['shipping_city_id']=$address_info['city_id'];
  211. $order['shipping_country_id']=$address_info['country_id'];
  212. $order['shipping_province_id']=$address_info['province_id'];
  213. $order['shipping_tel']=$address_info['telephone'];
  214. $order['comment']='';
  215. $order['order_status_id']=1;
  216. $order['ip']=get_client_ip();
  217. $order['voucher_id']=0;
  218. $order['voucher_credit']=0;
  219. $order['shipping_fare'] = 0;
  220. $order['is_zhuli'] = 2;
  221. $order['is_pin'] = 0;
  222. $order['ip_region'] = '';
  223. $order['date_added'] =time();
  224. $order['total'] =0;
  225. $order['user_agent']='';
  226. $order['shipping_method']=0;
  227. $order['delivery']=$data['delivery'];
  228. $order['payment_code']='express';
  229. $order['type']='normal';
  230. $order['address_id']=$zan_order_info['address_id'];
  231. $order_id=M('Order')->add($order);
  232. //goods_images
  233. $this->execute("INSERT INTO ".C('DB_PREFIX')."order_goods SET order_id = '" .$order_id
  234. ."',goods_id='".$zan_order_info['goods_id']."'"
  235. .",store_id='".$goods_info['store_id']."'"
  236. .",name='". addslashes($goods_info['name'])."'"
  237. .",model='".$goods_info['model']."'"
  238. .",commiss_one_money='0'"
  239. .",commiss_two_money='0'"
  240. .",commiss_three_money='0'"
  241. .",head_disc='100'"
  242. .",is_pin='0'"
  243. .",goods_images='".$goods_info['image']."'"
  244. .",goods_type='normal'"
  245. .",shipping_fare='0'"
  246. .",quantity='1'"
  247. .",price='0'"
  248. .",rela_goodsoption_valueid='".$zan_order_info['sku_str']."'"
  249. .",comment=''"
  250. .",total='0'"
  251. );
  252. $order_goods_id=$this->getLastInsID();
  253. $sku_str = ($zan_order_info['sku_str']);
  254. $options = $sku_str;
  255. $option_data = array();
  256. $goods_id = $zan_order_info['goods_id'];
  257. //$good['sku_str']
  258. if(!empty($sku_str))
  259. {
  260. $options_arr = array();
  261. $option_value_id_arr = explode('_',$sku_str);
  262. foreach($option_value_id_arr as $id_val)
  263. {
  264. $goods_option_value = M('goods_option_value')->where( array('option_value_id' => $id_val,'goods_id' =>$goods_id) )->find();
  265. $options_arr[$goods_option_value['goods_option_id']] = $goods_option_value['goods_option_value_id'];
  266. $goods_option = M('goods_option')->where( array('goods_option_id' =>$goods_option_value['goods_option_id']) )->find();
  267. $option_value = M('option_value')->where( array('option_value_id' =>$goods_option_value['option_value_id']) )->find();
  268. $this->execute("INSERT INTO ".C('DB_PREFIX')."order_option SET order_id = '" .$order_id
  269. ."',order_goods_id='".$order_goods_id."'"
  270. .",goods_option_id='".(int)$goods_option_value['goods_option_id']."'"
  271. .",goods_option_value_id='".(int)$goods_option_value['goods_option_value_id']."'"
  272. .",name='".$goods_option['option_name']."'"
  273. .",value='".$option_value['value_name']."'"
  274. );
  275. }
  276. }
  277. $oh['order_id']=$order_id;
  278. $oh['order_status_id']=1;
  279. $oh['comment']='砍价成功';
  280. $oh['date_added']=time();
  281. $oh_id=M('OrderHistory')->add($oh);
  282. return $order_id;
  283. }
  284. /**
  285. * 获取商品规格
  286. * @param unknown $goods_id
  287. */
  288. public function get_goods_options($goods_id) {
  289. $result = array();
  290. $goods_option_name = array();
  291. $goods_option_data = array();
  292. $goods_option_query = M()->query("SELECT * FROM " . C('DB_PREFIX') . "goods_option po LEFT JOIN "
  293. . C('DB_PREFIX') . "option o ON po.option_id = o.option_id WHERE po.goods_id =".(int)$goods_id);
  294. foreach ($goods_option_query as $goods_option) {
  295. $goods_option_value_data = array();
  296. $goods_option_value_query = M()->query("SELECT pov.*,ov.value_name FROM " . C('DB_PREFIX')
  297. . "goods_option_value pov LEFT JOIN ". C('DB_PREFIX')
  298. ."option_value ov ON pov.option_value_id=ov.option_value_id"
  299. ." WHERE pov.goods_option_id = '"
  300. . (int)$goods_option['goods_option_id'] . "'");
  301. foreach ($goods_option_value_query as $goods_option_value) {
  302. $goods_option_value_data[] = array(
  303. 'goods_option_value_id' => $goods_option_value['goods_option_value_id'],
  304. 'option_value_id' => $goods_option_value['option_value_id'],
  305. 'quantity' => $goods_option_value['quantity'],
  306. 'name' =>$goods_option_value['value_name'],
  307. 'image' =>isset($goods_option_value['image'])?$goods_option_value['image']:'',
  308. 'price' =>'¥'.$goods_option_value['price'],
  309. 'price_prefix' => $goods_option_value['price_prefix'],
  310. );
  311. }
  312. $goods_option_name[] = $goods_option['name'];
  313. $goods_option_data[] = array(
  314. 'goods_option_id' => $goods_option['goods_option_id'],
  315. 'option_id' => $goods_option['option_id'],
  316. 'name' => $goods_option['name'],
  317. 'type' => $goods_option['type'],
  318. 'option_value' => $goods_option_value_data,
  319. 'required' => $goods_option['required']
  320. );
  321. }
  322. $result['list'] = $goods_option_data;
  323. $result['name'] = $goods_option_name;
  324. return $result;
  325. }
  326. //user_fav_store_toggle
  327. public function user_fav_store_toggle($store_id, $member_id)
  328. {
  329. $res = $this->check_store_fav($store_id, $member_id);
  330. if($res)
  331. {
  332. //删除
  333. $rs = M('user_favstore')->where( array('member_id' => $member_id, 'store_id' => $store_id) )->delete();
  334. return 1;
  335. } else {
  336. //添加
  337. $data = array();
  338. $data['member_id'] = $member_id;
  339. $data['store_id'] = $store_id;
  340. $data['add_time'] = time();
  341. M('user_favstore')->add($data);
  342. return 2;
  343. }
  344. }
  345. /**
  346. 关注取消商品收藏
  347. 删除返回1
  348. **/
  349. public function user_fav_goods_toggle($goods_id, $member_id)
  350. {
  351. $res = $this->check_goods_fav($goods_id, $member_id);
  352. if($res)
  353. {
  354. //删除
  355. $rs = M('user_favgoods')->where( array('member_id' => $member_id, 'goods_id' => $goods_id) )->delete();
  356. return 1;
  357. } else {
  358. //添加
  359. $data = array();
  360. $data['member_id'] = $member_id;
  361. $data['goods_id'] = $goods_id;
  362. $data['add_time'] = time();
  363. M('user_favgoods')->add($data);
  364. return 2;
  365. }
  366. }
  367. public function get_goods_pin_avatar($goods_id,$limit =10)
  368. {
  369. $sql = "select distinct(m.member_id), m.avatar from ".C('DB_PREFIX')."order_goods as og ,".C('DB_PREFIX')."order as o,".C('DB_PREFIX')."member as m
  370. where og.order_id=o.order_id and o.member_id=m.member_id and og.pin_id>0 and og.goods_id={$goods_id} order by og.pin_id desc limit {$limit}";
  371. $avatar_list = M()->query($sql);
  372. return $avatar_list;
  373. }
  374. /**
  375. 返回用户还能够买多少份该商品
  376. **/
  377. public function check_goods_user_canbuy_count($user_id, $goods_id)
  378. {
  379. $goods_desc = M('goods_description')->where( array('goods_id' => $goods_id) )->find();//per_number
  380. if($goods_desc['per_number'] > 0)
  381. {
  382. $query = M()->query("SELECT sum(og.quantity) as count FROM " . C('DB_PREFIX') . "order as o,
  383. " . C('DB_PREFIX') . "order_goods as og where o.order_id = og.order_id and og.goods_id =" . (int)$goods_id ."
  384. and o.member_id = {$user_id} and o.order_status_id in (1,2,3,4,6,7,9,11,12,13)");
  385. $buy_count = $query[0]['count'];
  386. if($buy_count >=$goods_desc['per_number'])
  387. {
  388. return -1;
  389. } else {
  390. return ($goods_desc['per_number'] - $buy_count);
  391. }
  392. } else{
  393. return 0;
  394. }
  395. }
  396. /**
  397. 扣除/增加商品多规格库存
  398. 1扣除, 2 增加
  399. **/
  400. public function del_goods_mult_option_quantity($order_id,$option,$goods_id,$quantity,$type='1')
  401. {
  402. //todo 库存减的不对。
  403. //$goods['option'],$goods_id[0],$goods['quantity'],1
  404. //`order_id` int(11) NOT NULL,
  405. // `order_goods_id` int(11) NOT NULL, quantity
  406. $tp_goods = M('goods')->field('quantity')->where( array('goods_id' => $goods_id) )->find();
  407. $order_goods = M('order_goods')->where( array('order_id' => $order_id,'goods_id' => $goods_id) )->find();
  408. $option_list = M('order_option')->where( array('order_id' => $order_id, 'order_goods_id' =>$goods_id ) )->order('order_option_id asc')->select();
  409. //$option_list = M('goods_option_value')->where( array('goods_id' => $goods_id) )->select();
  410. if($type == 1)
  411. {
  412. // rela_goodsoption_value_id order_id goods_id quantity addtime type last_quantity
  413. $quantity_order_data = array();
  414. $quantity_order_data['order_id'] = $order_id;
  415. $quantity_order_data['goods_id'] = $goods_id;
  416. $quantity_order_data['rela_goodsoption_value_id'] = $option;
  417. $quantity_order_data['quantity'] = $quantity;
  418. $quantity_order_data['type'] = 0;
  419. $quantity_order_data['last_quantity'] = $tp_goods['quantity'];
  420. $quantity_order_data['addtime'] = time();
  421. M('order_quantity_order')->add($quantity_order_data);
  422. //扣除库存
  423. $this->execute("UPDATE " . C('DB_PREFIX') . "goods SET quantity = (quantity - " . (int)$quantity . ") WHERE goods_id = '" . $goods_id . "' ");
  424. //销量增加
  425. $this->execute("UPDATE " . C('DB_PREFIX') . "goods SET seller_count = (seller_count + " . (int)$quantity . ") WHERE goods_id = '" . $goods_id . "' ");
  426. } else if($type == 2){
  427. $quantity_order_data = array();
  428. $quantity_order_data['order_id'] = $order_id;
  429. $quantity_order_data['goods_id'] = $goods_id;
  430. $quantity_order_data['rela_goodsoption_value_id'] = $option;
  431. $quantity_order_data['quantity'] = $quantity;
  432. $quantity_order_data['type'] = 1;
  433. $quantity_order_data['last_quantity'] = $tp_goods['quantity'];
  434. $quantity_order_data['addtime'] = time();
  435. M('order_quantity_order')->add($quantity_order_data);
  436. //增加库存
  437. $this->execute("UPDATE " . C('DB_PREFIX') . "goods SET quantity = (quantity + " . (int)$quantity . ") WHERE goods_id = '" . $goods_id . "' ");
  438. //销量减少
  439. $this->execute("UPDATE " . C('DB_PREFIX') . "goods SET seller_count = (seller_count - " . (int)$quantity . ") WHERE goods_id = '" . $goods_id . "' ");
  440. }
  441. foreach($option_list as $op_li)
  442. {
  443. if($type == 1)
  444. {
  445. $goods_option_value = M('goods_option_value')->where( array('goods_option_id'=>$op_li['goods_option_id'],'goods_option_value_id' => $op_li['goods_option_value_id']) )->setDec('quantity',$quantity);
  446. } else if($type ==2){
  447. $goods_option_value = M('goods_option_value')->where( array('goods_option_id'=>$op_li['goods_option_id'],'goods_option_value_id' => $op_li['goods_option_value_id']) )->setInc('quantity',$quantity);
  448. }
  449. }
  450. if(!empty($option))
  451. {
  452. if($type == 1)
  453. {
  454. M('goods_option_mult_value')->where( array('rela_goodsoption_valueid' => $option,'goods_id' =>$goods_id) )->setDec('quantity',$quantity);
  455. } else if($type ==2){
  456. M('goods_option_mult_value')->where( array('rela_goodsoption_valueid' => $option,'goods_id' =>$goods_id) )->setInc('quantity',$quantity);
  457. }
  458. }
  459. }
  460. /**
  461. 扣除/增加商品多规格库存
  462. 1扣除, 2 增加
  463. **/
  464. public function del_goods_mult_option_quantity2($order_id,$quantity,$type='1')
  465. {
  466. //todo 库存减的不对。
  467. $order_goods = M('order_goods')->where( array('order_id' => $order_id) )->find();
  468. $goods_id = $order_goods['goods_id'];
  469. $option_list = M('order_option')->where( array('order_id' => $order_id) )->order('order_option_id asc')->select();
  470. //$option_list = M('goods_option_value')->where( array('goods_id' => $goods_id) )->select();
  471. if($type == 1)
  472. {
  473. //扣除库存
  474. $this->execute("UPDATE " . C('DB_PREFIX') . "goods SET quantity = (quantity - " . (int)$quantity . ") WHERE goods_id = '" . $goods_id . "' ");
  475. //销量增加
  476. $this->execute("UPDATE " . C('DB_PREFIX') . "goods SET seller_count = (seller_count + " . (int)$quantity . ") WHERE goods_id = '" . $goods_id . "' ");
  477. } else if($type == 2){
  478. //增加库存
  479. $this->execute("UPDATE " . C('DB_PREFIX') . "goods SET quantity = (quantity + " . (int)$quantity . ") WHERE goods_id = '" . $goods_id . "' ");
  480. //销量减少
  481. $this->execute("UPDATE " . C('DB_PREFIX') . "goods SET seller_count = (seller_count - " . (int)$quantity . ") WHERE goods_id = '" . $goods_id . "' ");
  482. }
  483. $option = array();
  484. foreach($option_list as $op_li)
  485. {
  486. $option[$op_li['goods_option_id']] = $op_li['goods_option_value_id'];
  487. }
  488. $mul_opt_arr = array();
  489. foreach($option as $key => $option_value)
  490. {
  491. $goods_option_value = M('goods_option_value')->where( array('goods_option_id'=>$key,'goods_option_value_id' => $option_value) )->find();
  492. $mul_opt_arr[] = $goods_option_value['option_value_id'];
  493. }
  494. $rela_goodsoption_valueid = implode('_', $mul_opt_arr);
  495. $goods_option_mult_value = M('goods_option_mult_value')->where( array('rela_goodsoption_valueid' => $rela_goodsoption_valueid,'goods_id'=>$goods_id) )->find();
  496. if( !empty($goods_option_mult_value) )
  497. {
  498. if($type == 1)
  499. {
  500. $this->execute("UPDATE " . C('DB_PREFIX') . "goods_option_mult_value
  501. SET quantity = (quantity - " . (int)$quantity . ")
  502. WHERE rela_goodsoption_valueid = '" . $rela_goodsoption_valueid . "' and goods_id=".$goods_id);
  503. foreach($option as $key => $option_value)
  504. {
  505. $goods_option_value = M('goods_option_value')->where( array('goods_option_id'=>$key,'goods_option_value_id' => $option_value) )->setDec('quantity',$quantity);
  506. //$User->where('id=5')->setInc('score',3); + quantity
  507. //$User->where('id=5')->setDec('score',5); -
  508. }
  509. } else if($type ==2){
  510. $this->execute("UPDATE " . C('DB_PREFIX') . "goods_option_mult_value
  511. SET quantity = (quantity + " . (int)$quantity . ")
  512. WHERE rela_goodsoption_valueid = '" . $rela_goodsoption_valueid . "' and goods_id=".$goods_id);
  513. foreach($option as $key => $option_value)
  514. {
  515. $goods_option_value = M('goods_option_value')->where( array('goods_option_id'=>$key,'goods_option_value_id' => $option_value) )->setInc('quantity',$quantity);
  516. }
  517. }
  518. }
  519. }
  520. private function _curl_get_avatar($url)
  521. {
  522. $ch = curl_init();
  523. curl_setopt($ch, CURLOPT_URL, $url);
  524. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  525. curl_setopt($ch, CURLOPT_HEADER, 0);
  526. $output = curl_exec($ch);
  527. curl_close($ch);
  528. return $output;
  529. }
  530. /**
  531. 获取首页商品
  532. **/
  533. public function _get_index_user_wxqrcode($goods_id,$member_id)
  534. {
  535. //qrcode
  536. $jssdk = new \Lib\Weixin\Jssdk( C('weprogram_appid'), C('weprogram_appscret') );
  537. $weqrcode = $jssdk->getAllWeQrcode('pages/index/index','0_'.$member_id );
  538. //line_color
  539. //var_dump($weqrcode);die();
  540. //保存图片
  541. $image_dir = ROOT_PATH.'Uploads/image/goods';
  542. $image_dir .= '/'.date('Y-m-d').'/';
  543. $file_path = C('SITE_URL').'Uploads/image/goods/'.date('Y-m-d').'/';
  544. $kufile_path = $dir.'/'.date('Y-m-d').'/';
  545. RecursiveMkdir($image_dir);
  546. $file_name = md5('qrcode_'.$goods_id.'_'.$member_id.time()).'.png';
  547. //qrcode
  548. file_put_contents($image_dir.$file_name, $weqrcode);
  549. return $image_dir.$file_name;
  550. }
  551. public function _get_goods_user_wxqrcode($goods_id,$member_id)
  552. {
  553. //qrcode
  554. $jssdk = new \Lib\Weixin\Jssdk( C('weprogram_appid'), C('weprogram_appscret') );
  555. $weqrcode = $jssdk->getAllWeQrcode('pages/goods/index',$goods_id.'_'.$member_id );
  556. //line_color
  557. //var_dump($weqrcode);die();
  558. //保存图片
  559. $image_dir = ROOT_PATH.'Uploads/image/goods';
  560. $image_dir .= '/'.date('Y-m-d').'/';
  561. $file_path = C('SITE_URL').'Uploads/image/goods/'.date('Y-m-d').'/';
  562. $kufile_path = $dir.'/'.date('Y-m-d').'/';
  563. RecursiveMkdir($image_dir);
  564. $file_name = md5('qrcode_'.$goods_id.'_'.$member_id.time()).'.png';
  565. //qrcode
  566. file_put_contents($image_dir.$file_name, $weqrcode);
  567. return $image_dir.$file_name;
  568. }
  569. public function _get_compare_qrcode_bgimg($bg_img, $qrcode_img,$s_x = '500',$s_y = '660')
  570. {
  571. $image_dir = ROOT_PATH."Uploads/image/".date('Y-m-d')."/";
  572. RecursiveMkdir($image_dir);
  573. $thumb_image_name = "thumb_img".md5($qrcode_img).'.png';
  574. $image = new \Think\Image();
  575. $image->open($qrcode_img);
  576. $image->thumb(230, 230)->save($image_dir.$thumb_image_name);
  577. $thumb_qrcode_img = $image_dir.$thumb_image_name;
  578. $bg_img = ROOT_PATH."Uploads/image/".$bg_img;
  579. $dst = imagecreatefromjpeg ($bg_img);
  580. $src = imagecreatefromstring(file_get_contents($thumb_qrcode_img));
  581. if (imageistruecolor($src))
  582. imagetruecolortopalette($src, false, 65535);
  583. list($src_w, $src_h) = getimagesize($thumb_qrcode_img);
  584. list($dst_w, $dst_h, $dst_type) = getimagesize($bg_img);
  585. imagecopymerge($dst, $src, $s_x, $s_y, 0, 0, $src_w, $src_h, 100);
  586. $last_img = $image_dir;
  587. $file_path = C('SITE_URL').'Uploads/image/'.date('Y-m-d').'/';
  588. $return_file_path = ''.date('Y-m-d').'/';
  589. $last_img_name = "last_qrcode".md5( time().$bg_img.$qrcode_img).'';
  590. switch ($dst_type) {
  591. case 1://GIF
  592. $last_img_name .= '.gif';
  593. header('Content-Type: image/gif');
  594. imagegif($dst, $last_img.$last_img_name);
  595. break;
  596. case 2://JPG
  597. $last_img_name .= '.jpg';
  598. //header('Content-Type: image/jpeg');
  599. imagejpeg($dst, $last_img.$last_img_name);
  600. break;
  601. case 3://PNG
  602. $last_img_name .= '.png';
  603. header('Content-Type: image/png');
  604. imagepng($dst, $last_img.$last_img_name);
  605. break;
  606. default:
  607. break;
  608. }
  609. imagedestroy($dst);
  610. imagedestroy($src);
  611. imagedestroy($goods_src);
  612. //imagedestroy($avatar_src);
  613. //return_file_path
  614. $result = array('full_path' => $file_path.$last_img_name,'need_path' => $return_file_path.$last_img_name);
  615. return $result;
  616. }
  617. //_get_compare_zan_img($rocede_path,$goods_img,$goods_title,$goods_price)
  618. public function _get_compare_zan_img($goods_img,$goods_title,$goods_price)
  619. {
  620. //$qrcode_img = ROOT_PATH."Uploads/image/2018-01-19/7b4f87260dff5247f2313dfe6cc0fe83.png";
  621. $image_dir = ROOT_PATH."Uploads/image/".date('Y-m-d')."/";
  622. RecursiveMkdir($image_dir);
  623. // 70 70 /alidata/www/pinduoduo/Common/img/bg.jpg
  624. // Common/img/bg.png 590 460 11258
  625. $bg_img = ROOT_PATH."Common/image/bg.jpg";
  626. //$goods_img = ROOT_PATH."Uploads/image/goods/2017-11-25/1511619168701f098ef1190364f8beb079a306673c.jpg";
  627. $thumb_goods_name = "thumb_img".md5($goods_img).'.png';
  628. $image2 = new \Think\Image();
  629. $image2->open($goods_img);
  630. $image2->thumb(750, 585)->save($image_dir.$thumb_goods_name);
  631. $thumb_goods_img = $image_dir.$thumb_goods_name;
  632. $image_dir = ROOT_PATH.'Uploads/image/goods';
  633. $image_dir .= '/'.date('Y-m-d').'/';
  634. $file_path = C('SITE_URL').'Uploads/image/goods/'.date('Y-m-d').'/';
  635. $return_file_path = 'goods/'.date('Y-m-d').'/';
  636. RecursiveMkdir($image_dir);
  637. //$image_dir.$thumb_avatar_name
  638. //文字:74 640
  639. //长按二维码领取: 517 640
  640. //商品文字: 24 710
  641. //快和我一起领取吧: 24 817
  642. //市场价,单价 24 895
  643. //var_dump($thumb_goods_img);die();
  644. //$dst = imagecreatefromstring(file_get_contents($bg_img));
  645. $dst = imagecreatefromjpeg ($bg_img);
  646. $goods_src = imagecreatefromstring(file_get_contents($thumb_goods_img));
  647. if (imageistruecolor($goods_src))
  648. imagetruecolortopalette($goods_src, false, 65535);
  649. if (imageistruecolor($avatar_src))
  650. imagetruecolortopalette($avatar_src, false, 65535);
  651. list($goods_src_w, $goods_src_h) = getimagesize($thumb_goods_img);
  652. list($dst_w, $dst_h, $dst_type) = getimagesize($bg_img);
  653. imagecopymerge($dst, $goods_src, ($dst_w - $goods_src_w) / 2, 0, 0, 0, $goods_src_w, $goods_src_h, 100);
  654. //imagecopymerge($dst, $avatar_src, 24, 615, 0, 0, $avatar_w, $avatar_h, 100);
  655. $ttf_path = ROOT_PATH."Common/js/simhei.ttf";
  656. //打上文字
  657. $black = imagecolorallocate($dst, 20,20,20);//黑色
  658. $red = imagecolorallocate($dst, 237, 48, 43); //红色 201 55 49
  659. $huise = imagecolorallocate($dst, 159, 159, 159); //灰色 159 159 159
  660. $fense = imagecolorallocate($dst, 248, 136, 161); //粉色 248 136 161
  661. //ffb7d7 248 136 161
  662. //$goods_title = "我免费领取了【大白兔奶糖果零食铁盒装114g】的所得税的色舞认太热太热太热";
  663. $goods_title = $goods_title;
  664. $need_goods_title = mb_substr($goods_title,0,12,'utf-8')."\r\n";
  665. $need_goods_title .= mb_substr($goods_title,12,12,'utf-8');
  666. imagefttext($dst, 25, 0, 120, 660, $black, $ttf_path, $username);
  667. //imagefttext($dst, 15, 0, 518, 920, $huise, $ttf_path, '长按二维码领取');
  668. imagefttext($dst, 30, 0, 24, 750, $black, $ttf_path, $need_goods_title);
  669. imagefttext($dst, 15, 0, 24, 860, $fense, $ttf_path, "限时爆款价");
  670. imagefttext($dst, 36, 0, 24, 920, $black, $ttf_path, "¥".$goods_price['price']);
  671. imagefttext($dst, 18, 0, 186, 920, $huise, $ttf_path, "市场价¥".$goods_price['market_price']);
  672. $last_img = $image_dir;
  673. $last_img_name = "last_avatar".md5( time().$need_goods_title.$username).'';
  674. switch ($dst_type) {
  675. case 1://GIF
  676. $last_img_name .= '.gif';
  677. header('Content-Type: image/gif');
  678. imagegif($dst, $last_img.$last_img_name);
  679. break;
  680. case 2://JPG
  681. $last_img_name .= '.jpg';
  682. //header('Content-Type: image/jpeg');
  683. imagejpeg($dst, $last_img.$last_img_name);
  684. break;
  685. case 3://PNG
  686. $last_img_name .= '.png';
  687. header('Content-Type: image/png');
  688. imagepng($dst, $last_img.$last_img_name);
  689. break;
  690. default:
  691. break;
  692. }
  693. imagedestroy($dst);
  694. imagedestroy($goods_src);
  695. //imagedestroy($avatar_src);
  696. //return_file_path
  697. $result = array('full_path' => $file_path.$last_img_name,'need_path' => $return_file_path.$last_img_name);
  698. return $result;
  699. }
  700. /**
  701. * 前端已售单位自定义
  702. * @return [string] [自定义值,默认件]
  703. */
  704. public function get_sale_unit()
  705. {
  706. $isnull_goods_sale_unit = D('Home/Front')->get_config_by_name('isnull_goods_sale_unit');
  707. if($isnull_goods_sale_unit==1) {
  708. return '';
  709. } else {
  710. $goods_sale_unit = D('Home/Front')->get_config_by_name('goods_sale_unit');
  711. return $goods_sale_unit ? $goods_sale_unit : '件';
  712. }
  713. }
  714. }