EvaluateController.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. <?php
  2. namespace app\admin\controller\client;
  3. use app\admin\service\member\MemberService;
  4. use app\controller\Curd;
  5. use app\model\ClientFavorite;
  6. use app\model\Goods;
  7. use app\model\GoodsEvaluate;
  8. use app\model\Member;
  9. use app\model\MemberRole;
  10. use support\exception\BusinessException;
  11. use support\Request;
  12. use support\Response;
  13. class EvaluateController extends Curd
  14. {
  15. public function __construct()
  16. {
  17. $this->model = new GoodsEvaluate();
  18. }
  19. public function select(Request $request): Response
  20. {
  21. $goodsName = $request->get('goods_name', '');
  22. $searchTimes = $request->get('goods_evaluate_addtimes', []);
  23. [$where, $format, $limit, $field, $order] = $this->selectInput($request);
  24. $order = $request->get('order', 'desc');
  25. $field = $field ?? 'goods_evaluate_addtimes';
  26. if ($goodsName != '') {
  27. $goodsIds = Goods::where('goods_name', 'like', '%' . $goodsName . '%')->pluck('goods_id')->toArray();
  28. $where['join_evaluate_goods_id'] = ['in', implode(',', $goodsIds)];
  29. }
  30. if (!empty($searchTimes)) {
  31. $searchTimes[0] = !empty($searchTimes[0]) ? strtotime($searchTimes[0]) : time();
  32. $searchTimes[1] = !empty($searchTimes[1]) ? strtotime($searchTimes[1]) : time();
  33. $where['goods_evaluate_addtimes'] = $searchTimes;
  34. }
  35. $query = $this->doSelect($where, $field, $order);
  36. return $this->doFormat($query, $format, $limit);
  37. }
  38. protected function doSelect(array $where, string $field = null, string $order = 'desc')
  39. {
  40. $model = $this->model->with([
  41. 'member' => function ($query) {
  42. $query->select('member_id', 'member_mobile');
  43. },
  44. 'good' => function ($query) {
  45. $query->select('goods_id', 'goods_name');
  46. },
  47. 'cert' => function ($query) {
  48. $query->select('join_cert_member_id', 'member_cert_name');
  49. },
  50. ]);
  51. foreach ($where as $column => $value) {
  52. if (is_array($value)) {
  53. if ($value[0] === 'like' || $value[0] === 'not like') {
  54. $model = $model->where($column, $value[0], "%$value[1]%");
  55. } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
  56. $model = $model->where($column, $value[0], $value[1]);
  57. } elseif ($value[0] == 'in' && !empty($value[1])) {
  58. $valArr = $value[1];
  59. if (is_string($value[1])) {
  60. $valArr = explode(",", trim($value[1]));
  61. }
  62. $model = $model->whereIn($column, $valArr);
  63. } elseif ($value[0] == 'not in' && !empty($value[1])) {
  64. $valArr = $value[1];
  65. if (is_string($value[1])) {
  66. $valArr = explode(",", trim($value[1]));
  67. }
  68. $model = $model->whereNotIn($column, $valArr);
  69. } elseif ($value[0] == 'null') {
  70. $model = $model->whereNull($column);
  71. } elseif ($value[0] == 'not null') {
  72. $model = $model->whereNotNull($column);
  73. } elseif ($value[0] !== '' || $value[1] !== '') {
  74. $model = $model->whereBetween($column, $value);
  75. }
  76. } else {
  77. $model = $model->where($column, $value);
  78. }
  79. }
  80. if ($field) {
  81. $model = $model->orderBy($field, $order);
  82. }
  83. return $model;
  84. }
  85. public function afterQuery($items)
  86. {
  87. foreach ($items as &$item) {
  88. if (!empty($item->goods_evaluate_json)) {
  89. $imagesStr = '';
  90. $goodsEvaluateJson = json_decode($item->goods_evaluate_json, true);
  91. if (isset($goodsEvaluateJson['photo'])) {
  92. foreach ($goodsEvaluateJson['photo'] as $images) {
  93. if (isset($images['file'])) {
  94. $imagesStr .= getenv('APP_IMAGE_DOMAIN') . $images['file']['file_savepath'] . $images['file']['file_savename'] . ',';
  95. }
  96. }
  97. }
  98. if (isset($goodsEvaluateJson['desc'])) {
  99. $item->desc = $goodsEvaluateJson['desc'];
  100. }
  101. $item->photos = rtrim($imagesStr, ',');
  102. }
  103. }
  104. return $items;
  105. }
  106. /**
  107. * @Desc 评价管理顶部统计
  108. * @Author Gorden
  109. * @Date 2024/11/1 13:41
  110. *
  111. * @param Request $request
  112. * @return Response
  113. */
  114. public function statistics(Request $request)
  115. {
  116. $total = GoodsEvaluate::count('goods_evaluate_id');
  117. $pendingTotal = GoodsEvaluate::where('goods_evaluate_status', 'PENDING')->count('goods_evaluate_id');
  118. $activedTotal = GoodsEvaluate::where('goods_evaluate_status', 'ACTIVED')->count('goods_evaluate_id');
  119. $disabledTotal = GoodsEvaluate::where('goods_evaluate_status', 'DELETED')->count('goods_evaluate_id');
  120. $oneScoreTotal = GoodsEvaluate::where('goods_evaluate_value', 1)->count('goods_evaluate_id');
  121. $twoScoretotal = GoodsEvaluate::where('goods_evaluate_value', 2)->count('goods_evaluate_id');
  122. $threeScoretotal = GoodsEvaluate::where('goods_evaluate_value', 3)->count('goods_evaluate_id');
  123. $fourScoretotal = GoodsEvaluate::where('goods_evaluate_value', 4)->count('goods_evaluate_id');
  124. $fiveScoretotal = GoodsEvaluate::where('goods_evaluate_value', 5)->count('goods_evaluate_id');
  125. return json_success('success', [
  126. 'total' => $total,
  127. 'pendingTotal' => $pendingTotal,
  128. 'activedTotal' => $activedTotal,
  129. 'disabledTotal' => $disabledTotal,
  130. 'oneScoreTotal' => $oneScoreTotal,
  131. 'twoScoretotal' => $twoScoretotal,
  132. 'threeScoretotal' => $threeScoretotal,
  133. 'fourScoretotal' => $fourScoretotal,
  134. 'fiveScoretotal' => $fiveScoretotal,
  135. ]);
  136. }
  137. public function info(Request $request): Response
  138. {
  139. $goodsEvaluateId = $request->get('goods_evaluate_id', '');
  140. if (!$goodsEvaluateId) {
  141. return json_fail('参数异常');
  142. }
  143. $evaluate = GoodsEvaluate::with([
  144. 'member' => function ($query) {
  145. $query->select('member_id', 'member_mobile', 'member_is_owner', 'join_member_role_id', 'member_is_vip', 'member_is_partner', 'member_is_referrer');
  146. },
  147. 'cert' => function ($query) {
  148. $query->select('join_cert_member_id', 'member_cert_name');
  149. },
  150. 'info' => function ($query) {
  151. $query->select('join_info_member_id', 'member_info_nickname', 'member_info_headimg');
  152. },
  153. 'goods' => function ($query) {
  154. $query->select('goods_id', 'goods_name', 'goods_cover', 'goods_sales_price');
  155. },
  156. 'order' => function ($query) {
  157. $query->select('order_id', 'order_amount_pay', 'order_is_complete', 'order_status_system', 'order_status_payment', 'order_addtimes');
  158. }
  159. ])->where('goods_evaluate_id', $goodsEvaluateId)
  160. ->first();
  161. if (!empty($evaluate->goods_evaluate_json)) {
  162. $imagesStr = '';
  163. $goodsEvaluateJson = json_decode($evaluate->goods_evaluate_json, true);
  164. if (isset($goodsEvaluateJson['photo'])) {
  165. foreach ($goodsEvaluateJson['photo'] as $file) {
  166. if (isset($file['file'])) {
  167. $imagesStr .= getenv('APP_IMAGE_DOMAIN_LOCAL') . $file['file']['file_savepath'] . $file['file']['file_savename'] . ',';
  168. }
  169. }
  170. $evaluate->photos = rtrim($imagesStr, ',');
  171. }
  172. if (isset($goodsEvaluateJson['desc'])) {
  173. $evaluate->desc = $goodsEvaluateJson['desc'];
  174. }
  175. }
  176. if (!empty($evaluate->member) && !empty($evaluate->member->join_member_role_id)) {
  177. $evaluate->member->level = MemberRole::where('member_role_id', $evaluate->member->join_member_role_id)->value('member_role_name');
  178. }
  179. $evaluate->member->member_info_headimg = MemberService::getAvatarUrl(!empty($evaluate->info) && !empty($evaluate->info->member_info_headimg) ? $evaluate->info->member_info_headimg : '');
  180. $evaluate->member->member_info_nickname = MemberService::getNickname(!empty($evaluate->info) && !empty($evaluate->info->member_info_nickname) ? $evaluate->info->member_info_nickname : '', !empty($evaluate->member) ? $evaluate->member->member_mobile : '');
  181. if (!empty($evaluate->goods)) {
  182. $evaluateGoods = $evaluate->goods;
  183. foreach ($evaluateGoods as &$good) {
  184. $good['goods_cover'] = getenv('STORAGE_DOMAIN') . $good['goods_cover'];
  185. $good['goods_cover_origin'] = str_replace('/thumb', '', $good['goods_cover']);
  186. }
  187. $evaluate->goods = $evaluateGoods;
  188. // $evaluate->goods->goods_cover = getenv('STORAGE_DOMAIN') . $evaluate->goods->goods_cover;
  189. // $evaluate->goods->goods_cover_origin = str_replace('/thumb', '', $evaluate->goods->goods_cover);
  190. }
  191. return json_success('success', $evaluate);
  192. }
  193. /**
  194. * 添加
  195. * @param Request $request
  196. * @return Response
  197. * @throws BusinessException
  198. */
  199. public function insert(Request $request): Response
  200. {
  201. try {
  202. $data = $this->insertInput($request);
  203. $this->doInsert($data);
  204. } catch (BusinessException $customException) {
  205. return json_fail($customException->getMessage());
  206. } catch (\Exception $e) {
  207. dump($e->getMessage());
  208. return json_fail('数据写入失败');
  209. }
  210. return json_success('success');
  211. }
  212. /**
  213. * 插入前置方法
  214. * @param Request $request
  215. * @return array
  216. * @throws BusinessException
  217. */
  218. protected function insertInput(Request $request): array
  219. {
  220. $data = $this->inputFilter($request->post());
  221. if (!empty($data['goods_evaluate_json'])) {
  222. $goodsEvaluateJson = explode(',', $data['goods_evaluate_json']);
  223. foreach ($goodsEvaluateJson as $key => $item) {
  224. $goodsEvaluateJson[$key] = str_replace(getenv('STORAGE_DOMAIN'), '', $item);
  225. }
  226. $data['goods_evaluate_json'] = json_encode(['images' => $goodsEvaluateJson]);
  227. }
  228. return $data;
  229. }
  230. /**
  231. * 更新
  232. * @param Request $request
  233. * @return Response
  234. * @throws BusinessException
  235. */
  236. public function update(Request $request): Response
  237. {
  238. try {
  239. [$id, $data] = $this->updateInput($request);
  240. $this->doUpdate($id, $data);
  241. } catch (BusinessException $e) {
  242. return json_fail($e->getMessage());
  243. } catch (\Exception $e) {
  244. dump($e->getMessage());
  245. return json_fail('数据更新失败');
  246. }
  247. return json_success('success');
  248. }
  249. /**
  250. * 更新前置方法
  251. * @param Request $request
  252. * @return array
  253. * @throws BusinessException
  254. */
  255. protected function updateInput(Request $request): array
  256. {
  257. $primary_key = $this->model->getKeyName();
  258. $id = $request->post($primary_key);
  259. $data = $this->inputFilter($request->post());
  260. $model = $this->model->find($id);
  261. if (!$model) {
  262. throw new BusinessException('记录不存在', 2);
  263. }
  264. if (!empty($data['goods_evaluate_json'])) {
  265. $goodsEvaluateJson = explode(',', $data['goods_evaluate_json']);
  266. foreach ($goodsEvaluateJson as $key => $item) {
  267. $goodsEvaluateJson[$key] = str_replace(getenv('STORAGE_DOMAIN'), '', $item);
  268. }
  269. $data['goods_evaluate_json'] = json_encode(['images' => $goodsEvaluateJson]);
  270. }
  271. unset($data[$primary_key]);
  272. return [$id, $data];
  273. }
  274. }