Pointcart.php 19 KB

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