EvaluateController.php 9.4 KB

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