Evaluategoods.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. <?php
  2. namespace app\common\model;
  3. use think\facade\Db;
  4. /**
  5. * ============================================================================
  6. *
  7. * ============================================================================
  8. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  9. * 网站地址: https://www.valimart.net/
  10. * ----------------------------------------------------------------------------
  11. *
  12. * ============================================================================
  13. * 数据层模型
  14. */
  15. class Evaluategoods extends BaseModel {
  16. public $page_info;
  17. /**
  18. * 查询评价列表
  19. * @access public
  20. * @author csdeshang
  21. * @param array $condition 查询条件
  22. * @param int $pagesize 分页数
  23. * @param string $order 排序
  24. * @param string $field 字段
  25. * @return array
  26. */
  27. public function getEvaluategoodsList($condition, $pagesize = null, $order = 'geval_id desc', $field = '*') {
  28. if ($pagesize) {
  29. $list = Db::name('evaluategoods')->field($field)->where($condition)->order($order)->paginate(['list_rows'=>$pagesize,'query' => request()->param()],false);
  30. $this->page_info = $list;
  31. return $list->items();
  32. } else {
  33. $list = Db::name('evaluategoods')->field($field)->where($condition)->order($order)->select()->toArray();
  34. return $list;
  35. }
  36. }
  37. /**
  38. * 根据编号查询商品评价
  39. * @access public
  40. * @author csdeshang
  41. * @param int $geval_id 编号
  42. * @param int $store_id 店铺ID
  43. * @return type
  44. */
  45. public function getEvaluategoodsInfoByID($geval_id, $store_id = 0) {
  46. if (intval($geval_id) <= 0) {
  47. return null;
  48. }
  49. $info = Db::name('evaluategoods')->where(array('geval_id' => $geval_id))->find();
  50. if ($store_id > 0 && intval($info['geval_storeid']) !== $store_id) {
  51. return null;
  52. } else {
  53. return $info;
  54. }
  55. }
  56. /**
  57. * 根据商品编号查询商品评价信息
  58. * @access public
  59. * @author csdeshang
  60. * @param int $goods_id 商品ID
  61. * @return array
  62. */
  63. public function getEvaluategoodsInfoByGoodsID($goods_id) {
  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. $prefix = 'goods_common_evaluation';
  111. $info = rcache($goods_commonid, $prefix);
  112. if (empty($info)) {
  113. $info = array();
  114. $info['good_percent'] = 100;
  115. $info['normal_percent'] = 0;
  116. $info['bad_percent'] = 0;
  117. $info['good_star'] = 5;
  118. $info['all'] = 0;
  119. $info['good'] = 0;
  120. $info['normal'] = 0;
  121. $info['bad'] = 0;
  122. $condition = array();
  123. $condition[] = array('goods_commonid','=',$goods_commonid);
  124. $goods_list = model('goods')->getGoodsList($condition, 'goods_id');
  125. if (!empty($goods_list)) {
  126. $goodsid_array = array();
  127. foreach ($goods_list as $value) {
  128. $goodsid_array[] = $value['goods_id'];
  129. }
  130. $good = Db::name('evaluategoods')->where('geval_goodsid','in', $goodsid_array)->where('geval_scores','in', '4,5')->count();
  131. $info['good'] = $good;
  132. $normal = Db::name('evaluategoods')->where('geval_goodsid','in', $goodsid_array)->where('geval_scores','in', '2,3')->count();
  133. $info['normal'] = $normal;
  134. $bad = Db::name('evaluategoods')->where('geval_goodsid','in', $goodsid_array)->where('geval_scores','in', '1')->count();
  135. $info['bad'] = $bad;
  136. $info['all'] = $info['good'] + $info['normal'] + $info['bad'];
  137. if (intval($info['all']) > 0) {
  138. $info['good_percent'] = intval($info['good'] / $info['all'] * 100);
  139. $info['normal_percent'] = intval($info['normal'] / $info['all'] * 100);
  140. $info['bad_percent'] = intval($info['bad'] / $info['all'] * 100);
  141. $info['good_star'] = ceil($info['good'] / $info['all'] * 5);
  142. }
  143. }
  144. wcache($goods_commonid, $info, $prefix, 24 * 60); // 缓存周期1天。
  145. }
  146. return $info;
  147. }
  148. /**
  149. * 批量添加商品评价
  150. * @access public
  151. * @author csdeshang
  152. * @param array $datas 参数内容
  153. * @param array $goodsid_array 商品id数组,更新缓存使用
  154. * @return boolean
  155. */
  156. public function addEvaluategoodsArray($datas, $goodsid_array) {
  157. $result = Db::name('evaluategoods')->insertAll($datas);
  158. // 删除商品评价缓存
  159. if ($result && !empty($goodsid_array)) {
  160. foreach ($goodsid_array as $goods_id) {
  161. dcache($goods_id, 'goods_evaluation');
  162. }
  163. }
  164. return $result;
  165. }
  166. /**
  167. * 更新商品评价
  168. *
  169. * 现在此方法只是编辑晒单,不需要更新缓存
  170. * 如果使用此方法修改大星星数量请根据goods_id删除缓存
  171. * 例:dcache($goods_id, 'goods_evaluation');
  172. * @access public
  173. * @author csdeshang
  174. * @param array $update 更新数据
  175. * @param array $condition 条件
  176. * @return bool
  177. */
  178. public function editEvaluategoods($update, $condition) {
  179. $goodsid_array = Db::name('evaluategoods')->where($condition)->column('geval_goodsid');
  180. foreach ($goodsid_array as $goods_id) {
  181. dcache($goods_id, 'goods_evaluation');
  182. }
  183. return Db::name('evaluategoods')->where($condition)->update($update);
  184. }
  185. /**
  186. * 删除商品评价
  187. * @access public
  188. * @author csdeshang
  189. * @param type $condition 条件
  190. * @return bool
  191. */
  192. public function delEvaluategoods($condition) {
  193. $goodsid_array = Db::name('evaluategoods')->where($condition)->column('geval_goodsid');
  194. foreach ($goodsid_array as $goods_id) {
  195. dcache($goods_id, 'goods_evaluation');
  196. }
  197. return Db::name('evaluategoods')->where($condition)->delete();
  198. }
  199. }
  200. ?>