goods_combo.model.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. <?php
  2. /**
  3. * 商品推荐组合模型
  4. *
  5. */
  6. defined('InShopNC') or exit('Access Invalid!');
  7. class goods_comboModel extends Model {
  8. public function __construct(){
  9. parent::__construct('goods_combo');
  10. }
  11. /**
  12. * 插入数据
  13. *
  14. * @param unknown $insert
  15. * @return boolean
  16. */
  17. public function addGoodsComboAll($insert) {
  18. $result = $this->insertAll($insert);
  19. if ($result) {
  20. foreach ((array)$insert as $v) {
  21. if ($v['goods_id']) $this->_dGoodsComboCache($v['goods_id']);
  22. }
  23. $this->publish_message();
  24. }
  25. return $result;
  26. }
  27. /**
  28. * 查询组合商品列表
  29. * @param unknown $condition
  30. */
  31. public function getGoodsComboList($condition) {
  32. return $this->where($condition)->limit(false)->select();
  33. }
  34. /**
  35. * 删除推荐组合商品
  36. */
  37. public function delGoodsCombo($condition) {
  38. $list = $this->getGoodsComboList($condition, 'goods_id');
  39. if (empty($list)) {
  40. return true;
  41. }
  42. $result = $this->where($condition)->delete();
  43. if ($result) {
  44. foreach ($list as $v) {
  45. $this->_dGoodsComboCache($v['goods_id']);
  46. }
  47. $this->publish_message();
  48. }
  49. return $result;
  50. }
  51. public function getGoodsComboCacheByGoodsId($goods_id) {
  52. $array = $this->_rGoodsComboCache($goods_id);
  53. if (empty($array)) {
  54. $gcombo_list = array();
  55. $combo_list = $this->getGoodsComboList(array('goods_id' => $goods_id));
  56. if (!empty($combo_list)) {
  57. $comboid_array= array();
  58. foreach ($combo_list as $val) {
  59. $comboid_array[] = $val['combo_goodsid'];
  60. }
  61. $gcombo_list = Model('goods')->getGeneralGoodsList(array('goods_id' => array('in', $comboid_array)));
  62. }
  63. $array = array('gcombo_list' => serialize($gcombo_list));
  64. $this->_wGoodsComboCache($goods_id, $array);
  65. }
  66. return $array;
  67. }
  68. /**
  69. * 读取商品推荐搭配缓存
  70. * @param int $goods_id
  71. * @return array
  72. */
  73. private function _rGoodsComboCache($goods_id) {
  74. return rcache($goods_id, 'goods_combo');
  75. }
  76. /**
  77. * 写入商品推荐搭配缓存
  78. * @param int $goods_id
  79. * @param array $array
  80. * @return boolean
  81. */
  82. private function _wGoodsComboCache($goods_id, $array) {
  83. return wcache($goods_id, $array, 'goods_combo', 60);
  84. }
  85. /**
  86. * 删除商品推荐搭配缓存
  87. * @param int $goods_id
  88. * @return boolean
  89. */
  90. private function _dGoodsComboCache($goods_id) {
  91. return dcache($goods_id, 'goods_combo');
  92. }
  93. private function publish_message()
  94. {
  95. $publisher = new message\publisher();
  96. $publisher->modify_activity_recommend_combo();
  97. }
  98. }