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