Evaluategoods.php 8.3 KB

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