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