//
// 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.entity.Repair;
import com.luozhihui.project.mapper.RepairShowMapper;
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.RestController;

@RestController
@RequestMapping({"/api"})
@CrossOrigin
public class RepairShowController {
    @Resource
    RepairShowMapper repairShowMapper;

    public RepairShowController() {
    }

    @PostMapping({"/AllRepair"})
    public ApiResponse AllCar() {
        try {
            List<Repair> repair = this.repairShowMapper.FindAllRepair();
            return repair != null ? new ApiResponse(repair, 200, "查询成功") : new ApiResponse((Object)null, 404, "未找到车辆");
        } catch (Exception var2) {
            Exception e = var2;
            e.printStackTrace();
            return new ApiResponse((Object)null, 500, "服务器内部错误：" + e.getMessage());
        }
    }

    @PostMapping({"/AddRepair"})
    public ApiResponse addRepair(@RequestBody Repair repair) {
        try {
            this.repairShowMapper.addRepair(repair);
            this.repairShowMapper.updateCarStatusToRepair(repair.getCarId());
            return new ApiResponse((Object)null, 200, "维修信息添加成功");
        } catch (Exception var3) {
            Exception e = var3;
            e.printStackTrace();
            return new ApiResponse((Object)null, 500, "服务器内部错误");
        }
    }

    @CrossOrigin
    @PostMapping({"/delete_repair"})
    public ApiResponse delete_repairs(@RequestBody Map<String, Object> requestBody) {
        try {
            String repairIdsInt = (String)requestBody.get("repairIds");
            String repairIdsParam = repairIdsInt;
            List<String> repairIdStrings = Arrays.asList(repairIdsParam.split(","));
            List<Long> repairIds = (List)repairIdStrings.stream().map(Long::parseLong).collect(Collectors.toList());
            int deletedCount = 0;
            Iterator var7 = repairIds.iterator();

            while(var7.hasNext()) {
                Long repairId = (Long)var7.next();
                boolean isDeleted = this.repairShowMapper.DeleteRepairById(String.valueOf(repairId));
                if (isDeleted) {
                    ++deletedCount;
                }
            }

            if (deletedCount == repairIds.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 var10) {
            Exception e = var10;
            e.printStackTrace();
            return new ApiResponse(e.getMessage(), 500, "服务器内部错误");
        }
    }

    @PostMapping({"/uprepair"})
    public ApiResponse updateCar(@RequestBody Repair repair) {
        try {
            int repairId = repair.getRepairId();
            System.out.println(repairId);
            Repair existingRepairinfo = this.repairShowMapper.FindRepairById(String.valueOf(repairId));
            System.out.println(existingRepairinfo);
            if (existingRepairinfo != null) {
                existingRepairinfo.setRepairMoney(repair.getRepairMoney());
                existingRepairinfo.setRepairReason(repair.getRepairReason());
                existingRepairinfo.setRepairStatus(repair.getRepairStatus());
                this.repairShowMapper.update(existingRepairinfo);
                if (repair.getRepairStatus() == 1) {
                    this.repairShowMapper.updateCarStatusToAvailable(repair.getCarId());
                    System.out.println("-----------------");
                    System.out.println(repair.getRepairStatus());
                    System.out.println(repair.getRepairStatus());
                    System.out.println(repair.getRepairStatus());
                    System.out.println(repair.getRepairStatus());
                }

                if (repair.getRepairStatus() == 0) {
                    this.repairShowMapper.updateCarStatusToRepair(repair.getCarId());
                    System.out.println("0000000000000000000");
                    System.out.println(repair.getRepairStatus());
                    System.out.println(repair.getRepairStatus());
                    System.out.println(repair.getRepairStatus());
                    System.out.println(repair.getRepairStatus());
                }

                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, "更新车辆信息发生异常");
        }
    }
}
