Pointcart.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * ============================================================================
  12. * 数据层模型
  13. */
  14. class Pointcart extends BaseModel
  15. {
  16. /**
  17. * 礼品购物车保存
  18. * @access public
  19. * @author csdeshang
  20. * @param type $data 数据
  21. * @return boolean
  22. */
  23. public function addPointcart($data)
  24. {
  25. if (empty($data)) {
  26. return false;
  27. }
  28. $result = Db::name('pointscart')->insertGetId($data);
  29. if ($result) {
  30. //清除相关缓存
  31. wcache($data['pmember_id'], array('pointcart_count' => null), 'm_pointcart');
  32. return $result;
  33. } else {
  34. return false;
  35. }
  36. }
  37. /**
  38. * 兑换礼品购物车列表
  39. * @access public
  40. * @author csdeshang
  41. * @param type $where 条件
  42. * @param type $field 字段
  43. * @return type
  44. */
  45. public function getPointcartList($where, $field = '*')
  46. {
  47. $cartgoods_list = Db::name('pointscart')->field($field)->where($where)->select()->toArray();
  48. if ($cartgoods_list) {
  49. foreach ($cartgoods_list as $k => $v) {
  50. $v['pgoods_image_old'] = $v['pgoods_image'];
  51. $v['pgoods_image_max'] = pointprod_thumb($v['pgoods_image']);
  52. $v['pgoods_image_small'] = pointprod_thumb($v['pgoods_image'], 'small');
  53. $v['pgoods_image'] = pointprod_thumb($v['pgoods_image'], 'normal');
  54. $cartgoods_list[$k] = $v;
  55. }
  56. }
  57. return $cartgoods_list;
  58. }
  59. /**
  60. * 查询兑换礼品购物车列表并计算总积分
  61. * @access public
  62. * @author csdeshang
  63. * @param type $where 条件
  64. * @param type $field 字段
  65. * @param type $pagesize 分页
  66. * @param type $limit 限制
  67. * @param type $order 排序
  68. * @param type $group 分组
  69. * @return type
  70. */
  71. public function getPCartListAndAmount($where, $field = '*', $pagesize = 0, $limit = 0, $order = '', $group = '')
  72. {
  73. $cartgoods_list = $this->getPointcartList($where);
  74. //兑换礼品总积分
  75. $cartgoods_pointall = 0;
  76. if (!empty($cartgoods_list) && is_array($cartgoods_list)) {
  77. foreach ($cartgoods_list as $k => $v) {
  78. $pointsgoods_info = Db::name('pointsgoods')->where('pgoods_id', $v['pgoods_id'])->find();
  79. $v['pgoods_storage'] = $pointsgoods_info['pgoods_storage'];
  80. $v['pgoods_pointone'] = intval($v['pgoods_points']) * intval($v['pgoods_choosenum']);
  81. $cartgoods_list[$k] = $v;
  82. $cartgoods_pointall = $cartgoods_pointall + $v['pgoods_pointone'];
  83. }
  84. }
  85. return array(
  86. 'state' => true,
  87. 'data' => array('cartgoods_list' => $cartgoods_list, 'cartgoods_pointall' => $cartgoods_pointall)
  88. );
  89. }
  90. /**
  91. * 礼品购物车信息单条
  92. * @access public
  93. * @author csdeshang
  94. * @param type $where 条件
  95. * @param type $field 字段
  96. * @param type $order 排序
  97. * @return type
  98. */
  99. public function getPointcartInfo($where, $field = '*', $order = '')
  100. {
  101. return Db::name('pointscart')->field($field)->where($where)->order($order)->find();
  102. }
  103. /**
  104. * 礼品购物车礼品数量
  105. * @access public
  106. * @author csdeshang
  107. * @param type $member_id 会员id
  108. * @return type
  109. */
  110. public function getPointcartCount($member_id)
  111. {
  112. $info = rcache($member_id, 'm_pointcart');
  113. if (!isset($info['pointcart_count']) || $info['pointcart_count'] == 0) {
  114. $pointcart_count = Db::name('pointscart')->where(array('pmember_id' => $member_id))->count();
  115. $pointcart_count = intval($pointcart_count);
  116. wcache($member_id, array('pointcart_count' => $pointcart_count), 'm_pointcart');
  117. } else {
  118. $pointcart_count = intval($info['pointcart_count']);
  119. }
  120. return $pointcart_count;
  121. }
  122. /**
  123. * 删除礼品购物车信息按照购物车编号
  124. * @access public
  125. * @author csdeshang
  126. * @param type $pc_id 礼品ID
  127. * @param type $member_id 会员ID
  128. * @return boolean
  129. */
  130. public function delPointcartById($pc_id, $member_id = 0)
  131. {
  132. if (empty($pc_id)) {
  133. return false;
  134. }
  135. $where = array();
  136. if (is_array($pc_id)) {
  137. $where[] = array('pcart_id', 'in', $pc_id);
  138. } else {
  139. $where[] = array('pcart_id', '=', $pc_id);
  140. }
  141. $result = Db::name('pointscart')->where($where)->delete();
  142. //清除相关缓存
  143. if ($result && $member_id > 0) {
  144. wcache($member_id, array('pointcart_count' => null), 'm_pointcart');
  145. }
  146. return $result;
  147. }
  148. /**
  149. * 删除特定条件礼品购物车信息
  150. * @access public
  151. * @author csdeshang
  152. * @param type $where 条件
  153. * @param type $member_id 会员id
  154. * @return type
  155. */
  156. public function delPointcart($where, $member_id = 0)
  157. {
  158. $result = Db::name('pointscart')->where($where)->delete();
  159. //清除相关缓存
  160. if ($result && $member_id > 0) {
  161. wcache($member_id, array('pointcart_count' => null), 'm_pointcart');
  162. }
  163. return $result;
  164. }
  165. /**
  166. * 积分礼品购物车信息修改
  167. * @access public
  168. * @author csdeshang
  169. * @param type $condition 条件数组
  170. * @param type $data 修改信息数组
  171. * @return boolean
  172. */
  173. public function editPointcart($condition, $data)
  174. {
  175. if (empty($data)) {
  176. return false;
  177. }
  178. $result = Db::name('pointscart')->where($condition)->update($data);
  179. return $result;
  180. }
  181. /**
  182. * 验证是否能兑换
  183. * @param type $pgoods_id 礼品ID
  184. * @param type $quantity 数量
  185. * @param type $member_id 会员ID
  186. * @return type
  187. */
  188. public function checkExchange($pgoods_id, $quantity, $member_id)
  189. {
  190. $pgoods_id = intval($pgoods_id);
  191. $quantity = intval($quantity);
  192. if ($pgoods_id <= 0 || $quantity <= 0) {
  193. return array('state' => false, 'error' => 'ParameterError', 'msg' => '参数错误');
  194. }
  195. $pointprod_model = model('pointprod');
  196. //获取兑换商品的展示状态
  197. $pgoodsshowstate_arr = $pointprod_model->getPgoodsShowState();
  198. //获取兑换商品的开启状态
  199. $pgoodsopenstate_arr = $pointprod_model->getPgoodsOpenState();
  200. //验证积分礼品是否存在
  201. $prod_info = $pointprod_model->getPointProdInfo(array(
  202. 'pgoods_id' => $pgoods_id,
  203. 'pgoods_show' => $pgoodsshowstate_arr['show'][0],
  204. 'pgoods_state' => $pgoodsopenstate_arr['open'][0]
  205. ));
  206. if (!$prod_info) {
  207. return array('state' => false, 'error' => 'ParameterError', 'msg' => '兑换礼品信息不存在');
  208. }
  209. //验证积分礼品兑换状态
  210. $ex_state = $pointprod_model->getPointProdExstate($prod_info);
  211. switch ($ex_state) {
  212. case 'willbe':
  213. return array('state' => false, 'error' => 'StateError', 'msg' => '该兑换礼品的兑换活动即将开始');
  214. break;
  215. case 'end':
  216. return array('state' => false, 'error' => 'StateError', 'msg' => '该兑换礼品的兑换活动已经结束');
  217. break;
  218. }
  219. //查询会员信息
  220. $member_model = model('member');
  221. $member_info = $member_model->getMemberInfoByID($member_id);
  222. //验证是否满足会员级别
  223. $member_info['member_grade'] = $member_model->getOneMemberGrade($member_info['member_exppoints']);
  224. if ($prod_info['pgoods_limitmgrade'] > 0 && $member_info['member_grade']['level'] < $prod_info['pgoods_limitmgrade']) {
  225. return array('state' => false, 'error' => 'MemberGradeError', 'msg' => '您还未达到兑换所需的会员级别,不能进行兑换');
  226. }
  227. //验证兑换数量是否合法
  228. $data = $this->checkPointProdExnum($prod_info, $quantity, $member_id);
  229. if (!$data['state']) {
  230. return array('state' => false, 'error' => 'StateError', 'msg' => $data['msg']);
  231. }
  232. $prod_info['quantity'] = $quantity;
  233. //计算消耗积分总数
  234. $prod_info['pointsamount'] = intval($prod_info['pgoods_points']) * intval($quantity);
  235. //验证积分数是否足够
  236. $data = $this->checkPointEnough($prod_info['pointsamount'], $member_id, $member_info);
  237. if (!$data['state']) {
  238. return array('state' => false, 'error' => 'PointsShortof', 'msg' => $data['msg']);
  239. }
  240. return array('state' => true, 'data' => array('prod_info' => $prod_info));
  241. }
  242. /**
  243. * 验证礼品兑换数量是否合法
  244. * @access public
  245. * @author csdeshang
  246. * @param array $prodinfo 礼品数组
  247. * @param int $quantity 兑换数量
  248. * @param array $member_id 会员编号
  249. * @return array 兑换数量是否合法及其错误数组
  250. */
  251. public function checkPointProdExnum($prodinfo, $quantity, $member_id)
  252. {
  253. $data = $this->getPointProdExnum($prodinfo, $quantity, $member_id);
  254. if (!$data['state']) {
  255. return array('state' => false, 'msg' => $data['msg']);
  256. }
  257. //如果兑换数量大于可兑换数量则提示错误
  258. if ($data['data']['quantity'] < $quantity) {
  259. return array('state' => false, 'msg' => "兑换礼品数量不能大于{$data['data']['quantity']}");
  260. }
  261. return array('state' => true);
  262. }
  263. /**
  264. * 获得礼品可兑换数量
  265. * @access public
  266. * @author csdeshang
  267. * @param array $prodinfo 礼品数组
  268. * @param int $quantity 兑换数量
  269. * @param array $member_id 会员编号
  270. * @return array 兑换数量及其错误数组
  271. */
  272. public function getPointProdExnum($prodinfo, $quantity, $member_id)
  273. {
  274. if ($quantity <= 0) {
  275. return array('state' => false, 'msg' => '兑换数量错误');
  276. }
  277. if ($prodinfo['pgoods_storage'] <= 0) {
  278. return array('state' => false, 'msg' => '该礼品已兑换完');
  279. }
  280. if ($prodinfo['pgoods_storage'] < $quantity) {
  281. //如果兑换数量大于库存,则兑换数量为库存数量
  282. $quantity = $prodinfo['pgoods_storage'];
  283. }
  284. if ($prodinfo['pgoods_islimit'] == 1 && $prodinfo['pgoods_limitnum'] < $quantity) {
  285. //如果兑换数量大于限兑数量,则兑换数量为限兑数量
  286. $quantity = $prodinfo['pgoods_limitnum'];
  287. }
  288. //查询已兑换数量,并获得剩余可兑换数量
  289. if ($prodinfo['pgoods_islimit'] == 1) {
  290. $pointorder_model = model('pointorder');
  291. //获取兑换订单状态
  292. $pointorderstate_arr = $pointorder_model->getPointorderStateBySign();
  293. $where = array();
  294. $where[] = array('pointog_goodsid', '=', $prodinfo['pgoods_id']);
  295. $where[] = array('point_buyerid', '=', $member_id);
  296. $where[] = array('point_orderstate', '<>', $pointorderstate_arr['canceled'][0]); //未取消
  297. $pordergoodsinfo = $pointorder_model->getPointorderAndGoodsInfo($where, "SUM(pointog_goodsnum) as exnum", '', 'pointog_goodsid');
  298. if ($pordergoodsinfo) {
  299. $ablenum = $prodinfo['pgoods_limitnum'] - intval($pordergoodsinfo['exnum']);
  300. if ($ablenum <= 0) {
  301. return array('state' => false, 'msg' => '您已达到该礼品的最大兑换数,不能再兑换,请兑换其他礼品');
  302. }
  303. if ($ablenum < $quantity) {
  304. $quantity = $ablenum;
  305. }
  306. }
  307. }
  308. return array('state' => true, 'data' => array('quantity' => $quantity));
  309. }
  310. /**
  311. * 获得某会员购物车礼品总积分
  312. * @access public
  313. * @author csdeshang
  314. * @param type $member_id 会员ID
  315. * @return type
  316. */
  317. public function getPointcartAmount($member_id)
  318. {
  319. if ($member_id <= 0) {
  320. return array('state' => false, 'msg' => '参数错误');
  321. }
  322. $info = $this->getPointcartInfo(array('pmember_id' => $member_id), 'SUM(pgoods_points*pgoods_choosenum) as amount');
  323. $amount = intval($info['amount']);
  324. return $amount;
  325. }
  326. /**
  327. * 获得符合条件的购物车商品列表同时计算运费、总积分数等信息,用于下单过程
  328. * @access public
  329. * @author csdeshang
  330. * @param type $member_id 会员id
  331. * @return type
  332. */
  333. public function getCartGoodsList($member_id, $post = array())
  334. {
  335. $return_array = array();
  336. //获取礼品购物车内信息
  337. if (!empty($post) && isset($post['ifcart']) && !$post['ifcart'] && isset($post['cart_id'])) {
  338. $temp = explode('|', $post['cart_id']);
  339. $data = $this->checkExchange($temp[0], $temp[1], $member_id);
  340. if (!$data['state']) {
  341. return array('state' => false, 'msg' => $data['msg']);
  342. }
  343. $prod_info = $data['data']['prod_info'];
  344. $temp = array();
  345. $temp['pcart_id'] = 0;
  346. $temp['pmember_id'] = $member_id;
  347. $temp['pgoods_id'] = $prod_info['pgoods_id'];
  348. $temp['pgoods_name'] = $prod_info['pgoods_name'];
  349. $temp['pgoods_points'] = $prod_info['pgoods_points'];
  350. $temp['pgoods_choosenum'] = $prod_info['quantity'];
  351. $temp['pgoods_image'] = $prod_info['pgoods_image_old'];
  352. $cartgoodslist_tmp = array($temp);
  353. } else {
  354. $cartgoodslist_tmp = $this->getPointcartList(array('pmember_id' => $member_id));
  355. }
  356. if (!$cartgoodslist_tmp) {
  357. return array('state' => false, 'msg' => '购物车信息错误');
  358. }
  359. $cartgoodslist = array();
  360. foreach ($cartgoodslist_tmp as $v) {
  361. $cartgoodslist[$v['pgoods_id']] = $v;
  362. }
  363. //购物车礼品ID数组
  364. $cartgoodsid_arr = array_keys($cartgoodslist);
  365. //查询积分礼品信息
  366. $pointprod_model = model('pointprod');
  367. $pointprod_list = $pointprod_model->getOnlinePointProdList(array(array('pgoods_id', 'in', $cartgoodsid_arr)));
  368. if (!$pointprod_list) {
  369. return array('state' => false, 'msg' => '购物车信息错误');
  370. }
  371. unset($cartgoodsid_arr);
  372. unset($cartgoodslist_tmp);
  373. $cart_delid_arr = array(); //应删除的购物车信息
  374. $pgoods_pointall = 0; //积分总数
  375. //查询会员信息
  376. $member_model = model('member');
  377. $member_info = $member_model->getMemberInfoByID($member_id);
  378. $member_info['member_grade'] = $member_model->getOneMemberGrade($member_info['member_exppoints']);
  379. //处理购物车礼品信息
  380. foreach ($pointprod_list as $k => $v) {
  381. //验证是否满足会员级别
  382. if ($v['pgoods_limitmgrade'] > 0 && $member_info['member_grade']['level'] < $v['pgoods_limitmgrade']) {
  383. $cart_delid_arr[] = $cartgoodslist[$v['pgoods_id']]['pcart_id'];
  384. unset($pointprod_list[$k]);
  385. break;
  386. }
  387. //验证积分礼品兑换状态
  388. $ex_state = $pointprod_model->getPointProdExstate($v);
  389. switch ($ex_state) {
  390. case 'going':
  391. //验证兑换数量是否合法
  392. $data = $this->getPointProdExnum($v, $cartgoodslist[$v['pgoods_id']]['pgoods_choosenum'], $member_id);
  393. if (!$data['state']) {
  394. //删除积分礼品兑换信息
  395. $cart_delid_arr[] = $cartgoodslist[$v['pgoods_id']]['pcart_id'];
  396. unset($pointprod_list[$k]);
  397. } else {
  398. $quantity = $data['data']['quantity'];
  399. $pointprod_list[$k]['quantity'] = $quantity;
  400. //计算单件礼品积分数
  401. $pointprod_list[$k]['onepoints'] = $quantity * intval($v['pgoods_points']);
  402. //计算所有礼品积分数
  403. $pgoods_pointall = $pgoods_pointall + $pointprod_list[$k]['onepoints'];
  404. }
  405. break;
  406. default:
  407. //删除积分礼品兑换信息
  408. $cart_delid_arr[] = $cartgoodslist[$v['pgoods_id']]['pcart_id'];
  409. unset($pointprod_list[$k]);
  410. break;
  411. }
  412. }
  413. //删除不符合条件的礼品购物车信息
  414. if (is_array($cart_delid_arr) && count($cart_delid_arr) > 0) {
  415. $this->delPointcartById($cart_delid_arr, $member_id);
  416. }
  417. if (!$pointprod_list) {
  418. return array('state' => false, 'msg' => '购物车信息错误');
  419. }
  420. return array(
  421. 'state' => true, 'data' => array('pointprod_list' => $pointprod_list, 'pgoods_pointall' => $pgoods_pointall)
  422. );
  423. }
  424. /**
  425. * 验证积分是否足够
  426. * @access public
  427. * @author csdeshang
  428. * @param $points int 所需积分
  429. * @param $member_id int 会员ID
  430. * @param $member_info array 会员详细信息,不必须
  431. * @return bool 积分是否足够
  432. */
  433. public function checkPointEnough($points, $member_id, $member_info = array())
  434. {
  435. $points = intval($points);
  436. if ($member_id <= 0) {
  437. return array('state' => false, 'msg' => '会员信息错误');
  438. }
  439. if (!$member_info) {
  440. $member_info = model('member')->getMemberInfoByID($member_id);
  441. }
  442. if (intval($member_info['member_points']) < $points) {
  443. return array('state' => false, 'msg' => '积分不足,暂时不能兑换');
  444. }
  445. return array('state' => true);
  446. }
  447. }