123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260 |
- <?php
- namespace app\admin\controller\client;
- use app\controller\Curd;
- use app\model\ClientFavorite;
- use app\model\Goods;
- use app\model\GoodsEvaluate;
- use support\exception\BusinessException;
- use support\Request;
- use support\Response;
- class EvaluateController extends Curd
- {
- public function __construct()
- {
- $this->model = new GoodsEvaluate();
- }
- public function select(Request $request): Response
- {
- $goodsName = $request->get('goods_name','');
- $searchTimes = $request->get('goods_evaluate_addtimes',[]);
- [$where, $format, $limit, $field, $order] = $this->selectInput($request);
- $order = $request->get('order', 'desc');
- $field = $field ?? 'goods_evaluate_addtimes';
- if ($goodsName != ''){
- $goodsIds = Goods::where('goods_name','like','%'.$goodsName.'%')->pluck('goods_id')->toArray();
- $where['join_evaluate_goods_id'] = ['in',implode(',',$goodsIds)];
- }
- if(!empty($searchTimes)){
- $searchTimes[0] = !empty($searchTimes[0]) ? strtotime($searchTimes[0]) : time();
- $searchTimes[1] = !empty($searchTimes[1]) ? strtotime($searchTimes[1]) : time();
- $where['goods_evaluate_addtimes'] = $searchTimes;
- }
- $query = $this->doSelect($where, $field, $order);
- return $this->doFormat($query, $format, $limit);
- }
- protected function doSelect(array $where, string $field = null, string $order = 'desc')
- {
- $model = $this->model->with([
- 'member' => function ($query) {
- $query->select('member_id', 'member_mobile');
- },
- 'goods' => function ($query) {
- $query->select('goods_id', 'goods_name');
- },
- 'cert' => function ($query) {
- $query->select('join_cert_member_id', 'member_cert_name');
- },
- ]);
- foreach ($where as $column => $value) {
- if (is_array($value)) {
- if ($value[0] === 'like' || $value[0] === 'not like') {
- $model = $model->where($column, $value[0], "%$value[1]%");
- } elseif (in_array($value[0], ['>', '=', '<', '<>'])) {
- $model = $model->where($column, $value[0], $value[1]);
- } elseif ($value[0] == 'in' && !empty($value[1])) {
- $valArr = $value[1];
- if (is_string($value[1])) {
- $valArr = explode(",", trim($value[1]));
- }
- $model = $model->whereIn($column, $valArr);
- } elseif ($value[0] == 'not in' && !empty($value[1])) {
- $valArr = $value[1];
- if (is_string($value[1])) {
- $valArr = explode(",", trim($value[1]));
- }
- $model = $model->whereNotIn($column, $valArr);
- } elseif ($value[0] == 'null') {
- $model = $model->whereNull($column);
- } elseif ($value[0] == 'not null') {
- $model = $model->whereNotNull($column);
- } elseif ($value[0] !== '' || $value[1] !== '') {
- $model = $model->whereBetween($column, $value);
- }
- } else {
- $model = $model->where($column, $value);
- }
- }
- if ($field) {
- $model = $model->orderBy($field, $order);
- }
- return $model;
- }
- public function afterQuery($items)
- {
- foreach ($items as &$item){
- if (!empty($item->goods_evaluate_json)){
- $imagesStr = '';
- $goodsEvaluateJson = json_decode($item->goods_evaluate_json,true);
- if (isset($goodsEvaluateJson['photo'])){
- foreach ($goodsEvaluateJson['photo'] as $images){
- if (isset($images['file'])){
- $imagesStr .= getenv('APP_IMAGE_DOMAIN').$images['file']['file_savepath'].$images['file']['file_savename'].',';
- }
- }
- }
- if (isset($goodsEvaluateJson['desc'])){
- $item->desc = $goodsEvaluateJson['desc'];
- }
- $item->photos = rtrim($imagesStr,',');
- }
- }
- return $items;
- }
- public function info(Request $request):Response
- {
- $goodsEvaluateId = $request->get('goods_evaluate_id','');
- if (!$goodsEvaluateId){
- return json_fail('参数异常');
- }
- $evaluate = GoodsEvaluate::with([
- 'member'=>function($query){
- $query->select('member_id','member_mobile');
- },
- 'cert' => function ($query){
- $query->select('join_cert_member_id','member_cert_name');
- },
- 'info' => function ($query){
- $query->select('join_info_member_id','member_info_nickname','member_info_headimg');
- },
- 'goods' => function($query){
- $query->select('goods_id','goods_name','goods_cover','goods_sales_price');
- },
- 'order' => function($query){
- $query->select('order_id','order_amount_pay','order_is_complete','order_status_system','order_status_payment','order_addtimes');
- }
- ])->where('goods_evaluate_id',$goodsEvaluateId)
- ->first();
- if (!empty($evaluate->goods_evaluate_json)){
- $imagesStr = '';
- $goodsEvaluateJson = json_decode($evaluate->goods_evaluate_json,true);
- if (isset($goodsEvaluateJson['photo'])){
- foreach($goodsEvaluateJson['photo'] as $file){
- if (isset($file['file'])){
- $imagesStr .= getenv('APP_IMAGE_DOMAIN').$file['file']['file_savepath'].$file['file']['file_savename'].',';
- }
- }
- $evaluate->photos = rtrim($imagesStr,',');
- }
- if (isset($goodsEvaluateJson['desc'])){
- $evaluate->desc = $goodsEvaluateJson['desc'];
- }
- }
- if (!empty($evaluate->info)){
- $evaluate->info->member_info_headimg = $evaluate->info->member_info_headimg ?? getenv('STORAGE_DOMAIN').'/images/avatar_default.png';
- $evaluate->info->member_info_nickname ?? substr($evaluate->member->member_mobile,-4).'会员';
- }
- if (!empty($evaluate->goods)){
- $evaluate->goods->goods_cover = getenv('STORAGE_DOMAIN').$evaluate->goods->goods_cover;
- $evaluate->goods->goods_cover_origin = str_replace('/thumb','',$evaluate->goods->goods_cover);
- }
- return json_success('success',$evaluate);
- }
- /**
- * 添加
- * @param Request $request
- * @return Response
- * @throws BusinessException
- */
- public function insert(Request $request): Response
- {
- try {
- $data = $this->insertInput($request);
- $this->doInsert($data);
-
- } catch (BusinessException $customException) {
- return json_fail($customException->getMessage());
- } catch (\Exception $e) {
- dump($e->getMessage());
- return json_fail('数据写入失败');
- }
- return json_success('success');
- }
- /**
- * 插入前置方法
- * @param Request $request
- * @return array
- * @throws BusinessException
- */
- protected function insertInput(Request $request): array
- {
- $data = $this->inputFilter($request->post());
- if (!empty($data['goods_evaluate_json'])){
- $goodsEvaluateJson = explode(',',$data['goods_evaluate_json']);
- foreach ($goodsEvaluateJson as $key => $item){
- $goodsEvaluateJson[$key] = str_replace(getenv('STORAGE_DOMAIN'), '', $item);
- }
- $data['goods_evaluate_json'] = json_encode(['images'=>$goodsEvaluateJson]);
- }
-
- return $data;
- }
- /**
- * 更新
- * @param Request $request
- * @return Response
- * @throws BusinessException
- */
- public function update(Request $request): Response
- {
- try {
- [$id, $data] = $this->updateInput($request);
- $this->doUpdate($id, $data);
- } catch (BusinessException $e) {
- return json_fail($e->getMessage());
- } catch (\Exception $e) {
- dump($e->getMessage());
- return json_fail('数据更新失败');
- }
- return json_success('success');
- }
- /**
- * 更新前置方法
- * @param Request $request
- * @return array
- * @throws BusinessException
- */
- protected function updateInput(Request $request): array
- {
- $primary_key = $this->model->getKeyName();
- $id = $request->post($primary_key);
- $data = $this->inputFilter($request->post());
- $model = $this->model->find($id);
- if (!$model) {
- throw new BusinessException('记录不存在', 2);
- }
- if (!empty($data['goods_evaluate_json'])){
- $goodsEvaluateJson = explode(',',$data['goods_evaluate_json']);
- foreach ($goodsEvaluateJson as $key => $item){
- $goodsEvaluateJson[$key] = str_replace(getenv('STORAGE_DOMAIN'), '', $item);
- }
- $data['goods_evaluate_json'] = json_encode(['images'=>$goodsEvaluateJson]);
- }
- unset($data[$primary_key]);
- return [$id, $data];
- }
- }
|