Pointorder.php 29 KB

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