Goodscombo.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. /**
  3. * 商品推荐组合模型
  4. *
  5. */
  6. namespace app\common\model;
  7. use think\facade\Db;
  8. /**
  9. * ============================================================================
  10. *
  11. * ============================================================================
  12. *
  13. * ----------------------------------------------------------------------------
  14. *
  15. * ============================================================================
  16. * 数据层模型
  17. */
  18. class Goodscombo extends BaseModel
  19. {
  20. /**
  21. * 插入数据
  22. * @access public
  23. * @author csdeshang
  24. * @param array $data 参数内容
  25. * @return boolean
  26. */
  27. public function addGoodscomboAll($data)
  28. {
  29. $result = Db::name('goodscombo')->insertAll($data);
  30. if ($result) {
  31. foreach ((array)$data as $v) {
  32. if ($v['goods_id']) $this->_dGoodscomboCache($v['goods_id']);
  33. }
  34. }
  35. return $result;
  36. }
  37. /**
  38. * 查询组合商品列表
  39. * @access public
  40. * @author csdeshang
  41. * @param array $condition 查询条件
  42. * @return boolean
  43. */
  44. public function getGoodscomboList($condition)
  45. {
  46. return Db::name('goodscombo')->where($condition)->select()->toArray();
  47. }
  48. /**
  49. * 查询组合商品列表
  50. * @access public
  51. * @author csdeshang
  52. * @param type $condition 查询条件
  53. * @return boolean
  54. */
  55. public function delGoodscombo($condition)
  56. {
  57. $list = $this->getGoodscomboList($condition, 'goods_id');
  58. if (empty($list)) {
  59. return true;
  60. }
  61. $result = Db::name('goodscombo')->where($condition)->delete();
  62. if ($result) {
  63. foreach ($list as $v) {
  64. $this->_dGoodscomboCache($v['goods_id']);
  65. }
  66. }
  67. return $result;
  68. }
  69. /**
  70. * 获取商品组合缓存
  71. * @access public
  72. * @author csdeshang
  73. * @param type $goods_id 商品ID
  74. * @return array
  75. */
  76. public function getGoodscomboCacheByGoodsId($goods_id)
  77. {
  78. $array = $this->_rGoodscomboCache($goods_id);
  79. if (empty($array)) {
  80. $gcombo_list = array();
  81. $combo_list = $this->getGoodscomboList(array('goods_id' => $goods_id));
  82. if (!empty($combo_list)) {
  83. $comboid_array = array();
  84. foreach ($combo_list as $val) {
  85. $comboid_array[] = $val['combo_goodsid'];
  86. }
  87. $gcombo_list = model('goods')->getGeneralGoodsList(array(array('goods_id', 'in', $comboid_array)));
  88. }
  89. $array = array('gcombo_list' => serialize($gcombo_list));
  90. $this->_wGoodscomboCache($goods_id, $array);
  91. }
  92. return $array;
  93. }
  94. /**
  95. * 读取商品推荐搭配缓存
  96. * @access public
  97. * @author csdeshang
  98. * @param int $goods_id 商品id
  99. * @return array
  100. */
  101. private function _rGoodscomboCache($goods_id)
  102. {
  103. return rcache($goods_id, 'goods_combo');
  104. }
  105. /**
  106. * 写入商品推荐搭配缓存
  107. * @access public
  108. * @author csdeshang
  109. * @param int $goods_id 商品ID
  110. * @param array $array 数组内容
  111. * @return boolean
  112. */
  113. private function _wGoodscomboCache($goods_id, $array)
  114. {
  115. return wcache($goods_id, $array, 'goods_combo', 60);
  116. }
  117. /**
  118. * 删除商品推荐搭配缓存
  119. * @access public
  120. * @author csdeshang
  121. * @param int $goods_id 商品第
  122. * @return boolean
  123. */
  124. private function _dGoodscomboCache($goods_id)
  125. {
  126. return dcache($goods_id, 'goods_combo');
  127. }
  128. }