//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.luozhihui.project.controller;

import com.luozhihui.project.common.ApiResponse;
import com.luozhihui.project.common.totalResponse;
import com.luozhihui.project.entity.Order;
import com.luozhihui.project.mapper.OrderShowMapper;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/api"})
@CrossOrigin
public class OrderShowController {
    @Resource
    OrderShowMapper orderShowMapper;

    public OrderShowController() {
    }

    @PostMapping({"/AllOrder"})
    public ApiResponse AllCar() {
        try {
            List<Order> order = this.orderShowMapper.FindAllOrder();
            return order != null ? new ApiResponse(order, 200, "查询成功") : new ApiResponse((Object)null, 404, "未找到订单");
        } catch (Exception var2) {
            Exception e = var2;
            e.printStackTrace();
            return new ApiResponse((Object)null, 500, "服务器内部错误");
        }
    }

    @CrossOrigin
    @PostMapping({"/getorder"})
    public ApiResponse OrderTotal() {
        try {
            List<Order> order = this.orderShowMapper.GetOrder();
            return order != null ? new ApiResponse(order, 200, "查询成功") : new ApiResponse((Object)null, 404, "未找到车辆");
        } catch (Exception var2) {
            Exception e = var2;
            e.printStackTrace();
            return new ApiResponse((Object)null, 500, "服务器内部错误");
        }
    }

    @CrossOrigin
    @PostMapping({"/delete_order"})
    public ApiResponse delete_order(@RequestBody Map<String, Object> requestBody) {
        try {
            String orderIdsParam = (String)requestBody.get("orderIds");
            List<String> orderIdStrings = Arrays.asList(orderIdsParam.split(","));
            List<Long> orderIds = (List)orderIdStrings.stream().map(Long::parseLong).collect(Collectors.toList());
            int deletedCount = 0;
            Iterator var6 = orderIds.iterator();

            while(var6.hasNext()) {
                Long orderId = (Long)var6.next();
                boolean isDeleted = this.orderShowMapper.DeleteOrderById(String.valueOf(orderId));
                if (isDeleted) {
                    ++deletedCount;
                }
            }

            if (deletedCount == orderIds.size()) {
                return new ApiResponse((Object)null, 200, "删除成功");
            } else if (deletedCount == 0) {
                return new ApiResponse((Object)null, 404, "未找到订单信息");
            } else {
                return new ApiResponse((Object)null, 207, "部分订单删除成功");
            }
        } catch (Exception var9) {
            Exception e = var9;
            e.printStackTrace();
            return new ApiResponse((Object)null, 500, "服务器内部错误");
        }
    }

    @PostMapping({"/FindOrderById"})
    public totalResponse findOrderById(@RequestParam Integer userId, @RequestParam int pageNumber, @RequestParam int pageSize) {
        try {
            if (userId == null) {
                return new totalResponse((Object)null, 400, "订单信息不完整", (Integer)null);
            } else {
                List<Order> orders = this.orderShowMapper.findOrdersByUserId(userId, (pageNumber - 1) * pageSize, pageSize);
                int total = this.orderShowMapper.countOrdersByUserId(String.valueOf(userId));
                if (orders != null && !orders.isEmpty()) {
                    ZoneId zoneId = ZoneId.systemDefault();
                    Iterator var7 = orders.iterator();

                    while(var7.hasNext()) {
                        Order o = (Order)var7.next();
                        LocalDateTime outTime;
                        if (o.getInTime() != null) {
                            outTime = o.getInTime().atZone(zoneId).toLocalDateTime();
                            o.setInTime(outTime);
                        }

                        if (o.getOutTime() != null) {
                            outTime = o.getOutTime().atZone(zoneId).toLocalDateTime();
                            o.setOutTime(outTime);
                        }
                    }

                    return new totalResponse(orders, 200, "查询成功", total);
                } else {
                    return new totalResponse((Object)null, 404, "未找到订单", (Integer)null);
                }
            }
        } catch (Exception var10) {
            Exception e = var10;
            e.printStackTrace();
            return new totalResponse((Object)null, 500, "服务器内部错误", (Integer)null);
        }
    }

    @CrossOrigin
    @PostMapping({"/uporderinfo"})
    public ApiResponse updateOrder(@RequestBody Order order) {
        try {
            int orderId = Integer.parseInt(order.getOrderId());
            System.out.println(orderId);
            Order existingOrderinfo = this.orderShowMapper.OrderId(orderId);
            System.out.println(existingOrderinfo);
            if (existingOrderinfo != null) {
                existingOrderinfo.setOrderMoney(order.getOrderMoney());
                existingOrderinfo.setOrderStatus(order.getOrderStatus());
                this.orderShowMapper.update(existingOrderinfo);
                return new ApiResponse((Object)null, 200, "订单信息更新成功");
            } else {
                return new ApiResponse((Object)null, 404, "订单不存在");
            }
        } catch (Exception var4) {
            Exception e = var4;
            return new ApiResponse(e.getMessage(), 500, "更新车辆信息发生异常");
        }
    }
}
