Evaluategoods.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. *
  9. * ----------------------------------------------------------------------------
  10. *
  11. * ============================================================================
  12. * 数据层模型
  13. */
  14. class Evaluategoods extends BaseModel {
  15. public $page_info;
  16. /**
  17. * 查询评价列表
  18. * @access public
  19. * @author csdeshang
  20. * @param array $condition 查询条件
  21. * @param int $pagesize 分页数
  22. * @param string $order 排序
  23. * @param string $field 字段
  24. * @return array
  25. */
  26. public function getEvaluategoodsList($condition, $pagesize = null, $order = 'geval_id desc', $field = '*') {
  27. if ($pagesize) {
  28. $list = Db::name('evaluategoods')->field($field)->where($condition)->order($order)->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
  29. $this->page_info = $list;
  30. return $list->items();
  31. } else {
  32. $list = Db::name('evaluategoods')->field($field)->where($condition)->order($order)->select()->toArray();
  33. return $list;
  34. }
  35. }
  36. /**
  37. * 根据编号查询商品评价
  38. * @access public
  39. * @author csdeshang
  40. * @param int $geval_id 编号
  41. * @param int $store_id 店铺ID
  42. * @return type
  43. */
  44. public function getEvaluategoodsInfoByID($geval_id, $store_id = 0) {
  45. if (intval($geval_id) <= 0) {
  46. return null;
  47. }
  48. $info = Db::name('evaluategoods')->where(array('geval_id' => $geval_id))->find();
  49. if ($store_id > 0 && intval($info['geval_storeid']) !== $store_id) {
  50. return null;
  51. } else {
  52. return $info;
  53. }
  54. }
  55. /**
  56. * 根据商品编号查询商品评价信息
  57. * @access public
  58. * @author csdeshang
  59. * @param int $goods_id 商品ID
  60. * @return array
  61. */
  62. public function getEvaluategoodsInfoByGoodsID($goods_id) {
  63. $prefix = 'goods_evaluation';
  64. $info = rcache($goods_id, $prefix);
  65. if (empty($info)) {
  66. $info = array();
  67. $count_array = Db::name('evaluategoods')->field('count(*) as count,geval_scores')->where(array('geval_goodsid' => $goods_id))->group('geval_scores')->select()->toArray();
  68. $count_array = ds_change_arraykey($count_array, 'geval_scores');
  69. $star1 = isset($count_array['1']['count']) ? intval($count_array['1']['count']) : 0;
  70. $star2 = isset($count_array['2']['count']) ? intval($count_array['2']['count']) : 0;
  71. $star3 = isset($count_array['3']['count']) ? intval($count_array['3']['count']) : 0;
  72. $star4 = isset($count_array['4']['count']) ? intval($count_array['4']['count']) : 0;
  73. $star5 = isset($count_array['5']['count']) ? intval($count_array['5']['count']) : 0;
  74. $info['good'] = $star4 + $star5;
  75. $info['normal'] = $star2 + $star3;
  76. $info['bad'] = $star1;
  77. $info['all'] = $star1 + $star2 + $star3 + $star4 + $star5;
  78. if (intval($info['all']) > 0) {
  79. $info['good_percent'] = intval($info['good'] / $info['all'] * 100);
  80. $info['normal_percent'] = intval($info['normal'] / $info['all'] * 100);
  81. $info['bad_percent'] = intval($info['bad'] / $info['all'] * 100);
  82. $info['good_star'] = ceil($info['good'] / $info['all'] * 5);
  83. $info['star_average'] = ceil(($star1 + $star2 * 2 + $star3 * 3 + $star4 * 4 + $star5 * 5) / $info['all']);
  84. } else {
  85. $info['good_percent'] = 100;
  86. $info['normal_percent'] = 0;
  87. $info['bad_percent'] = 0;
  88. $info['good_star'] = 5;
  89. $info['star_average'] = 5;
  90. }
  91. //更新商品表好评星级和评论数
  92. $goods_model = model('goods');
  93. $update = array();
  94. $update['evaluation_good_star'] = $info['star_average'];
  95. $update['evaluation_count'] = $info['all'];
  96. $goods_model->editGoodsById($update, $goods_id);
  97. wcache($goods_id, $info, $prefix);
  98. }
  99. return $info;
  100. }
  101. /**
  102. * 根据抢购编号查询商品评价信息
  103. * @access public
  104. * @author csdeshang
  105. * @param int $goods_commonid 抢购编号
  106. * @return array
  107. */
  108. public function getEvaluategoodsInfoByCommonidID($goods_commonid) {
  109. $prefix = 'goods_common_evaluation';
  110. $info = rcache($goods_commonid, $prefix);
  111. if (empty($info)) {
  112. $info = array();
  113. $info['good_percent'] = 100;
  114. $info['normal_percent'] = 0;
  115. $info['bad_percent'] = 0;
  116. $info['good_star'] = 5;
  117. $info['all'] = 0;
  118. $info['good'] = 0;
  119. $info['normal'] = 0;
  120. $info['bad'] = 0;
  121. $condition = array();
  122. $condition[] = array('goods_commonid','=',$goods_commonid);
  123. $goods_list = model('goods')->getGoodsList($condition, 'goods_id');
  124. if (!empty($goods_list)) {
  125. $goodsid_array = array();
  126. foreach ($goods_list as $value) {
  127. $goodsid_array[] = $value['goods_id'];
  128. }
  129. $good = Db::name('evaluategoods')->where('geval_goodsid','in', $goodsid_array)->where('geval_scores','in', '4,5')->count();
  130. $info['good'] = $good;
  131. $normal = Db::name('evaluategoods')->where('geval_goodsid','in', $goodsid_array)->where('geval_scores','in', '2,3')->count();
  132. $info['normal'] = $normal;
  133. $bad = Db::name('evaluategoods')->where('geval_goodsid','in', $goodsid_array)->where('geval_scores','in', '1')->count();
  134. $info['bad'] = $bad;
  135. $info['all'] = $info['good'] + $info['normal'] + $info['bad'];
  136. if (intval($info['all']) > 0) {
  137. $info['good_percent'] = intval($info['good'] / $info['all'] * 100);
  138. $info['normal_percent'] = intval($info['normal'] / $info['all'] * 100);
  139. $info['bad_percent'] = intval($info['bad'] / $info['all'] * 100);
  140. $info['good_star'] = ceil($info['good'] / $info['all'] * 5);
  141. }
  142. }
  143. wcache($goods_commonid, $info, $prefix, 24 * 60); // 缓存周期1天。
  144. }
  145. return $info;
  146. }
  147. /**
  148. * 批量添加商品评价
  149. * @access public
  150. * @author csdeshang
  151. * @param array $datas 参数内容
  152. * @param array $goodsid_array 商品id数组,更新缓存使用
  153. * @return boolean
  154. */
  155. public function addEvaluategoodsArray($datas, $goodsid_array) {
  156. $result = Db::name('evaluategoods')->insertAll($datas);
  157. // 删除商品评价缓存
  158. if ($result && !empty($goodsid_array)) {
  159. foreach ($goodsid_array as $goods_id) {
  160. dcache($goods_id, 'goods_evaluation');
  161. }
  162. }
  163. return $result;
  164. }
  165. /**
  166. * 更新商品评价
  167. *
  168. * 现在此方法只是编辑晒单,不需要更新缓存
  169. * 如果使用此方法修改大星星数量请根据goods_id删除缓存
  170. * 例:dcache($goods_id, 'goods_evaluation');
  171. * @access public
  172. * @author csdeshang
  173. * @param array $update 更新数据
  174. * @param array $condition 条件
  175. * @return bool
  176. */
  177. public function editEvaluategoods($update, $condition) {
  178. $goodsid_array = Db::name('evaluategoods')->where($condition)->column('geval_goodsid');
  179. foreach ($goodsid_array as $goods_id) {
  180. dcache($goods_id, 'goods_evaluation');
  181. }
  182. return Db::name('evaluategoods')->where($condition)->update($update);
  183. }
  184. /**
  185. * 删除商品评价
  186. * @access public
  187. * @author csdeshang
  188. * @param type $condition 条件
  189. * @return bool
  190. */
  191. public function delEvaluategoods($condition) {
  192. $goodsid_array = Db::name('evaluategoods')->where($condition)->column('geval_goodsid');
  193. foreach ($goodsid_array as $goods_id) {
  194. dcache($goods_id, 'goods_evaluation');
  195. }
  196. return Db::name('evaluategoods')->where($condition)->delete();
  197. }
  198. }