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