Pointorder.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. * DSMall多用户商城
  7. * ============================================================================
  8. * 版权所有 2014-2028 长沙德尚网络科技有限公司,并保留所有权利。
  9. * 网站地址: http://www.csdeshang.com
  10. * ----------------------------------------------------------------------------
  11. * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
  12. * 不允许对程序代码以任何形式任何目的的再发布。
  13. * ============================================================================
  14. * 数据层模型
  15. */
  16. class Pointorder extends BaseModel {
  17. public $page_info;
  18. /**
  19. * 通过状态标识获取兑换订单状态
  20. * @access public
  21. * @author csdeshang
  22. * @return int
  23. */
  24. public function getPointorderStateBySign() {
  25. $pointorderstate_arr = array();
  26. $pointorderstate_arr['canceled'] = array(
  27. 2,
  28. '已取消'
  29. );
  30. $pointorderstate_arr['waitship'] = array(
  31. 20,
  32. '待发货'
  33. );
  34. $pointorderstate_arr['waitreceiving'] = array(
  35. 30,
  36. '待收货'
  37. );
  38. $pointorderstate_arr['finished'] = array(
  39. 40,
  40. '已完成'
  41. );
  42. return $pointorderstate_arr;
  43. }
  44. /**
  45. * 通过状态值获取兑换订单状态
  46. * @access public
  47. * @author csdeshang
  48. * @param type $order_state 订单状态
  49. * @return string
  50. */
  51. public function getPointorderState($order_state) {
  52. $pointorderstate_arr = array();
  53. $pointorderstate_arr[2] = array(
  54. 'canceled',
  55. '已取消'
  56. );
  57. $pointorderstate_arr[20] = array(
  58. 'waitship',
  59. '待发货'
  60. );
  61. $pointorderstate_arr[30] = array(
  62. 'waitreceiving',
  63. '待收货'
  64. );
  65. $pointorderstate_arr[40] = array(
  66. 'finished',
  67. '已完成'
  68. );
  69. if ($pointorderstate_arr[$order_state]) {
  70. return $pointorderstate_arr[$order_state];
  71. } else {
  72. return array(
  73. 'unknown',
  74. '未知'
  75. );
  76. }
  77. }
  78. /**
  79. * 新增兑换礼品订单
  80. * @access public
  81. * @author csdeshang
  82. * @param array $data 参数内容
  83. * @return bool
  84. */
  85. public function addPointorder($data) {
  86. if (!$data) {
  87. return false;
  88. }
  89. $result = Db::name('pointsorder')->insertGetId($data);
  90. //清除相关缓存
  91. if ($result && $data['point_buyerid'] > 0) {
  92. wcache($data['point_buyerid'], array('pointordercount' => null), 'm_pointorder');
  93. }
  94. return $result;
  95. }
  96. /**
  97. * 批量添加订单积分礼品
  98. * @access public
  99. * @author csdeshang
  100. * @param array $data 订单礼品信息
  101. * @return bool
  102. */
  103. public function addPointorderProdAll($data) {
  104. if (!$data) {
  105. return false;
  106. }
  107. $result = Db::name('pointsordergoods')->insertAll($data);
  108. return $result;
  109. }
  110. /**
  111. * 查询兑换订单礼品列表
  112. * @access public
  113. * @author csdeshang
  114. * @param type $where 查询条件
  115. * @param type $field 查询字段
  116. * @return type
  117. */
  118. public function getPointordergoodsList($where, $field = '*') {
  119. $ordergoods_list = Db::name('pointsordergoods')->field($field)->where($where)->select()->toArray();
  120. if ($ordergoods_list) {
  121. foreach ($ordergoods_list as $k => $v) {
  122. $v['point_goodsimage_old'] = $v['pointog_goodsimage'];
  123. $v['point_goodsimage_small'] = pointprod_thumb($v['pointog_goodsimage'], 'small');
  124. $v['pointog_goodsimage'] = pointprod_thumb($v['pointog_goodsimage'], 'mid');
  125. $ordergoods_list[$k] = $v;
  126. }
  127. }
  128. return $ordergoods_list;
  129. }
  130. /**
  131. * 删除兑换订单信息
  132. * @access public
  133. * @author csdeshang
  134. * @param array $where 查询条件
  135. * @return bool
  136. */
  137. public function delPointorder($where) {
  138. return Db::name('pointsorder')->where($where)->delete();
  139. }
  140. /**
  141. * 删除兑换订单礼品信息
  142. * @access public
  143. * @author csdeshang
  144. * @param array $where 查询条件
  145. * @return bool
  146. */
  147. public function delPointordergoods($where) {
  148. return Db::name('pointsordergoods')->where($where)->delete();
  149. }
  150. /**
  151. * 删除兑换订单地址信息
  152. * @access public
  153. * @author csdeshang
  154. * @param array $where 查询条件
  155. * @return bool
  156. */
  157. public function delPointorderAddress($where) {
  158. return Db::name('pointsorderaddress')->where($where)->delete();
  159. }
  160. /**
  161. * 添加积分兑换订单地址
  162. * @access public
  163. * @author csdeshang
  164. * @param array $data 订单收货地址信息
  165. * @return bool
  166. */
  167. public function addPointorderAddress($data) {
  168. if (!$data) {
  169. return false;
  170. }
  171. return Db::name('pointsorderaddress')->insertGetId($data);
  172. }
  173. /**
  174. * 查询兑换订单详情
  175. * @access public
  176. * @author csdeshang
  177. * @param array $where 查询条件
  178. * @param string $field 查询字段
  179. * @param string $order 排序
  180. * @param string $group 分组
  181. * @return array
  182. */
  183. public function getPointorderInfo($where, $field = '*', $order = '', $group = '') {
  184. $order_info = Db::name('pointsorder')->field($field)->where($where)->group($group)->order($order)->find();
  185. if(!$order_info){
  186. return $order_info;
  187. }
  188. $point_orderstate_arr = $this->getPointorderState($order_info['point_orderstate']);
  189. $order_info['point_orderstatetext'] = $point_orderstate_arr[1];
  190. $order_info['point_orderstatesign'] = $point_orderstate_arr[0];
  191. //是否可以发货
  192. $tmp = $this->allowPointorderShip($order_info);
  193. $order_info['point_orderallowship'] = $tmp['state'];
  194. unset($tmp);
  195. //是否可以修改发货信息
  196. $tmp = $this->allowEditPointorderShip($order_info);
  197. $order_info['point_orderalloweditship'] = $tmp['state'];
  198. unset($tmp);
  199. //是否允许取消
  200. $tmp = $this->allowPointorderCancel($order_info);
  201. $order_info['point_orderallowcancel'] = $tmp['state'];
  202. unset($tmp);
  203. //是否允许删除
  204. $tmp = $this->allowPointorderDelete($order_info);
  205. $order_info['point_orderallowdelete'] = $tmp['state'];
  206. unset($tmp);
  207. //允许确认收货
  208. $tmp = $this->allowPointorderReceiving($order_info);
  209. $order_info['point_orderallowreceiving'] = $tmp['state'];
  210. unset($tmp);
  211. return $order_info;
  212. }
  213. /**
  214. * 查询兑换订单收货人地址信息
  215. * @access public
  216. * @author csdeshang
  217. * @param type $where 条件
  218. * @param type $field 字段
  219. * @param type $order 排序
  220. * @param type $group 分组
  221. * @return type
  222. */
  223. public function getPointorderAddressInfo($where, $field = '*', $order = '', $group = '') {
  224. return Db::name('pointsorderaddress')->field($field)->where($where)->group($group)->order($order)->find();
  225. }
  226. /**
  227. * 查询兑换订单列表
  228. * @access public
  229. * @author csdeshang
  230. * @param array $where 条件
  231. * @param str $field 字段
  232. * @param int $pagesize 分页
  233. * @param int $limit 限制
  234. * @param str $order 排序
  235. * @return array
  236. */
  237. public function getPointorderList($where, $field = '*', $pagesize = 0, $limit = 0, $order = '') {
  238. if($pagesize){
  239. $order_list = Db::name('pointsorder')->field($field)->where($where)->order($order)->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
  240. $this->page_info = $order_list;
  241. $order_list = $order_list->items();
  242. }else{
  243. $order_list = Db::name('pointsorder')->field($field)->where($where)->limit($limit)->order($order)->select()->toArray();
  244. }
  245. foreach ($order_list as $k => $v) {
  246. //订单状态
  247. $point_orderstate_arr = $this->getPointorderState($v['point_orderstate']);
  248. $v['point_orderstatetext'] = $point_orderstate_arr[1];
  249. $v['point_orderstatesign'] = $point_orderstate_arr[0];
  250. //是否可以发货
  251. $tmp = $this->allowPointorderShip($v);
  252. $v['point_orderallowship'] = $tmp['state'];
  253. unset($tmp);
  254. //是否可以修改发货信息
  255. $tmp = $this->allowEditPointorderShip($v);
  256. $v['point_orderalloweditship'] = $tmp['state'];
  257. unset($tmp);
  258. //是否允许取消
  259. $tmp = $this->allowPointorderCancel($v);
  260. $v['point_orderallowcancel'] = $tmp['state'];
  261. unset($tmp);
  262. //是否允许删除
  263. $tmp = $this->allowPointorderDelete($v);
  264. $v['point_orderallowdelete'] = $tmp['state'];
  265. unset($tmp);
  266. //允许确认收货
  267. $tmp = $this->allowPointorderReceiving($v);
  268. $v['point_orderallowreceiving'] = $tmp['state'];
  269. unset($tmp);
  270. $order_list[$k] = $v;
  271. }
  272. return $order_list;
  273. }
  274. /**
  275. * 是否可以发货
  276. * @access public
  277. * @author csdeshang
  278. * @param array $pointorder_info 兑换订单详情
  279. * @return array
  280. */
  281. public function allowPointorderShip($pointorder_info) {
  282. if (!$pointorder_info) {
  283. return array(
  284. 'state' => false,
  285. 'msg' => '参数错误'
  286. );
  287. }
  288. $pointorderstate_arr = $this->getPointorderStateBySign();
  289. if ($pointorder_info['point_orderstate'] == $pointorderstate_arr['waitship'][0]) {
  290. return array('state' => true);
  291. } else {
  292. return array('state' => false);
  293. }
  294. }
  295. /**
  296. * 是否可以修改发货信息
  297. * @access public
  298. * @author csdeshang
  299. * @param array $pointorder_info 兑换订单详情
  300. * @return bool
  301. */
  302. public function allowEditPointorderShip($pointorder_info) {
  303. if (!$pointorder_info) {
  304. return array(
  305. 'state' => false,
  306. 'msg' => '参数错误'
  307. );
  308. }
  309. $pointorderstate_arr = $this->getPointorderStateBySign();
  310. if ($pointorder_info['point_orderstate'] == $pointorderstate_arr['waitreceiving'][0]) {
  311. return array('state' => true);
  312. } else {
  313. return array('state' => false);
  314. }
  315. }
  316. /**
  317. * 是否可以确认收货
  318. * @access public
  319. * @author csdeshang
  320. * @param array $pointorder_info 兑换订单详情
  321. * @return bool
  322. */
  323. public function allowPointorderReceiving($pointorder_info) {
  324. if (!$pointorder_info) {
  325. return array(
  326. 'state' => false,
  327. 'msg' => '参数错误'
  328. );
  329. }
  330. $pointorderstate_arr = $this->getPointorderStateBySign();
  331. if ($pointorder_info['point_orderstate'] == $pointorderstate_arr['waitreceiving'][0]) {
  332. return array('state' => true);
  333. } else {
  334. return array('state' => false);
  335. }
  336. }
  337. /**
  338. * 是否可以发货
  339. * @access public
  340. * @author csdeshang
  341. * @param array $pointorder_info 兑换订单详情
  342. * @return bool
  343. */
  344. public function allowPointorderCancel($pointorder_info) {
  345. if (!$pointorder_info) {
  346. return array(
  347. 'state' => false,
  348. 'msg' => '参数错误'
  349. );
  350. }
  351. $pointorderstate_arr = $this->getPointorderStateBySign();
  352. if ($pointorder_info['point_orderstate'] == $pointorderstate_arr['waitship'][0]) {
  353. return array('state' => true);
  354. } else {
  355. return array('state' => false);
  356. }
  357. }
  358. /**
  359. * 是否可以发货
  360. * @access public
  361. * @author csdeshang
  362. * @param array $pointorder_info 兑换订单详情
  363. * @return array
  364. */
  365. public function allowPointorderDelete($pointorder_info) {
  366. if (!$pointorder_info) {
  367. return array(
  368. 'state' => false,
  369. 'msg' => '参数错误'
  370. );
  371. }
  372. $pointorderstate_arr = $this->getPointorderStateBySign();
  373. if ($pointorder_info['point_orderstate'] == $pointorderstate_arr['canceled'][0]) {
  374. return array('state' => true);
  375. } else {
  376. return array('state' => false);
  377. }
  378. }
  379. /**
  380. * 积分礼品兑换订单信息修改
  381. * @access public
  382. * @author csdeshang
  383. * @param array $data 修改信息数组
  384. * @param array $condition 条件
  385. * @return bool
  386. */
  387. public function editPointorder($condition, $data) {
  388. if (empty($data)) {
  389. return false;
  390. }
  391. return Db::name('pointsorder')->where($condition)->update($data);
  392. }
  393. /**
  394. * 取消兑换商品订单
  395. * @access public
  396. * @author csdeshang
  397. * @param type $point_orderid 积分订单ID
  398. * @param type $member_id 会员ID
  399. * @return type
  400. */
  401. public function cancelPointorder($point_orderid, $member_id = 0) {
  402. $point_orderid = intval($point_orderid);
  403. if ($point_orderid <= 0) {
  404. return array(
  405. 'state' => false,
  406. 'msg' => '参数错误'
  407. );
  408. }
  409. //获取订单状态
  410. $pointorderstate_arr = $this->getPointorderStateBySign();
  411. $where = array();
  412. $where[] = array('point_orderid','=',$point_orderid);
  413. if ($member_id > 0) {
  414. $where[] = array('point_buyerid','=',$member_id);
  415. }
  416. $where[] = array('point_orderstate','=',$pointorderstate_arr['waitship'][0]); //未发货时可取消
  417. //查询兑换信息
  418. $order_info = $this->getPointorderInfo($where, 'point_orderid,point_ordersn,point_buyerid,point_buyername,point_allpoint,point_orderstate');
  419. if (!$order_info) {
  420. return array(
  421. 'state' => false,
  422. 'msg' => '兑换订单信息错误'
  423. );
  424. }
  425. $result = $this->editPointorder(array(
  426. 'point_orderid' => $order_info['point_orderid'],
  427. 'point_buyerid' => $order_info['point_buyerid']
  428. ), array('point_orderstate' => $pointorderstate_arr['canceled'][0]));
  429. if ($result) {
  430. //更新会员缓存
  431. wcache($order_info['point_buyerid'], array('pointordercount' => null), 'm_pointorder');
  432. //退还会员积分
  433. $insert_arr = array();
  434. $insert_arr['pl_memberid'] = $order_info['point_buyerid'];
  435. $insert_arr['pl_membername'] = $order_info['point_buyername'];
  436. $insert_arr['pl_points'] = $order_info['point_allpoint'];
  437. $insert_arr['point_ordersn'] = $order_info['point_ordersn'];
  438. $insert_arr['pl_desc'] = lang('member_pointorder_cancel_tip1') . $order_info['point_ordersn'] . lang('member_pointorder_cancel_tip2');
  439. model('points')->savePointslog('pointorder', $insert_arr, true);
  440. //更改兑换礼品库存
  441. $prod_list = $this->getPointorderAndGoodsList(array('pointsordergoods.pointog_orderid' => $order_info['point_orderid']), 'pointog_goodsid,pointog_goodsnum');
  442. if ($prod_list) {
  443. $pointprod_model = model('pointprod');
  444. foreach ($prod_list as $v) {
  445. $update_arr = array();
  446. $update_arr['pgoods_storage'] = Db::raw('pgoods_storage+'.$v['pointog_goodsnum']);
  447. $update_arr['pgoods_salenum'] = Db::raw('pgoods_salenum-'.$v['pointog_goodsnum']);
  448. $pointprod_model->editPointProd($update_arr, array('pgoods_id' => $v['pointog_goodsid']));
  449. unset($update_arr);
  450. }
  451. }
  452. return array(
  453. 'state' => true,
  454. 'data' => array('order_info' => $order_info)
  455. );
  456. } else {
  457. return array(
  458. 'state' => true,
  459. 'msg' => '取消失败'
  460. );
  461. }
  462. }
  463. /**
  464. * 兑换订单确认收货
  465. * @access public
  466. * @author csdeshang
  467. * @param type $point_orderid 积分订单ID
  468. * @param type $member_id 会员ID
  469. * @return type
  470. */
  471. public function receivingPointorder($point_orderid, $member_id = 0) {
  472. $point_orderid = intval($point_orderid);
  473. if ($point_orderid <= 0) {
  474. return array(
  475. 'state' => false,
  476. 'msg' => '参数错误'
  477. );
  478. }
  479. //获取订单状态
  480. $pointorderstate_arr = $this->getPointorderStateBySign();
  481. $where = array();
  482. $where[] = array('point_orderid','=',$point_orderid);
  483. if ($member_id > 0) {
  484. $where[] = array('point_buyerid','=',$member_id);
  485. }
  486. $where[] = array('point_orderstate','=',$pointorderstate_arr['waitreceiving'][0]); //已发货待收货
  487. //更新运费
  488. $result = $this->editPointorder($where, array(
  489. 'point_orderstate' => $pointorderstate_arr['finished'][0],
  490. 'point_finnshedtime' => TIMESTAMP
  491. ));
  492. if ($result) {
  493. return array('state' => true);
  494. } else {
  495. return array(
  496. 'state' => true,
  497. 'msg' => '确认收货失败'
  498. );
  499. }
  500. }
  501. /**
  502. * 查询兑换订单总数
  503. * @access public
  504. * @author csdeshang
  505. * @param array $condition 条件
  506. * @return int
  507. */
  508. public function getPointorderCount($condition) {
  509. return Db::name('pointsorder')->where($condition)->count();
  510. }
  511. /**
  512. * 查询积分兑换商品订单及商品列表
  513. * @access public
  514. * @author csdeshang
  515. * @param array $where 条件
  516. * @param string $field 字段
  517. * @param string $order 排序
  518. * @return array
  519. */
  520. public function getPointorderAndGoodsList($where, $field = '*', $order = '',$pagesize='') {
  521. if($pagesize){
  522. $order_list = Db::name('pointsordergoods')->alias('pointsordergoods')->field($field)->join('pointsorder pointsorder','pointsordergoods.pointog_orderid=pointsorder.point_orderid')->where($where)->order($order)->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
  523. $this->page_info = $order_list;
  524. $order_list = $order_list->items();
  525. return $order_list;
  526. }else{
  527. return Db::name('pointsordergoods')->alias('pointsordergoods')->field($field)->join('pointsorder pointsorder','pointsordergoods.pointog_orderid=pointsorder.point_orderid')->where($where)->order($order)->select()->toArray();
  528. }
  529. }
  530. /**
  531. * 查询积分兑换商品订单及商品详细
  532. * @access public
  533. * @author csdeshang
  534. * @param array $where 条件
  535. * @param str $field 字段
  536. * @param str $order 排序
  537. * @param str $group 分组
  538. * @return array
  539. */
  540. public function getPointorderAndGoodsInfo($where, $field = '*', $order = '', $group = '') {
  541. return Db::name('pointsordergoods')->alias('pointsordergoods')->field($field)->join('pointsorder pointsorder','pointsordergoods.pointog_orderid=pointsorder.point_orderid')->where($where)->group($group)->order($order)->find();
  542. }
  543. /**
  544. * 查询会员已经兑换商品数
  545. * @access public
  546. * @author csdeshang
  547. * @param int $member_id 会员编号
  548. * @return int
  549. */
  550. public function getMemberPointsOrderGoodsCount($member_id) {
  551. $info = rcache($member_id, 'm_pointorder');
  552. if (!isset($info['pointordercount']) || $info['pointordercount'] !== 0) {
  553. //获取兑换订单状态
  554. $pointorderstate_arr = $this->getPointorderStateBySign();
  555. $where = array();
  556. $where[] = array('point_buyerid','=',$member_id);
  557. $where[] = array('point_orderstate','<>',$pointorderstate_arr['canceled'][0]);
  558. $list = $this->getPointorderAndGoodsList($where, 'SUM(pointog_goodsnum) as goodsnum');
  559. $pointordercount = 0;
  560. if ($list) {
  561. $pointordercount = intval($list[0]['goodsnum']);
  562. }
  563. wcache($member_id, array('pointordercount' => $pointordercount), 'm_pointorder');
  564. } else {
  565. $pointordercount = intval($info['pointordercount']);
  566. }
  567. return $pointordercount;
  568. }
  569. /**
  570. * 创建积分礼品兑换订单
  571. * @access public
  572. * @author csdeshang
  573. * @param type $post_arr 数据数组
  574. * @param type $pointprod_arr 积分数组
  575. * @param type $member_info 会员信息
  576. * @return type
  577. */
  578. public function createOrder($post_arr, $pointprod_arr, $member_info) {
  579. //验证是否存在收货地址
  580. $address_options = intval($post_arr['address_options']);
  581. if ($address_options <= 0) {
  582. return array(
  583. 'state' => false,
  584. 'msg' => '请选择收货人地址'
  585. );
  586. }
  587. $address_info = model('address')->getOneAddress($address_options);
  588. if (empty($address_info) || $member_info['member_id']!=$address_info['member_id']) {
  589. return array(
  590. 'state' => false,
  591. 'msg' => '收货人地址信息错误'
  592. );
  593. }
  594. //获取兑换订单状态
  595. $pointorderstate_arr = $this->getPointorderStateBySign();
  596. //新增兑换订单
  597. $order_array = array();
  598. $order_array['point_ordersn'] = makePaySn($member_info['member_id']);
  599. $order_array['point_buyerid'] = $member_info['member_id'];
  600. $order_array['point_buyername'] = $member_info['member_name'];
  601. $order_array['point_buyeremail'] = $member_info['member_email'];
  602. $order_array['point_addtime'] = TIMESTAMP;
  603. $order_array['point_allpoint'] = $pointprod_arr['pgoods_pointall'];
  604. $order_array['point_ordermessage'] = isset($post_arr['pcart_message'])?trim($post_arr['pcart_message']):'';
  605. $order_array['point_orderstate'] = $pointorderstate_arr['waitship'][0];
  606. $order_id = $this->addPointorder($order_array);
  607. if (!$order_id) {
  608. return array(
  609. 'state' => false,
  610. 'msg' => '兑换操作失败'
  611. );
  612. }
  613. //扣除会员积分
  614. $insert_arr = array();
  615. $insert_arr['pl_memberid'] = $member_info['member_id'];
  616. $insert_arr['pl_membername'] = $member_info['member_name'];
  617. $insert_arr['pl_points'] = -$pointprod_arr['pgoods_pointall'];
  618. $insert_arr['point_ordersn'] = $order_array['point_ordersn'];
  619. model('points')->savePointslog('pointorder', $insert_arr, true);
  620. //添加订单中的礼品信息
  621. $pointprod_model = model('pointprod');
  622. if ($pointprod_arr['pointprod_list']) {
  623. $output_goods_name = array(); //输出显示的对话礼品名称数组
  624. foreach ($pointprod_arr['pointprod_list'] as $v) {
  625. $tmp = array();
  626. $tmp['pointog_orderid'] = $order_id;
  627. $tmp['pointog_goodsid'] = $v['pgoods_id'];
  628. $tmp['pointog_goodsname'] = $v['pgoods_name'];
  629. $tmp['pointog_goodspoints'] = $v['pgoods_points'];
  630. $tmp['pointog_goodsnum'] = $v['quantity'];
  631. $tmp['pointog_goodsimage'] = $v['pgoods_image_old'];
  632. $order_goods_array[] = $tmp;
  633. //输出显示前3个兑换礼品名称
  634. if (count($output_goods_name) < 3) {
  635. $output_goods_name[] = $v['pgoods_name'];
  636. }
  637. unset($tmp);
  638. //更新积分礼品库存
  639. $tmp = array();
  640. $tmp['pgoods_id'] = $v['pgoods_id'];
  641. $tmp['pgoods_storage'] = Db::raw('pgoods_storage-'.$v['quantity']);
  642. $tmp['pgoods_salenum'] = Db::raw('pgoods_salenum+'.$v['quantity']);
  643. $pointprod_uparr[] = $tmp;
  644. unset($tmp);
  645. }
  646. //批量新增兑换订单礼品
  647. $this->addPointorderProdAll($order_goods_array);
  648. //更新兑换礼品库存
  649. foreach ($pointprod_uparr as $v) {
  650. $pointprod_model->editPointProd($v, array('pgoods_id' => $v['pgoods_id']));
  651. }
  652. }
  653. //清除购物车信息
  654. model('pointcart')->delPointcart(array('pmember_id' => $member_info['member_id']), $member_info['member_id']);
  655. //保存买家收货地址,添加订单收货地址
  656. if ($address_info) {
  657. $address_array = array();
  658. $address_array['pointoa_orderid'] = $order_id;
  659. $address_array['pointoa_truename'] = $address_info['address_realname'];
  660. $address_array['pointoa_areaid'] = $address_info['area_id'];
  661. $address_array['pointoa_areainfo'] = $address_info['area_info'];
  662. $address_array['pointoa_address'] = $address_info['address_detail'];
  663. $address_array['pointoa_zipcode'] = (isset($address_info['zip_code'])) ? $address_info['zip_code'] : '';
  664. $address_array['pointoa_telphone'] = $address_info['address_tel_phone'];
  665. $address_array['pointoa_mobphone'] = $address_info['address_mob_phone'];
  666. $this->addPointorderAddress($address_array);
  667. }
  668. return array(
  669. 'state' => true,
  670. 'data' => array('order_id' => $order_id)
  671. );
  672. }
  673. /**
  674. * 删除兑换订单
  675. * @access public
  676. * @author csdeshang
  677. * @param type $order_id 订单ID
  678. * @return type
  679. */
  680. public function delPointorderByOrderID($order_id) {
  681. $order_id = intval($order_id);
  682. if ($order_id <= 0) {
  683. return array(
  684. 'state' => false,
  685. 'msg' => '参数错误'
  686. );
  687. }
  688. //获取订单状态
  689. $pointorderstate_arr = $this->getPointorderStateBySign();
  690. //删除操作
  691. $where = array();
  692. $where[] = array('point_orderid','=',$order_id);
  693. $where[] = array('point_orderstate','=',$pointorderstate_arr['canceled'][0]); //只有取消的订单才能删除
  694. $result = $this->delPointorder($where);
  695. if ($result) {
  696. //删除兑换礼品信息
  697. $this->delPointordergoods(array('pointog_orderid' => $order_id));
  698. //删除兑换地址信息
  699. $this->delPointorderAddress(array('pointoa_orderid' => $order_id));
  700. return array('state' => true);
  701. } else {
  702. return array(
  703. 'state' => false,
  704. 'msg' => '删除失败'
  705. );
  706. }
  707. }
  708. /**
  709. * 兑换订单发货
  710. * @access public
  711. * @author csdeshang
  712. * @param type $order_id 订单id
  713. * @param type $postarr 数组
  714. * @param type $order_info 会员
  715. * @return type
  716. */
  717. public function shippingPointorder($order_id, $postarr, $order_info = array()) {
  718. $order_id = intval($order_id);
  719. if ($order_id <= 0) {
  720. return array(
  721. 'state' => false,
  722. 'msg' => '参数错误'
  723. );
  724. }
  725. //获取订单状态
  726. $pointorderstate_arr = $this->getPointorderStateBySign();
  727. //查询订单信息
  728. $where = array();
  729. $where[] = array('point_orderid','=',$order_id);
  730. $where[] = array(
  731. 'point_orderstate', 'in',
  732. array(
  733. $pointorderstate_arr['waitship'][0],
  734. $pointorderstate_arr['waitreceiving'][0]
  735. )
  736. );
  737. //待发货和已经发货状态
  738. //如果订单详情不存在,则查询
  739. if (!$order_info) {
  740. $order_info = $this->getPointorderInfo($where, 'point_orderstate');
  741. }
  742. if (!$order_info) {
  743. return array(
  744. 'state' => false,
  745. 'msg' => '参数错误'
  746. );
  747. }
  748. //更新发货信息
  749. $update_arr = array();
  750. if ($order_info['point_orderstate'] == $pointorderstate_arr['waitship'][0]) {
  751. $update_arr['point_shippingtime'] = TIMESTAMP;
  752. $update_arr['point_orderstate'] = $pointorderstate_arr['waitreceiving'][0]; //已经发货,待收货
  753. }
  754. $update_arr['point_shippingcode'] = $postarr['shippingcode'];
  755. $update_arr['point_shipping_ecode'] = $postarr['express_code'];
  756. $result = $this->editPointorder($where, $update_arr);
  757. if ($result) {
  758. return array('state' => true);
  759. } else {
  760. return array(
  761. 'state' => false,
  762. 'msg' => '发货修改失败'
  763. );
  764. }
  765. }
  766. }