//
// 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.Mode;
import com.luozhihui.project.mapper.ModeMapper;
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.GetMapping;
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 ModeController {
    @Resource
    ModeMapper modeMapper;

    public ModeController() {
    }

    @PostMapping({"/AllModepage"})
    public totalResponse AllBrand(@RequestParam int pageNumber, @RequestParam int pageSize) {
        try {
            List<Mode> mode = this.modeMapper.FindAllModepage((pageNumber - 1) * pageSize, pageSize);
            int total = this.modeMapper.countMode();
            return mode != null ? new totalResponse(mode, 200, "查询成功", total) : new totalResponse((Object)null, 404, "未找到该套餐", (Integer)null);
        } catch (Exception var5) {
            Exception e = var5;
            e.printStackTrace();
            return new totalResponse(e.getMessage(), 500, "服务器内部错误", (Integer)null);
        }
    }

    @PostMapping({"/AllMode"})
    public ApiResponse AllBrandpage() {
        try {
            List<Mode> mode = this.modeMapper.FindAllMode();
            return mode != null ? new ApiResponse(mode, 200, "查询成功") : new ApiResponse((Object)null, 404, "未找到套餐");
        } catch (Exception var2) {
            Exception e = var2;
            e.printStackTrace();
            return new ApiResponse(e.getMessage(), 500, "服务器内部错误");
        }
    }

    @PostMapping({"/FindModeById"})
    public ApiResponse FindBrandById(@RequestParam("id") Long id) {
        try {
            Mode mode = (Mode)this.modeMapper.FindModedById(String.valueOf(id));
            return mode != null ? new ApiResponse(mode, 200, "查询成功") : new ApiResponse((Object)null, 404, "未找到品牌信息");
        } catch (Exception var3) {
            Exception e = var3;
            e.printStackTrace();
            return new ApiResponse((Object)null, 500, "服务器内部错误：" + e.getMessage());
        }
    }

    @CrossOrigin
    @GetMapping({"/getPid"})
    public ApiResponse FindModeByParkingId(Mode mode) {
        try {
            List<Mode> modes = this.modeMapper.FindModedByParkingId(mode.getParking_id());
            return modes != null && !modes.isEmpty() ? new ApiResponse(modes, 200, "查询成功") : new ApiResponse((Object)null, 404, "未找到品牌信息");
        } catch (Exception var3) {
            Exception e = var3;
            e.printStackTrace();
            return new ApiResponse((Object)null, 500, "服务器内部错误：" + e.getMessage());
        }
    }

    @CrossOrigin
    @PostMapping({"/AddMode"})
    public ApiResponse AddBrand(@RequestBody Mode mode) {
        try {
            this.modeMapper.addMode(mode);
            return new ApiResponse((Object)null, 200, "添加成功");
        } catch (Exception var3) {
            Exception e = var3;
            e.printStackTrace();
            return new ApiResponse(e.getMessage(), 500, "服务器内部错误");
        }
    }

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

            while(var6.hasNext()) {
                Long carId = (Long)var6.next();
                boolean isDeleted = this.modeMapper.DeleteModeById(String.valueOf(carId));
                if (isDeleted) {
                    ++deletedCount;
                }
            }

            if (deletedCount == carIds.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({"/upmodeinfo"})
    public ApiResponse updateCar(@RequestBody Mode mode) {
        System.out.println(mode.getModeId());
        System.out.println("-------------------------------");

        try {
            Mode existingBrandinfo = this.modeMapper.ModeId(mode.getModeId());
            System.out.println(existingBrandinfo);
            System.out.println(mode.getModeId());
            if (existingBrandinfo != null) {
                existingBrandinfo.setBetMoney(mode.getBetMoney());
                existingBrandinfo.setDescription(mode.getDescription());
                existingBrandinfo.setDayMoney(mode.getDayMoney());
                existingBrandinfo.setHourMoney(mode.getHourMoney());
                existingBrandinfo.setModeId(mode.getModeId());
                existingBrandinfo.setMonthMoney(mode.getMonthMoney());
                existingBrandinfo.setParking_id(mode.getParking_id());
                existingBrandinfo.setModeName(mode.getModeName());
                existingBrandinfo.setModeImg(mode.getModeImg());
                existingBrandinfo.setModeNum(mode.getModeNum());
                this.modeMapper.update(existingBrandinfo);
                return new ApiResponse((Object)null, 200, "品牌信息更新成功");
            } else {
                return new ApiResponse((Object)null, 404, "品牌不存在");
            }
        } catch (Exception var3) {
            Exception e = var3;
            return new ApiResponse(e.getMessage(), 500, "更新品牌信息发生异常");
        }
    }
}
