package com.zh.share.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zh.share.common.R;
import com.zh.share.dto.MessageDto;
import com.zh.share.entity.*;
import com.zh.share.service.CategoryService;
import com.zh.share.service.MessageService;
import com.zh.share.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.stream.Collectors;
@RestController
@RequestMapping("/message")
@Slf4j
public class MessageController {
@Autowired
private MessageService messageService;
@Autowired
private UserService userService;
@Autowired
private CategoryService categoryService;
@PostMapping
public R<String> insert(@RequestBody Message message){
log.info("message:{}",message);
messageService.save(message);
return R.success("新增信息成功");
}
/**
* 获取信息
* @param type
* @param categoryId
* @return
*/
@RequestMapping("/list")
public R<List<Message>> list (@RequestParam String type,@RequestParam String categoryId){
List<Message> list = messageService.listMessage(type,categoryId);
return R.success(list);
}
/**
* 获取我的信息列表
* @param type
* @param
* @return
*/
@RequestMapping("/myList")
public R<List<Message>> myList (@RequestParam String type){
List<Message> list = messageService.listMyMessage(type);
return R.success(list);
}
/**
* 获取我联系的信息列表
* @param
* @return
*/
@RequestMapping("/myCallList")
public R<List<Message>> myCallList (){
List<Message> list = messageService.listMyCall();
return R.success(list);
}
/**
* 分页查找信息数据
* @param page
* @param pageSize
* @param name
* @return
*/
@GetMapping("/page")
public R<Page> page(int page, int pageSize, String name){
Page<Message> messagePage = new Page<>(page,pageSize);
Page<MessageDto> messageDtoPage = new Page<>();
LambdaQueryWrapper<Message> wrapper = new LambdaQueryWrapper<>();
wrapper.like(name!=null,Message::getName,name);
Page<Message> messages = messageService.page(messagePage, wrapper);
BeanUtils.copyProperties(messages,messageDtoPage,"records");
List<Message> records = messagePage.getRecords();
List<MessageDto> list = records.stream().map(item -> {
MessageDto messageDto = new MessageDto();
BeanUtils.copyProperties(item, messageDto);
Long userId = messageDto.getCreateUser();
User user = userService.getById(userId);
messageDto.setUserPhone(user.getPhone());
String dtoName = messageDto.getName();
LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(Category::getName, dtoName);
Category category = categoryService.getOne(queryWrapper);
LambdaQueryWrapper<Category> queryWrapper2 = new LambdaQueryWrapper<>();
queryWrapper2.eq(Category::getId, category.getParent());
Category categoryParent = categoryService.getOne(queryWrapper2);
messageDto.setCategoryName(categoryParent.getName());
return messageDto;
}).collect(Collectors.toList());
messageDtoPage.setRecords(list);
return R.success(messageDtoPage);
}
/**
* 修改状态
* @param id
* @return
*/
@GetMapping("/updateStatus")
public R<String> updateStatus(String id){
messageService.updateStatus(id);
return R.success("修改成功");
}
/**
* 根据id删除
* @param id
* @return
*/
@DeleteMapping("/deleteById")
public R<String> deleteById(String id){
Long msgId = Long.valueOf(id);
messageService.myRemoveById(msgId);
return R.success("删除成功");
}
/**
* 管理员后台单个删除和批量删除
* @param ids
* @return
*/
@DeleteMapping
public R<String> deleteByIds(@RequestParam List<Long> ids){
messageService.removeByIds(ids);
return R.success("删除成功");
}
@GetMapping("getById")
public Message getById(String id){
Message message = messageService.getById(id);
return message;
}
}