EvaluateController.php 9.3 KB

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