GoodsModel.class.php 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270
  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 Admin\Model;
  15. use Think\Model;
  16. class GoodsModel extends Model{
  17. /**
  18. 获取商品数量
  19. **/
  20. public function get_goods_count($where = '')
  21. {
  22. $total = M('lionfish_comshop_goods')->where(' 1=1 '.$where)->count();
  23. return $total;
  24. }
  25. public function show_bargain_page($search)
  26. {
  27. $sql='SELECT p.goods_id,p.name,p.quantity,p.type as goods_type,pg.begin_time,pg.end_time,p.status,pg.id,p.price,p.image,pg.bargain_price,pg.bargain_count FROM '
  28. .C('DB_PREFIX').'bargain_goods as pg left join '.C('DB_PREFIX').'goods as p on pg.goods_id=p.goods_id where 1=1 ';
  29. if(isset($search['customer_id'])){
  30. $sql.=" and p.store_id = ".$search['customer_id'];
  31. }
  32. //name
  33. if(isset($search['name'])){
  34. $sql.=" and p.name like '%".$search['name']."%'";
  35. }
  36. //'customer_id' => UID
  37. $count=count(M()->query($sql));
  38. $Page = new \Think\Page($count,C('BACK_PAGE_NUM'));
  39. $show = $Page->show();// 分页显示输出
  40. $sql.=' order by pg.id desc LIMIT '.$Page->firstRow.','.$Page->listRows;
  41. $list=M()->query($sql);
  42. foreach ($list as $key => $value) {
  43. $list[$key]['image']=resize($value['image'], 50, 50);
  44. }
  45. return array(
  46. 'empty'=>'<tr><td colspan="20">~~暂无数据</td></tr>',
  47. 'list'=>$list,
  48. 'page'=>$show
  49. );
  50. }
  51. public function show_pingoods_page($search)
  52. {
  53. $sql='SELECT p.goods_id,p.name,p.quantity,p.type as goods_type,pg.type,pg.begin_time,pg.end_time,p.status,pg.id,p.price,p.image,pg.pin_price,pg.pin_count FROM '
  54. .C('DB_PREFIX').'pin_goods as pg left join '.C('DB_PREFIX').'goods as p on pg.goods_id=p.goods_id where 1=1 ';
  55. if(isset($search['customer_id'])){
  56. $sql.=" and p.store_id = ".$search['customer_id'];
  57. }
  58. //name
  59. if(isset($search['name'])){
  60. $sql.=" and p.name like '%".$search['name']."%'";
  61. }
  62. //'customer_id' => UID
  63. $count=count(M()->query($sql));
  64. $Page = new \Think\Page($count,C('BACK_PAGE_NUM'));
  65. $show = $Page->show();// 分页显示输出
  66. $sql.=' order by pg.id desc LIMIT '.$Page->firstRow.','.$Page->listRows;
  67. $list=M()->query($sql);
  68. foreach ($list as $key => $value) {
  69. $list[$key]['image']=resize($value['image'], 50, 50);
  70. }
  71. return array(
  72. 'empty'=>'<tr><td colspan="20">~~暂无数据</td></tr>',
  73. 'list'=>$list,
  74. 'page'=>$show
  75. );
  76. }
  77. function copy_goods($goods_id){
  78. $query = M()->query("SELECT DISTINCT * FROM " . C('DB_PREFIX') . "goods p LEFT JOIN " . C('DB_PREFIX') . "goods_description pd ON (p.goods_id = pd.goods_id) WHERE p.goods_id =" . (int)$goods_id);
  79. if ($query) {
  80. $data = $query[0];
  81. $data['viewed'] = '0';
  82. $data['image']='';
  83. $data['goods_description'] =M('goods_description')->where(array('goods_id'=>$goods_id))->find();
  84. $data['goods_description']['name']=$data['name'];
  85. $data['goods_discount'] = M('goods_discount')->where(array('goods_id'=>$goods_id))->select();
  86. $category = M('goods_to_category')->where(array('goods_id'=>$goods_id))->select();
  87. foreach ($category as $k => $v) {
  88. $data['goods_category'][]=$v['category_id'];
  89. }
  90. $this->add_Goods($data);
  91. }
  92. }
  93. public function del_Goods($id){
  94. try{
  95. $image=M('goods')->where(array('goods_id'=>$id))->field('image')->find();
  96. if(!empty($image)){
  97. A('Image')->del_image('goods',$image['image'],'goods');
  98. }
  99. $gallery=M('goods_image')->where(array('goods_id'=>$id))->field('image')->select();
  100. if(!empty($gallery)){
  101. foreach ($gallery as $key => $value) {
  102. A('Image')->del_image('gallery',$value['image'],'gallery');
  103. }
  104. }
  105. M('goods')->where(array('goods_id'=>$id))->delete();
  106. M('goods_description')->where(array('goods_id'=>$id))->delete();
  107. M('goods_image')->where(array('goods_id'=>$id))->delete();
  108. M('goods_to_category')->where(array('goods_id'=>$id))->delete();
  109. M('goods_discount')->where(array('goods_id'=>$id))->delete();
  110. M('goods_option')->where(array('goods_id'=>$id))->delete();
  111. M('goods_option_value')->where(array('goods_id'=>$id))->delete();
  112. M('goods_area')->where(array('goods_id'=>$id))->delete();
  113. return array(
  114. 'status'=>'success',
  115. 'message'=>'删除成功',
  116. 'jump'=>U('Goods/index')
  117. );
  118. }catch(Exception $e){
  119. return array(
  120. 'status'=>'fail',
  121. 'message'=>'删除失败,未知异常',
  122. 'jump'=>U('Goods/index')
  123. );
  124. }
  125. }
  126. public function get_goods_data($id){
  127. $d=M('Goods')->find($id);
  128. $d['thumb_image']=resize($d['image'], 100, 100);
  129. if(!empty($d['fan_image']))
  130. {
  131. $d['fan_image_thumb']=resize($d['fan_image'], 100, 100);
  132. }
  133. return $d;
  134. }
  135. public function get_goods_image_data($id){
  136. $d=M('goods_image')->where(array('goods_id'=>$id))->select();
  137. foreach ($d as $k => $v) {
  138. $d[$k]['thumb']=resize($v['image'], 100, 100);
  139. }
  140. return $d;
  141. }
  142. public function get_goods_category_data($id){
  143. $sql='SELECT pc.name FROM '.C('DB_PREFIX').'goods_to_category ptc,'
  144. .C('DB_PREFIX').'goods_category pc WHERE (pc.id=ptc.class_id1 or pc.id=ptc.class_id2 or pc.id=ptc.class_id3) AND ptc.goods_id='.$id.' order by pc.pid asc,sort_order asc';
  145. $d=M()->query($sql);
  146. return $d;
  147. }
  148. public function show_comment_page($search){
  149. $sql='SELECT * FROM '.C('DB_PREFIX').'order_comment where 1= 1 ';
  150. if(isset($search['goods_id']))
  151. {
  152. $sql.=" and goods_id=".$search['goods_id'];
  153. }
  154. $count=count(M()->query($sql));
  155. $Page = new \Think\Page($count,C('BACK_PAGE_NUM'));
  156. $show = $Page->show();// 分页显示输出
  157. $sql.=' order by state asc,add_time desc LIMIT '.$Page->firstRow.','.$Page->listRows;
  158. $list=M()->query($sql);
  159. foreach($list as $key => $val)
  160. {
  161. //member_id
  162. $member_info = M('member')->field('name,avatar')->where( array('member_id' => intval($val['member_id'])) )->find();
  163. $val['user_name'] = $member_info['name'];
  164. $val['avatar'] = $member_info['avatar'];
  165. $list[$key] = $val;
  166. }
  167. return array(
  168. 'empty'=>'<tr><td colspan="20">~~暂无数据</td></tr>',
  169. 'list'=>$list,
  170. 'page'=>$show
  171. );
  172. }
  173. public function show_guobie_page()
  174. {
  175. $sql='SELECT * from '.C('DB_PREFIX').'guobie ';
  176. $count=count(M()->query($sql));
  177. $Page = new \Think\Page($count,C('BACK_PAGE_NUM'));
  178. $show = $Page->show();// 分页显示输出
  179. $sql.=' order by id desc LIMIT '.$Page->firstRow.','.$Page->listRows;
  180. $list=M()->query($sql);
  181. return array(
  182. 'empty'=>'<tr><td colspan="20">~~暂无数据</td></tr>',
  183. 'list'=>$list,
  184. 'page'=>$show
  185. );
  186. }
  187. public function show_goods_page($search){
  188. $sql='SELECT p.goods_id,p.name,p.model,p.price,p.danprice,p.is_index_show,pd.reason,p.index_sort,p.quantity,p.store_id,p.status,p.model,p.image,p.type,p.lock_type,gtc.class_id1,gtc.class_id2,gtc.class_id3 FROM '.C('DB_PREFIX').'goods_description pd,'
  189. .C('DB_PREFIX').'goods p,'.C('DB_PREFIX').'goods_to_category gtc WHERE pd.goods_id=p.goods_id and p.goods_id=gtc.goods_id';
  190. if(isset($search['name'])){
  191. $sql.=" and p.name like '%".$search['name']."%'";
  192. }
  193. if(isset($search['category'])){
  194. $sql.=" and (gtc.class_id1=".$search['category']." or gtc.class_id2=".$search['category']." or gtc.class_id3=".$search['category'].")";
  195. }
  196. if(isset($search['status'])){
  197. $sql.=" and p.status=".$search['status'];
  198. }else {
  199. $sql.=" and p.status!=4 and p.status != 5";
  200. }
  201. if(isset($search['store_id']))
  202. {
  203. $sql.=" and p.store_id=".$search['store_id'];
  204. }
  205. if(isset($search['type']))
  206. {
  207. if($search['type'] == 'activity')
  208. {
  209. $sql.=" and p.type != 'normal'";
  210. } else {
  211. $sql.=" and p.type= '".$search['type']."'";
  212. }
  213. }
  214. if(isset($search['_string']))
  215. {
  216. $sql.=" and ".$search['_string'];
  217. }
  218. $count=count(M()->query($sql));
  219. $Page = new \Think\Page($count,C('BACK_PAGE_NUM'));
  220. $show = $Page->show();// 分页显示输出
  221. $sql.=' order by p.goods_id desc LIMIT '.$Page->firstRow.','.$Page->listRows;
  222. $list=M()->query($sql);
  223. foreach ($list as $key => $value) {
  224. $list[$key]['image']=resize($value['image'], 50, 50);
  225. $class_info1 = M('goods_category')->where( array('id' => $value['class_id1']) )->field('name')->find();
  226. $list[$key]['class_name1'] = $class_info1['name'];
  227. $class_info2 = M('goods_category')->where( array('id' => $value['class_id2']) )->field('name')->find();
  228. $list[$key]['class_name2'] = $class_info2['name'];
  229. $class_info3 = M('goods_category')->where( array('id' => $value['class_id3']) )->field('name')->find();
  230. $list[$key]['class_name3'] = $class_info3['name'];
  231. }
  232. return array(
  233. 'empty'=>'<tr><td colspan="20">~~暂无数据</td></tr>',
  234. 'list'=>$list,
  235. 'page'=>$show
  236. );
  237. }
  238. public function validate($data){
  239. $error=array();
  240. if(empty($data['goods_description']['name'])){
  241. $error='产品名称必填';
  242. }
  243. if(!isset($data['class_1']) &&!isset($data['class_2'])&& !isset($data['class_3']) ){
  244. $error='产品分类必填';
  245. }else if( empty($data['class_1'])){
  246. $error='产品分类必填';
  247. }
  248. if($error){
  249. return array(
  250. 'status'=>'back',
  251. 'message'=>$error
  252. );
  253. }
  254. }
  255. public function edit_Goods($data){
  256. $error=array();
  257. if(empty($data['goods_description']['name'])){
  258. $error='产品名称必填';
  259. }
  260. if($error){
  261. return array(
  262. 'status'=>'back',
  263. 'message'=>$error
  264. );
  265. }
  266. $id=$data['goods_id'];
  267. $goods['goods_id']=$id;
  268. $goods['name']=$data['goods_description']['name'];
  269. $goods['image']=$data['image'];
  270. $goods['fan_image']=$goods['image'];
  271. $water_image = '';
  272. if(!empty($goods['fan_image']))
  273. {
  274. $water_image = get_team_water_image($goods['fan_image']);
  275. }
  276. $goods['model']=$data['model'];
  277. $goods['danprice']=$data['danprice'];
  278. $goods['virtual_count'] = $data['virtual_count'];
  279. $goods['pick_just'] = $data['pick_just'];
  280. $goods['is_free_in'] = $data['is_free_in'];
  281. if( !empty($data['express']) )
  282. {
  283. $need_express = array();
  284. foreach($data['express'] as $express)
  285. {
  286. $need_express[$express] = array('express_id' => $express,'price' => $data['express_price_'.$express] );
  287. }
  288. $goods['express_list'] = serialize($need_express);
  289. } else if( empty($data['express']) && $data['express'] > 0){
  290. $goods['express_list'] = '';
  291. }
  292. if( !empty($data['pick_just']) && $data['pick_just'] >0)
  293. {
  294. $goods['pick_up'] = serialize($data['pick_up']);
  295. } else if( empty($data['pick_up']) && $data['store_id'] > 0){
  296. $goods['pick_up'] = '';
  297. }
  298. $goods['price']=$data['price'];
  299. $goods['quantity']=$data['quantity'];
  300. $goods['transport_id'] = $data['transport_id'];
  301. $goods['goods_freight'] = $data['goods_freight'];
  302. $goods['shipping']=$data['shipping'];
  303. $goods['weight']=$data['weight'];
  304. $goods['head_disc']=$data['head_disc'];
  305. $goods['commiss_three_dan_disc'] = $data['commiss_three_dan_disc'];
  306. $goods['commiss_two_dan_disc'] = $data['commiss_two_dan_disc'];
  307. $goods['commiss_one_dan_disc'] = $data['commiss_one_dan_disc'];
  308. $goods['commiss_fen_one_disc'] = isset($data['commiss_fen_one_disc']) ? $data['commiss_fen_one_disc']:'';
  309. $goods['commiss_fen_two_disc'] = isset($data['commiss_fen_two_disc']) ? $data['commiss_fen_two_disc']:'';
  310. $goods['commiss_fen_three_disc'] = isset($data['commiss_fen_three_disc']) ? $data['commiss_fen_three_disc']:'';
  311. //commiss_fen_one_disc commiss_fen_two_disc commiss_fen_three_disc
  312. $goods['points'] = $data['points'];
  313. $goods['sort_order']=$data['sort_order'];
  314. $goods['date_modified']=date('Y-m-d H:i:s',time());
  315. $goods['status']=$data['status'];
  316. if(isset($data['type']))
  317. {
  318. $goods['type'] = $data['type'];
  319. }
  320. if(isset($data['guobie_id']))
  321. {
  322. $goods['guobie_id'] = $data['guobie_id'];
  323. }
  324. $r=M('Goods')->save($goods);
  325. $quantity = $data['quantity'];
  326. if($r){
  327. $goods_description['summary']=$data['goods_description']['summary'];
  328. $goods_description['activity_summary']=$data['goods_description']['activity_summary'];
  329. $goods_description['description']=$data['goods_description']['description'];
  330. $goods_description['tag']=$data['goods_description']['tag'];
  331. $goods_description['share_title']=$data['goods_description']['share_title'];
  332. $goods_description['share_group_title']=$data['goods_description']['share_group_title'];
  333. $goods_description['share_descript']=$data['goods_description']['share_descript'];
  334. $goods_description['per_number']=$data['goods_description']['per_number'];
  335. $goods_description['is_video']=$data['goods_description']['is_video'];
  336. $goods_description['video_src']=$data['goods_description']['video_src'];
  337. $goods_description['video_size_width']=$data['goods_description']['video_size_width'];
  338. $goods_description['vedio_size_height']=$data['goods_description']['vedio_size_height'];
  339. //goods_description[is_video]
  340. //goods_description[video_src]
  341. //goods_description[video_src]
  342. //goods_description[video_src]
  343. $goods_description['water_image'] = $water_image;
  344. $member_model= D('Admin/Member');
  345. $level_list = $member_model->show_member_level();
  346. $member_default_levelname_info = M('config')->where( array('name' => 'member_default_levelname') )->find();
  347. $member_defualt_discount_info = M('config')->where( array('name' => 'member_defualt_discount') )->find();
  348. $default = array('id'=>'default', 'level' => 0,'levelname' => $member_default_levelname_info['value'],'discount' => $member_defualt_discount_info['value']);
  349. array_unshift($level_list['list'], $default );
  350. $need_level_list = $level_list['list'];
  351. $need_disc = array();
  352. foreach($need_level_list as $val)
  353. {
  354. $need_disc[$val['level']] = $data['level_'.$val['level']];
  355. }
  356. $goods_description['is_untake_level'] = isset($data['isuntake_in_level']) ? intval($data['isuntake_in_level']) : 0;
  357. $goods_description['level_discount'] = serialize($need_disc);
  358. M('goods_description')->where(array('goods_id'=>$id))->save($goods_description);
  359. //citychk
  360. M('goods_area')->where( array('goods_id' => $id) )->delete();
  361. M('goods_area')->add( array('goods_id' =>$id,'area_ids_text' => serialize($data['citychk']) ) );
  362. try{
  363. //商品分类
  364. if(isset($data['class_1']) && !empty($data['class_1']) ){
  365. //M('goods_to_category')->where(array('goods_id'=>$id))->delete();
  366. $old_goods_info = M('goods_to_category')->where(array('goods_id'=>$id))->find();
  367. if( empty($old_goods_info) )
  368. {
  369. $this->execute("INSERT INTO " . C('DB_PREFIX') . "goods_to_category SET goods_id = '" . (int)$id . "', class_id1 = '" . (int)$data['class_1'] . "', class_id2 = '" . (int)$data['class_2'] . "', class_id3 = '" . (int)$data['class_3'] . "'");
  370. }else{
  371. //class_id1 = '" . (int)$data['class_1'] . "', class_id2 = '" . (int)$data['class_2'] . "', class_id3 = '" . (int)$data['class_3']
  372. M('goods_to_category')->where(array('goods_id'=>$id))->save( array('class_id1' => $data['class_1'], 'class_id2' => $data['class_2'], 'class_id3' => $data['class_3']) );
  373. }
  374. }
  375. M('GoodsImage')->where(array('goods_id'=>$id))->delete();
  376. if (isset($data['goods_image'])) {
  377. foreach ($data['goods_image'] as $goods_image) {
  378. $this->execute("INSERT INTO " . C('DB_PREFIX') . "goods_image SET goods_id = '" . (int)$id . "', image = '" . $goods_image['image'] . "', sort_order = '" . (int)$goods_image['sort_order'] . "',". " is_video_click = '" . (int)$goods_image['is_video_click'] . "'");
  379. }
  380. }
  381. //商品选项
  382. //商品选项
  383. //M('goods_option')->where(array('goods_id'=>$id))->delete();
  384. //M('goods_option_value')->where(array('goods_id'=>$id))->delete();
  385. $goods_option_old = M('goods_option')->where(array('goods_id'=>$id))->select();
  386. $goods_option_old_goodsid_optionid = array();
  387. foreach($goods_option_old as $old_val)
  388. {
  389. $goods_option_old_goodsid_optionid[$old_val['goods_id'].'_'.$old_val['option_id']] = $old_val['goods_id'].'_'.$old_val['option_id'];
  390. }
  391. $goods_option_value_old = M('goods_option_value')->where(array('goods_id'=>$id))->select();
  392. $goods_option_old_goodsid_optionvalueid = array();
  393. foreach($goods_option_value_old as $old_val)
  394. {
  395. $goods_option_old_goodsid_optionvalueid[$old_val['goods_id'].'_'.$old_val['option_value_id']] = $old_val['goods_id'].'_'.$old_val['option_value_id'];
  396. }
  397. $old_goods_option_mult_value = M('goods_option_mult_value')->where(array('goods_id'=>$id))->select();
  398. $old_goods_option_mult_value_relagoodsoptionvalueid = array();
  399. foreach($old_goods_option_mult_value as $old_val)
  400. {
  401. $old_goods_option_mult_value_relagoodsoptionvalueid[$old_val['rela_goodsoption_valueid']] = $old_val['rela_goodsoption_valueid'];
  402. }
  403. $goods_option_value_arr = $data['goods_option_value'];
  404. $rela_option_value = array();
  405. if(!empty($goods_option_value_arr))
  406. {
  407. foreach($goods_option_value_arr as $val)
  408. {
  409. $res = M('option_value')->field('option_id,option_value_id')->where( array('option_value_id' =>$val) )->find();
  410. if( !isset($rela_option_value[$res['option_id']]) || !in_array())
  411. {
  412. $rela_option_value[$res['option_id']][] = $res;
  413. }
  414. }
  415. }
  416. if (isset($data['goods_option']) && !empty($data['goods_option'])) {
  417. $data['goods_option'] = explode(',', $data['goods_option']);
  418. foreach ($data['goods_option'] as $goods_option_id) {
  419. $goods_option = M('option')->where( array('option_id' =>$goods_option_id) )->find();
  420. $option['goods_id']=$id;
  421. $option['option_id']=(int)$goods_option['option_id'];
  422. $option['required'] =(int)$goods_option['required'];
  423. $option['option_name'] =$goods_option['name'];
  424. $option['type'] =$goods_option['type'];
  425. if( in_array($id.'_'.$goods_option['option_id'], $goods_option_old_goodsid_optionid) )
  426. {
  427. $old_goods_option = M('goods_option')->where( array('goods_id' =>$id,'option_id' =>$goods_option['option_id']) )->find();
  428. unset($goods_option_old_goodsid_optionid[$id.'_'.$goods_option['option_id']]);
  429. M('goods_option')->where( array('goods_id' =>$id,'option_id' =>$goods_option['option_id']) )->save($option);
  430. $option_id=$old_goods_option['goods_option_id'];
  431. } else {
  432. $option_id=M('goods_option')->add($option);
  433. }
  434. //$rela_option_value[$res['option_id']][] = $res;
  435. $option_value_list = $rela_option_value[$goods_option_id];
  436. if (isset($option_value_list) && count($option_value_list) > 0 ) {
  437. foreach ($option_value_list as $goods_option_value) {
  438. $option_value['goods_option_id']=(int)$option_id;
  439. $option_value['goods_id']=$id;
  440. $option_value['option_id']=(int)$goods_option['option_id'];
  441. $option_value['image']='';
  442. $option_value['option_value_id']=(int)$goods_option_value['option_value_id'];
  443. $option_value['quantity']=0;
  444. $option_value['subtract']='';
  445. $option_value['price']=0 ;
  446. $option_value['price_prefix']=0;
  447. $option_value['weight']=0;
  448. $option_value['weight_prefix']=0;
  449. if( in_array($id.'_'.$goods_option_value['option_value_id'], $goods_option_old_goodsid_optionvalueid) )
  450. {
  451. unset($goods_option_old_goodsid_optionvalueid[$id.'_'.$goods_option_value['option_value_id']]);
  452. M('goods_option_value')->where( array('option_value_id' =>$goods_option_value['option_value_id'], 'goods_id' => $id) )->save($option_value);
  453. } else {
  454. M('goods_option_value')->add($option_value);
  455. }
  456. }
  457. }
  458. }
  459. //M('goods_option')->where(array('goods_id'=>$id))->delete();
  460. //商品多选项目 $data['mult_option_zuhe']
  461. $min_dan_price = $data['danprice'];
  462. //$id=$data['goods_id'];
  463. //$goods['danprice']=$data['danprice'];
  464. if(isset($data['mult_option_zuhe']) && !empty($data['mult_option_zuhe']))
  465. {
  466. //M('goods_option_mult_value')->where(array('goods_id'=>$id))->delete();
  467. //rela_goodsoption_valueid
  468. if(!empty($data['mult_option_zuhe']))
  469. {
  470. $mult_option_arr = explode(',', $data['mult_option_zuhe']);
  471. $option_value_id_arr = array();
  472. $new_quantity = 0;
  473. foreach($mult_option_arr as $mult_option)
  474. {
  475. $dan_option = explode('@@',$mult_option);
  476. $mult_id_arr = explode(':',$dan_option[0]);
  477. $mult_quantity_arr = explode(':',$dan_option[1]);
  478. $mult_image_arr = explode(':',$dan_option[2]);
  479. $mult_price_arr = explode(':',$dan_option[3]);
  480. $mult_weight_arr = explode(':',$dan_option[4]);
  481. $tmp_option_value_id = explode('_',$mult_id_arr[1]);
  482. foreach($tmp_option_value_id as $vv)
  483. {
  484. if(empty($option_value_id_arr) || !isset($option_value_id_arr[$vv]))
  485. {
  486. $option_value_id_arr[$vv] = $mult_quantity_arr[1];
  487. } else{
  488. $option_value_id_arr[$vv] += $mult_quantity_arr[1];
  489. }
  490. }
  491. $mul_option_data = array();
  492. $mul_option_data['rela_goodsoption_valueid'] = $mult_id_arr[1];
  493. $mul_option_data['goods_id'] = $id;
  494. $mul_option_data['pin_price'] = 0;
  495. $mul_option_data['weight'] = $mult_weight_arr[1];
  496. if($min_dan_price > $mult_price_arr[1])
  497. {
  498. $min_dan_price = $mult_price_arr[1];
  499. }
  500. $mul_option_data['dan_price'] = $mult_price_arr[1];
  501. $mul_option_data['quantity'] = $mult_quantity_arr[1];
  502. $mul_option_data['image'] = $mult_image_arr[1];
  503. if( in_array($mult_id_arr[1],$old_goods_option_mult_value_relagoodsoptionvalueid ) )
  504. {
  505. unset($mul_option_data['pin_price']);
  506. unset($old_goods_option_mult_value_relagoodsoptionvalueid[$mult_id_arr[1]]);
  507. M('goods_option_mult_value')->where( array('rela_goodsoption_valueid' =>$mult_id_arr[1] , 'goods_id' => $id) )->save($mul_option_data);
  508. }else {
  509. M('goods_option_mult_value')->add($mul_option_data);
  510. }
  511. $new_quantity += $mul_option_data['quantity'];
  512. }
  513. if($data['danprice'] > $min_dan_price)
  514. {
  515. M('goods')->where( array('goods_id' => $id) )->save( array('danprice' => $min_dan_price) );
  516. }
  517. //rela_goodsoption_valueid
  518. //var_dump($old_goods_option_mult_value_relagoodsoptionvalueid);die();
  519. if($new_quantity != $quantity)
  520. {
  521. //更新库存,以规格库存为依据
  522. M('goods')->where( array('goods_id' => $id) )->save( array('quantity' => $new_quantity) );
  523. }
  524. if(!empty($option_value_id_arr))
  525. {
  526. foreach($option_value_id_arr as $key => $id_quantity)
  527. {
  528. M('goods_option_value')->where( array('goods_id' => $id,'option_value_id' =>$key) )->save( array('quantity' => $id_quantity) );
  529. }
  530. }
  531. }
  532. }
  533. }
  534. if( !empty($goods_option_old_goodsid_optionid ))
  535. {
  536. foreach($goods_option_old_goodsid_optionid as $vv)
  537. {
  538. $tp_arr = explode('_',$vv);
  539. M('goods_option')->where( array('goods_id' =>$tp_arr[0],'option_id' =>$tp_arr[1]) )->delete();
  540. }
  541. }
  542. if(!empty($goods_option_old_goodsid_optionvalueid))
  543. {
  544. foreach($goods_option_old_goodsid_optionvalueid as $vv)
  545. {
  546. $tp_arr = explode('_',$vv);
  547. M('goods_option_value')->where( array('option_value_id' =>$tp_arr[1], 'goods_id' => $tp_arr[0]) )->delete();
  548. }
  549. }
  550. if( !empty($old_goods_option_mult_value_relagoodsoptionvalueid) )
  551. {
  552. foreach($old_goods_option_mult_value_relagoodsoptionvalueid as $vv)
  553. {
  554. //$tp_arr = explode('_',$vv);
  555. $rs = M('goods_option_mult_value')->where( array('rela_goodsoption_valueid' =>$vv , 'goods_id' => $id) )->delete();
  556. }
  557. }
  558. return array(
  559. 'status'=>'success',
  560. 'message'=>'修改成功',
  561. 'jump'=>U('Goods/index')
  562. );
  563. }catch(Exception $e){
  564. return array(
  565. 'status'=>'fail',
  566. 'message'=>'修改失败,未知异常',
  567. 'jump'=>U('Goods/index')
  568. );
  569. }
  570. }else{
  571. return array(
  572. 'status'=>'fail',
  573. 'message'=>'修改失败',
  574. 'jump'=>U('Goods/index')
  575. );
  576. }
  577. }
  578. function add_caiji_Goods($data)
  579. {
  580. /**
  581. $goods_data['title'] = $item['title'];
  582. $goods_data['marketprice'] = $item['marketprice'];
  583. $goods_data['sales'] = $item['sales'];
  584. $goods_data['quantity'] = $item['total'];
  585. $goods_data['html'] = $html;
  586. **/
  587. $name = $data['title'];
  588. $image = '';
  589. if( !empty($data['image_lists']) )
  590. {
  591. $tmp_img = array_slice($data['image_lists'],0,1);
  592. $image = $tmp_img[0];
  593. }
  594. $goods['name']=$name;
  595. $goods['image']=$image;
  596. $goods['fan_image']=$image;
  597. $water_image = '';
  598. if(!empty($goods['fan_image']))
  599. {
  600. $water_image = get_team_water_image($goods['fan_image']);
  601. }
  602. $goods['model']='';
  603. $goods['pinprice']=$data['marketprice'];
  604. $goods['danprice']=$data['marketprice'];
  605. $goods['pin_count']=2;
  606. $goods['pin_hour']=24;
  607. $goods['head_disc'] = 100;
  608. $goods['commiss_one_money'] = 0;
  609. $goods['commiss_one_pin_disc'] = 0;
  610. $goods['commiss_one_dan_disc'] = 0;
  611. $goods['virtual_count'] = $data['sales'];
  612. $goods['pick_just'] = 0;
  613. $goods['is_free_in'] = 0;
  614. $goods['express_list'] = '';
  615. $goods['pick_up'] = '';
  616. $goods['sku']='';
  617. $goods['price']=$data['marketprice'];
  618. $goods['quantity']=$data['quantity'];
  619. $goods['transport_id'] = 0;
  620. $goods['goods_freight'] = 0;
  621. $goods['shipping']=$data['shipping'];
  622. $goods['store_id']=$data['store_id'];
  623. $goods['status']=5;
  624. $goods['sort_order']=0;
  625. $goods['date_added']=date('Y-m-d H:i:s',time());
  626. $goods['date_modified']=date('Y-m-d H:i:s',time());
  627. $goods_id=M('Goods')->add($goods);
  628. if($goods_id){
  629. try{
  630. $goods_description['goods_id']=$goods_id;
  631. $goods_description['summary']='';
  632. $goods_description['description']= htmlspecialchars($data['html']);
  633. $goods_description['tag']='';
  634. $goods_description['per_number']=0;
  635. $goods_description['water_image'] = $water_image;
  636. M('goods_description')->add($goods_description);
  637. M('goods_area')->add( array('goods_id' =>$goods_id,'area_ids_text' => serialize(array()) ) );
  638. //商品分类
  639. $this->execute("INSERT INTO " . C('DB_PREFIX') . "goods_to_category SET goods_id = '" . (int)$goods_id . "', class_id1 = '0', class_id2 = '0', class_id3 = '0'");
  640. if (isset($data['image_lists'])) {
  641. foreach ($data['image_lists'] as $goods_image) {
  642. $this->execute("INSERT INTO " . C('DB_PREFIX') . "goods_image SET goods_id = '" . (int)$goods_id . "', image = '" . $goods_image . "', sort_order = '0'");
  643. }
  644. }
  645. return true;
  646. }catch(Exception $e){
  647. return false;
  648. }
  649. }else{
  650. return false;
  651. }
  652. }
  653. function add_Goods($data){
  654. $error=$this->validate($data);
  655. if($error){
  656. return $error;
  657. }
  658. $goods['name']=$data['goods_description']['name'];
  659. $goods['image']=$data['image'];
  660. $goods['fan_image']=$goods['image'];
  661. $water_image = '';
  662. if(!empty($goods['fan_image']))
  663. {
  664. $water_image = get_team_water_image($goods['fan_image']);
  665. }
  666. $goods['model']=$data['model'];
  667. $goods['danprice']=$data['danprice'];
  668. $goods['virtual_count'] = $data['virtual_count'];
  669. $goods['pick_just'] = $data['pick_just'];
  670. $goods['is_free_in'] = $data['is_free_in'];
  671. if( !empty($data['express']) )
  672. {
  673. $need_express = array();
  674. foreach($data['express'] as $express)
  675. {
  676. $need_express[$express] = array('express_id' => $express,'price' => $data['express_price_'.$express] );
  677. }
  678. $goods['express_list'] = serialize($need_express);
  679. } else if( empty($data['express']) && $data['express'] > 0){
  680. $goods['express_list'] = '';
  681. }
  682. if( !empty($data['pick_just']) && $data['pick_just'] >0 )
  683. {
  684. $goods['pick_up'] = serialize($data['pick_up']);
  685. } else if( empty($data['pick_up']) && $data['store_id'] > 0){
  686. $goods['pick_up'] = '';
  687. }
  688. $goods['sku']='';
  689. $goods['price']=$data['price'];
  690. $goods['quantity']= $data['quantity'];
  691. $goods['transport_id'] = $data['transport_id'];
  692. $goods['goods_freight'] = $data['goods_freight'];
  693. $goods['weight'] = $data['weight'];
  694. $goods['head_disc'] = $data['head_disc'];
  695. $goods['commiss_three_dan_disc'] = $data['commiss_three_dan_disc'];
  696. $goods['commiss_two_dan_disc'] = $data['commiss_two_dan_disc'];
  697. $goods['commiss_one_dan_disc'] = $data['commiss_one_dan_disc'];
  698. $goods['commiss_fen_one_disc'] = isset($data['commiss_fen_one_disc']) ? $data['commiss_fen_one_disc']:'';
  699. $goods['commiss_fen_two_disc'] = isset($data['commiss_fen_two_disc']) ? $data['commiss_fen_two_disc']:'';
  700. $goods['commiss_fen_three_disc'] = isset($data['commiss_fen_three_disc']) ? $data['commiss_fen_three_disc']:'';
  701. $goods['points'] = $data['points'];
  702. $goods['shipping']=$data['shipping'];
  703. $goods['store_id']=$data['store_id'];
  704. $goods['status']=$data['status'];
  705. $goods['sort_order']=$data['sort_order'];
  706. $goods['date_added']=date('Y-m-d H:i:s',time());
  707. $goods['date_modified']=date('Y-m-d H:i:s',time());
  708. if(isset($data['type']))
  709. {
  710. $goods['type'] = $data['type'];
  711. }
  712. //$goods['points'] = $data['points'];
  713. $goods_id=M('Goods')->add($goods);
  714. $quantity = $data['quantity'];
  715. if($goods_id){
  716. try{
  717. $goods_description['goods_id']=$goods_id;
  718. $goods_description['summary']=$data['goods_description']['summary'];
  719. $goods_description['activity_summary']=$data['goods_description']['activity_summary'];
  720. $goods_description['share_group_title']=$data['goods_description']['share_group_title'];
  721. $goods_description['share_title']=$data['goods_description']['share_title'];
  722. $goods_description['share_descript']=$data['goods_description']['share_descript'];
  723. $goods_description['description']=$data['goods_description']['description'];
  724. $goods_description['tag']=$data['goods_description']['tag'];
  725. $goods_description['per_number']=$data['goods_description']['per_number'];
  726. $goods_description['water_image'] = $water_image;
  727. $goods_description['is_video']=$data['goods_description']['is_video'];
  728. $goods_description['video_src']=$data['goods_description']['video_src'];
  729. $goods_description['video_size_width']=$data['goods_description']['video_size_width'];
  730. $goods_description['vedio_size_height']=$data['goods_description']['vedio_size_height'];
  731. $member_model= D('Admin/Member');
  732. $level_list = $member_model->show_member_level();
  733. $member_default_levelname_info = M('config')->where( array('name' => 'member_default_levelname') )->find();
  734. $member_defualt_discount_info = M('config')->where( array('name' => 'member_defualt_discount') )->find();
  735. $default = array('id'=>'default', 'level' => 0,'levelname' => $member_default_levelname_info['value'],'discount' => $member_defualt_discount_info['value']);
  736. array_unshift($level_list['list'], $default );
  737. $need_level_list = $level_list['list'];
  738. $need_disc = array();
  739. foreach($need_level_list as $val)
  740. {
  741. $need_disc[$val['level']] = $data['level_'.$val['level']];
  742. }
  743. $goods_description['is_untake_level'] = isset($data['isuntake_in_level']) ? intval($data['isuntake_in_level']) : 0;
  744. $goods_description['level_discount'] = serialize($need_disc);
  745. M('goods_description')->add($goods_description);
  746. M('goods_area')->add( array('goods_id' =>$goods_id,'area_ids_text' => serialize($data['citychk']) ) );
  747. //商品分类
  748. if(isset($data['class_1']) || isset($data['class_2']) || isset($data['class_3'])){
  749. $this->execute("INSERT INTO " . C('DB_PREFIX') . "goods_to_category SET goods_id = '" . (int)$goods_id . "', class_id1 = '" . (int)$data['class_1'] . "', class_id2 = '" . (int)$data['class_2'] . "', class_id3 = '" . (int)$data['class_3'] . "'");
  750. }
  751. if (isset($data['goods_image'])) {
  752. foreach ($data['goods_image'] as $goods_image) {
  753. $this->execute("INSERT INTO " . C('DB_PREFIX') . "goods_image SET goods_id = '" . (int)$goods_id . "', image = '" . $goods_image['image'] . "', sort_order = '" . (int)$goods_image['sort_order'] . "',". " is_video_click = '" . (int)$goods_image['is_video_click'] . "'");
  754. }
  755. }
  756. //商品选项
  757. if (isset($data['goods_option']) && !empty($data['goods_option'])) {
  758. $goods_option_value_arr = $data['goods_option_value'];
  759. $rela_option_value = array();
  760. if(!empty($goods_option_value_arr))
  761. {
  762. foreach($goods_option_value_arr as $val)
  763. {
  764. $res = M('option_value')->field('option_id,option_value_id')->where( array('option_value_id' =>$val) )->find();
  765. if( !isset($rela_option_value[$res['option_id']]) || !in_array())
  766. {
  767. $rela_option_value[$res['option_id']][] = $res;
  768. }
  769. }
  770. }
  771. $data['goods_option'] = explode(',', $data['goods_option']);
  772. foreach ($data['goods_option'] as $goods_option_id) {
  773. $goods_option = M('option')->where( array('option_id' =>$goods_option_id) )->find();
  774. $option['goods_id']=$goods_id;
  775. $option['option_id']=(int)$goods_option['option_id'];
  776. $option['required'] =(int)$goods_option['required'];
  777. $option['option_name'] =$goods_option['name'];
  778. $option['type'] =$goods_option['type'];
  779. $option_id=M('goods_option')->add($option);
  780. $option_value_list = $rela_option_value[$goods_option_id];
  781. if (isset($option_value_list) && count($option_value_list) > 0 ) {
  782. foreach ($option_value_list as $goods_option_value) {
  783. $option_value['goods_option_id']=(int)$option_id;
  784. $option_value['goods_id']=$goods_id;
  785. $option_value['option_id']=(int)$goods_option['option_id'];
  786. $option_value['image']='';
  787. $option_value['option_value_id']=(int)$goods_option_value['option_value_id'];
  788. $option_value['quantity']=0;
  789. $option_value['subtract']='';
  790. $option_value['price']=0 ;
  791. $option_value['price_prefix']=0;
  792. $option_value['weight']=0;
  793. $option_value['weight_prefix']=0;
  794. M('goods_option_value')->add($option_value);
  795. }
  796. }
  797. }
  798. }
  799. $new_quantity = 0;
  800. $min_dan_price = $data['danprice'];
  801. if(isset($data['mult_option_zuhe']) && !empty($data['mult_option_zuhe']))
  802. {
  803. if(!empty($data['mult_option_zuhe']))
  804. {
  805. $mult_option_arr = explode(',', $data['mult_option_zuhe']);
  806. $option_value_id_arr = array();
  807. foreach($mult_option_arr as $mult_option)
  808. {
  809. $dan_option = explode('@@',$mult_option);
  810. $mult_id_arr = explode(':',$dan_option[0]);
  811. $mult_quantity_arr = explode(':',$dan_option[1]);
  812. $mult_image_arr = explode(':',$dan_option[2]);
  813. $mult_price_arr = explode(':',$dan_option[3]);
  814. $mult_weight_arr = explode(':',$dan_option[4]);
  815. $tmp_option_value_id = explode('_',$mult_id_arr[1]);
  816. foreach($tmp_option_value_id as $vv)
  817. {
  818. if(empty($option_value_id_arr) || !isset($option_value_id_arr[$vv]))
  819. {
  820. $option_value_id_arr[$vv] = $mult_quantity_arr[1];
  821. } else{
  822. $option_value_id_arr[$vv] += $mult_quantity_arr[1];
  823. }
  824. }
  825. $mul_option_data = array();
  826. $mul_option_data['rela_goodsoption_valueid'] = $mult_id_arr[1];
  827. $mul_option_data['goods_id'] = $goods_id;
  828. if($min_dan_price > $mult_price_arr[1] )
  829. {
  830. $min_dan_price = $mult_price_arr[1];
  831. }
  832. $mul_option_data['dan_price'] = $mult_price_arr[1];
  833. $mul_option_data['quantity'] = $mult_quantity_arr[1];
  834. $mul_option_data['weight'] = $mult_weight_arr[1];
  835. $mul_option_data['image'] = $mult_image_arr[1];
  836. M('goods_option_mult_value')->add($mul_option_data);
  837. $new_quantity += $mul_option_data['quantity'];
  838. }
  839. if($data['danprice'] > $min_dan_price)
  840. {
  841. M('goods')->where( array('goods_id' => $goods_id) )->save( array('danprice' => $min_dan_price) );
  842. }
  843. if(!empty($option_value_id_arr))
  844. {
  845. foreach($option_value_id_arr as $key => $id_quantity)
  846. {
  847. M('goods_option_value')->where( array('goods_id' => $goods_id,'option_value_id' =>$key) )->save( array('quantity' => $id_quantity) );
  848. }
  849. }
  850. if($new_quantity != $quantity)
  851. {
  852. //更新库存,以规格库存为依据
  853. M('goods')->where( array('goods_id' => $goods_id) )->save( array('quantity' => $new_quantity) );
  854. }
  855. }
  856. }
  857. return array(
  858. 'status'=>'success',
  859. 'message'=>'新增成功',
  860. 'jump'=>U('Goods/index')
  861. );
  862. }catch(Exception $e){
  863. return array(
  864. 'status'=>'fail',
  865. 'message'=>'新增失败',
  866. 'jump'=>U('Goods/index')
  867. );
  868. }
  869. }else{
  870. return array(
  871. 'status'=>'fail',
  872. 'message'=>'新增失败',
  873. 'jump'=>U('Goods/index')
  874. );
  875. }
  876. }
  877. function get_goods_options($goods_id) {
  878. $goods_option_data = array();
  879. $goods_option_query = M()->query("SELECT * FROM " . C('DB_PREFIX') . "goods_option po LEFT JOIN "
  880. . C('DB_PREFIX') . "option o ON po.option_id = o.option_id WHERE po.goods_id =".(int)$goods_id);
  881. foreach ($goods_option_query as $goods_option) {
  882. $goods_option_value_data = array();
  883. $goods_option_value_query = M()->query("SELECT * FROM " . C('DB_PREFIX')
  884. . "goods_option_value WHERE goods_option_id = '"
  885. . (int)$goods_option['goods_option_id'] . "'");
  886. foreach ($goods_option_value_query as $goods_option_value) {
  887. $goods_option_value_data[] = array(
  888. 'goods_option_value_id' => $goods_option_value['goods_option_value_id'],
  889. 'option_value_id' => $goods_option_value['option_value_id'],
  890. 'quantity' => $goods_option_value['quantity'],
  891. 'subtract' => $goods_option_value['subtract'],
  892. 'price' => $goods_option_value['price'],
  893. 'price_prefix' => $goods_option_value['price_prefix'],
  894. 'image' => $goods_option_value['image'],
  895. 'weight' => $goods_option_value['weight'],
  896. 'weight_prefix' => $goods_option_value['weight_prefix']
  897. );
  898. }
  899. $goods_option_data[] = array(
  900. 'goods_option_id' => $goods_option['goods_option_id'],
  901. 'option_id' => $goods_option['option_id'],
  902. 'name' => $goods_option['name'],
  903. 'type' => $goods_option['type'],
  904. 'option_value' => $goods_option['name'],
  905. 'required' => $goods_option['required'],
  906. 'goods_option_value' => $goods_option_value_data,
  907. );
  908. }
  909. return $goods_option_data;
  910. }
  911. }
  912. ?>