Goodsgift.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 Goodsgift extends BaseModel
  16. {
  17. /**
  18. * 插入数据
  19. * @access public
  20. * @author csdeshang
  21. * @param array $insert 插入内容
  22. * @return boolean
  23. */
  24. public function addGoodsgiftAll($insert)
  25. {
  26. return Db::name('goodsgift')->insertAll($insert);
  27. }
  28. /**
  29. * 查询赠品列表
  30. * @access public
  31. * @author csdeshang
  32. * @param array $condition 条件
  33. * @return array
  34. */
  35. public function getGoodsgiftList($condition)
  36. {
  37. return Db::name('goodsgift')->where($condition)->select()->toArray();
  38. }
  39. /**
  40. * 获取赠品列表根据商品ID
  41. * @access public
  42. * @author csdeshang
  43. * @param int $goods_id
  44. * @return array
  45. */
  46. public function getGoodsgiftListByGoodsId($goods_id)
  47. {
  48. $condition = array();
  49. $condition[] = array('goods_id', '=', $goods_id);
  50. $list = $this->_rGoodsgiftCache($goods_id);
  51. if (empty($list)) {
  52. $gift_list = $this->getGoodsgiftList($condition);
  53. $list['gift'] = serialize($gift_list);
  54. $this->_wGoodsgiftCache($goods_id, $list);
  55. }
  56. $gift_list = unserialize($list['gift']);
  57. return $gift_list;
  58. }
  59. /**
  60. * 删除赠品
  61. * @access public
  62. * @author csdeshang
  63. * @param array $condition 条件
  64. * @return boolean
  65. */
  66. public function delGoodsgift($condition)
  67. {
  68. $gift_list = $this->getGoodsgiftList($condition);
  69. if (empty($gift_list)) {
  70. return true;
  71. }
  72. $result = Db::name('goodsgift')->where($condition)->delete();
  73. if ($result) {
  74. foreach ($gift_list as $val) {
  75. $this->_dGoodsgiftCache($val['goods_id']);
  76. }
  77. }
  78. return $result;
  79. }
  80. /**
  81. * 读取商品公共缓存
  82. * @access public
  83. * @author csdeshang
  84. * @param int $goods_id 商品ID
  85. * @return array
  86. */
  87. private function _rGoodsgiftCache($goods_id)
  88. {
  89. return rcache($goods_id, 'goods_gift');
  90. }
  91. /**
  92. * 写入商品公共缓存
  93. * @access public
  94. * @author csdeshang
  95. * @param int $goods_id 商品ID
  96. * @param array $list 列表
  97. * @return boolean
  98. */
  99. private function _wGoodsgiftCache($goods_id, $list)
  100. {
  101. return wcache($goods_id, $list, 'goods_gift');
  102. }
  103. /**
  104. * 删除商品公共缓存
  105. * @access public
  106. * @author csdeshang
  107. * @param int $goods_id 商品ID
  108. * @return boolean
  109. */
  110. private function _dGoodsgiftCache($goods_id)
  111. {
  112. return dcache($goods_id, 'goods_gift');
  113. }
  114. }