Pointcart.php 19 KB

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