package com.seashellhouse.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.seashellhouse.controller.utils.R;
import com.seashellhouse.pojo.Book;
import com.seashellhouse.pojo.House;
import com.seashellhouse.pojo.Rent;
import com.seashellhouse.pojo.Second;
import com.seashellhouse.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.sql.Wrapper;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
@CrossOrigin
@RestController
@RequestMapping("/books")
public class BookController {
@Autowired
private IBookService bookService;
@Autowired
private IHouseService houseService;
@Autowired
private ISecondService secondService;
@Autowired
private IRentService rentService;
@Autowired
private IUserService userService;
// @GetMapping("/getAll")
// public R getAll() {
// QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
// queryWrapper.last("order by `time2` desc");
// List<Book> books = bookService.list(queryWrapper);
// return new R(books);
// }
//分页查询全部订单信息
@GetMapping("/{currentPage}/{pageSize}")
public R getAll(@PathVariable Integer currentPage, @PathVariable Integer pageSize, Book book) {
IPage<Book> pageBook = bookService.getPage(currentPage, pageSize);
return new R(null != pageBook, pageBook);
}
//获取指定订单信息
@GetMapping("/{id}")
public R getRentById(@PathVariable Integer id) {
System.out.println("is me!!!~");
Book book = bookService.getById(id);
//获取该订单其他东西什么的
//xxx
return new R(book);
}
//获取指定用户的订单列表
@GetMapping("/only/{id}")
public R getByUserId(@PathVariable Integer id) {
System.out.println("进来了");
QueryWrapper<Book> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("user",id.toString());
queryWrapper.last("order by `time2` desc");
List<Book> books = bookService.list(queryWrapper);
Date dNow = new Date( );
SimpleDateFormat ft = new SimpleDateFormat ("yyyy年MM月dd日 hh:mm");
String timeNow = ft.format(dNow);
for (Book book : books) {
if(book.getTime1().compareTo(timeNow)<0 && book.getState().equals("1")){
//大于等于就是没到时间,小于0就是过了时间)
//过期设置为已完成
book.setState("2");
bookService.updateById(book);//更新状态
}
}
return new R(true, books);
}
//生成订单
@PostMapping("/addNew")
public R newBook(@RequestBody Map<String, String> map) {
Book book = new Book();
book.setUser(map.get("user"));//预约人id
book.setType(map.get("type"));//服务类型
book.setTarget(map.get("target"));//房源
String date = map.get("date").substring(0,10)+"日";
date=date.replaceFirst("-","年");
date=date.replaceFirst("-","月");
String theTime = map.get("time");
String tt=date+" "+theTime;
book.setTime1(tt);//预约时间
//生成指定格式的时间
Date dNow = new Date( );
SimpleDateFormat ft = new SimpleDateFormat ("yyyy年MM月dd日 hh:mm");
String time = ft.format(dNow);
book.setTime2(time);//订单生成时间
book.setMoney(map.get("money")+".00");//预约金额
book.setName(map.get("name"));
book.setPhone(map.get("phone"));
String title = null;//从房子中获取
String img = null;//从房子中获取
if (book.getType().equals("1")){
//新房
House house = houseService.getById(book.getTarget());
book.setTitle(house.getBlock());
book.setImg(house.getCover());
}else if(book.getType().equals("2")){
//二手房
Second second = secondService.getById(book.getTarget());
book.setTitle(second.getBlock());
book.setImg(second.getCover());
}else if(book.getType().equals("3")){
//租房
Rent rent = rentService.getById(book.getTarget());
book.setTitle(rent.getBlock());
book.setImg(rent.getCover());
}
return new R(bookService.save(book));
}
//退费并取消订单
//1.账户充值 2.修改订单状态(state1变为3,state2变为2)
@PostMapping("/cancel")
public R cancelBook(@RequestBody Map<String, String> map){
String id = map.get("id");
Book book = bookService.getById(id);
System.out.println("得到的是:"+book);
//退钱
userService.recharge(book.getUser(),Float.parseFloat(book.getMoney()));
//修改订单状态
book.setState("3");book.setState2("2");
Boolean b = bookService.updateById(book);
return (new R(b));
}
//取消订单
@PostMapping("/cancelBooks")
public R cancelBooks(@RequestBody Map<String, String> map){
String id = map.get("id");
Book book = bookService.getById(id);
//修改订单状态
book.setState("3");
return (new R(bookService.updateById(book)));
}
//退款
@PostMapping("/refund")
public R refund(@RequestBody Map<String, String> map){
String id = map.get("id");
String user = map.get("user");
String money = map.get("money");
userService.recharge(user,Float.parseFloat(money));
Book book = bookService.getById(id);
book.setState2("2");
return (new R(bookService.updateById(book)));
}
}