evaluate_goods.model.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. /**
  3. * 商品评价模型
  4. *
  5. */
  6. defined('InShopNC') or exit('Access Invalid!');
  7. class evaluate_goodsModel extends Model {
  8. public function __construct(){
  9. parent::__construct('evaluate_goods');
  10. }
  11. /**
  12. * 查询评价列表
  13. *
  14. * @param array $condition 查询条件
  15. * @param int $page 分页数
  16. * @param string $order 排序
  17. * @param string $field 字段
  18. * @return array
  19. */
  20. public function getEvaluateGoodsList($condition, $page = null, $order = 'geval_id desc', $field = '*') {
  21. $list = $this->field($field)->where($condition)->page($page)->order($order)->select();
  22. return $list;
  23. }
  24. /**
  25. * 根据编号查询商品评价
  26. */
  27. public function getEvaluateGoodsInfoByID($geval_id, $store_id = 0) {
  28. if(intval($geval_id) <= 0) {
  29. return null;
  30. }
  31. $info = $this->where(array('geval_id' => $geval_id))->find();
  32. if($store_id > 0 && intval($info['geval_storeid']) !== $store_id) {
  33. return null;
  34. } else {
  35. return $info;
  36. }
  37. }
  38. /**
  39. * 根据商品编号查询商品评价信息
  40. */
  41. public function getEvaluateGoodsInfoByGoodsID($goods_id) {
  42. $prefix = 'goods_evaluation';
  43. $info = rcache($goods_id, $prefix);
  44. if(empty($info)) {
  45. $info = array();
  46. $count_array = $this->field('count(*) as count,geval_scores')->where(array('geval_goodsid' => $goods_id))->group('geval_scores')->key(geval_scores)->select();
  47. $star1 = intval($count_array['1']['count']);
  48. $star2 = intval($count_array['2']['count']);
  49. $star3 = intval($count_array['3']['count']);
  50. $star4 = intval($count_array['4']['count']);
  51. $star5 = intval($count_array['5']['count']);
  52. $info['good'] = $star4 + $star5;
  53. $info['normal'] = $star2 + $star3;
  54. $info['bad'] = $star1;
  55. $info['all'] = $star1 + $star2 + $star3 + $star4 + $star5;
  56. if(intval($info['all']) > 0) {
  57. $info['good_percent'] = intval($info['good'] / $info['all'] * 100);
  58. $info['normal_percent'] = intval($info['normal'] / $info['all'] * 100);
  59. $info['bad_percent'] = intval($info['bad'] / $info['all'] * 100);
  60. $info['good_star'] = ceil($info['good'] / $info['all'] * 5);
  61. $info['star_average'] = ceil(($star1 + $star2 * 2 + $star3 * 3 + $star4 * 4 + $star5 * 5) / $info['all']);
  62. } else {
  63. $info['good_percent'] = 100;
  64. $info['normal_percent'] = 0;
  65. $info['bad_percent'] = 0;
  66. $info['good_star'] = 5;
  67. $info['star_average'] = 5;
  68. }
  69. //更新商品表好评星级和评论数
  70. $model_goods = Model('goods');
  71. $update = array();
  72. $update['evaluation_good_star'] = $info['star_average'];
  73. $update['evaluation_count'] = $info['all'];
  74. $model_goods->editGoodsById($update, $goods_id);
  75. wcache($goods_id, $info, $prefix);
  76. }
  77. return $info;
  78. }
  79. /**
  80. * 根据抢购编号查询商品评价信息
  81. */
  82. public function getEvaluateGoodsInfoByCommonidID($goods_commonid) {
  83. $prefix = 'goods_common_evaluation';
  84. $info = rcache($goods_commonid, $prefix);
  85. if(empty($info)) {
  86. $info = array();
  87. $info['good_percent'] = 100;
  88. $info['normal_percent'] = 0;
  89. $info['bad_percent'] = 0;
  90. $info['good_star'] = 5;
  91. $info['all'] = 0;
  92. $info['good'] = 0;
  93. $info['normal'] = 0;
  94. $info['bad'] = 0;
  95. $condition = array();
  96. $condition['goods_commonid'] = $goods_commonid;
  97. $goods_list = Model('goods')->getGoodsList($condition, 'goods_id');
  98. if (!empty($goods_list)) {
  99. $goodsid_array = array();
  100. foreach ($goods_list as $value) {
  101. $goodsid_array[] = $value['goods_id'];
  102. }
  103. $good = $this->where(array('geval_goodsid'=>array('in' ,$goodsid_array),'geval_scores' => array('in', '4,5')))->count();
  104. $info['good'] = $good;
  105. $normal = $this->where(array('geval_goodsid'=>array('in' ,$goodsid_array),'geval_scores' => array('in', '2,3')))->count();
  106. $info['normal'] = $normal;
  107. $bad = $this->where(array('geval_goodsid'=>array('in' ,$goodsid_array),'geval_scores' => array('in', '1')))->count();
  108. $info['bad'] = $bad;
  109. $info['all'] = $info['good'] + $info['normal'] + $info['bad'];
  110. if(intval($info['all']) > 0) {
  111. $info['good_percent'] = intval($info['good'] / $info['all'] * 100);
  112. $info['normal_percent'] = intval($info['normal'] / $info['all'] * 100);
  113. $info['bad_percent'] = intval($info['bad'] / $info['all'] * 100);
  114. $info['good_star'] = ceil($info['good'] / $info['all'] * 5);
  115. }
  116. }
  117. wcache($goods_commonid, $info, $prefix, 24*60); // 缓存周期1天。
  118. }
  119. return $info;
  120. }
  121. /**
  122. * 批量添加商品评价
  123. *
  124. * @param array $param
  125. * @param array $goodsid_array 商品id数组,更新缓存使用
  126. * @return boolean
  127. */
  128. public function addEvaluateGoodsArray($param, $goodsid_array) {
  129. $result = $this->insertAll($param);
  130. // 删除商品评价缓存
  131. if ($result && !empty($goodsid_array)) {
  132. foreach ($goodsid_array as $goods_id) {
  133. dcache($goods_id, 'goods_evaluation');
  134. }
  135. }
  136. return $result;
  137. }
  138. /**
  139. * 更新商品评价
  140. *
  141. * 现在此方法只是编辑晒单,不需要更新缓存
  142. * 如果使用此方法修改大星星数量请根据goods_id删除缓存
  143. * 例:dcache($goods_id, 'goods_evaluation');
  144. */
  145. public function editEvaluateGoods($update, $condition) {
  146. return $this->where($condition)->update($update);
  147. }
  148. /**
  149. * 删除商品评价
  150. */
  151. public function delEvaluateGoods($condition) {
  152. return $this->where($condition)->delete();
  153. }
  154. }