Goodscombo.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /**
  3. * 商品推荐组合模型
  4. *
  5. */
  6. namespace app\common\model;
  7. use think\facade\Db;
  8. /**
  9. * ============================================================================
  10. *
  11. * ============================================================================
  12. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  13. * 网站地址: https://www.valimart.net/
  14. * ----------------------------------------------------------------------------
  15. *
  16. * ============================================================================
  17. * 数据层模型
  18. */
  19. class Goodscombo extends BaseModel
  20. {
  21. /**
  22. * 插入数据
  23. * @access public
  24. * @author csdeshang
  25. * @param array $data 参数内容
  26. * @return boolean
  27. */
  28. public function addGoodscomboAll($data) {
  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. return Db::name('goodscombo')->where($condition)->select()->toArray();
  46. }
  47. /**
  48. * 查询组合商品列表
  49. * @access public
  50. * @author csdeshang
  51. * @param type $condition 查询条件
  52. * @return boolean
  53. */
  54. public function delGoodscombo($condition) {
  55. $list = $this->getGoodscomboList($condition, 'goods_id');
  56. if (empty($list)) {
  57. return true;
  58. }
  59. $result = Db::name('goodscombo')->where($condition)->delete();
  60. if ($result) {
  61. foreach ($list as $v) {
  62. $this->_dGoodscomboCache($v['goods_id']);
  63. }
  64. }
  65. return $result;
  66. }
  67. /**
  68. * 获取商品组合缓存
  69. * @access public
  70. * @author csdeshang
  71. * @param type $goods_id 商品ID
  72. * @return array
  73. */
  74. public function getGoodscomboCacheByGoodsId($goods_id) {
  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. return rcache($goods_id, 'goods_combo');
  100. }
  101. /**
  102. * 写入商品推荐搭配缓存
  103. * @access public
  104. * @author csdeshang
  105. * @param int $goods_id 商品ID
  106. * @param array $array 数组内容
  107. * @return boolean
  108. */
  109. private function _wGoodscomboCache($goods_id, $array) {
  110. return wcache($goods_id, $array, 'goods_combo', 60);
  111. }
  112. /**
  113. * 删除商品推荐搭配缓存
  114. * @access public
  115. * @author csdeshang
  116. * @param int $goods_id 商品第
  117. * @return boolean
  118. */
  119. private function _dGoodscomboCache($goods_id) {
  120. return dcache($goods_id, 'goods_combo');
  121. }
  122. }