Pointorder.php 29 KB

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