package com.ruoyi.web.controller.backstage;


import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ruoyi.framework.web.domain.server.Sys;
import com.ruoyi.system.domain.SysScenicDefault;
import com.ruoyi.system.domain.SysSource;
import com.ruoyi.system.domain.dto.ShowSysSourceListDto;
import com.ruoyi.system.service.ISysScenicDefaultService;
import com.ruoyi.system.service.ISysSourceService;
import com.ruoyi.system.service.impl.SysSourceServiceImpl;
import com.ruoyi.web.controller.app.baidu.GsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 根据文件插入资源位信息
@RestController
@RequestMapping("/backstage/insertSource")
public class insertSource {
    @Autowired
    private ISysSourceService sysSourceService ;

    @Autowired
    private ISysScenicDefaultService sysScenicDefaultService ;

    @Transactional
    @GetMapping("/insertSource")
    public  void it() {




        //-----------------------------------读文件----------------------------------------------------------------------------------------
        String filePath = "C:\\Users\\admin\\Desktop\\项目\\wenlvapp\\ruoyi-admin\\src\\main\\java\\com\\ruoyi\\web\\controller\\backstage\\source\\source_3.txt";
        StringBuilder jsonStringBuilder = new StringBuilder();
        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                jsonStringBuilder.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
//            return null;
        }

        String jsonString = jsonStringBuilder.toString();


        System.out.println(jsonString);


        Gson gson = new Gson();
        Type seatsType = new TypeToken<SeatsContainer>(){}.getType();
        SeatsContainer seatsContainer = gson.fromJson(jsonString, seatsType);

        List<Seat> seats = seatsContainer.getSeats();

        System.out.println("Seats: " + seats.size());

        for(Seat seat : seats){
            System.out.println(seat.getX() + " " + seat.getY() + " " + seat.getStatus() + " " + seat.getArea());
            SysSource sysSource = new SysSource();
            sysSource.setcX(seat.getX());
            sysSource.setcY(seat.getY());
            sysSource.setSourceStatus("0");
            sysSource.setPart(seat.getArea());

            SysScenicDefault sysScenicDefault = sysScenicDefaultService.getScenicDefaultByScenicAndPart("泉城广场", seat.getArea());


            sysSource.setPrice(sysScenicDefault.getPrice());
            sysSource.setCityId(1L);;
            sysSource.setCity("济南");
            sysSource.setScenicId(1L);
            sysSource.setScenic("泉城广场");
            sysSource.setCoordinate(seat.getY()+"-"+ seat.getX());
            sysSource.setWorkAddress("https://img.keaitupian.cn/newupload/11/1668068437206545.jpg");
            sysSource.setScale("L");
//            System.out.println(sysSource);
            sysSourceService.insertSysSource(sysSource);
//            break;
        }

        //-----------------------------------------读文件end--------------------------------------------------------------------------

    }
//    }


}

class SeatsContainer {
    private List<Seat> seats;

    public List<Seat> getSeats() {
        return seats;
    }

    public void setSeats(List<Seat> seats) {
        this.seats = seats;
    }
}

class Seat {
    private int x;
    private int y;
    private int status;
    private String area;

    // Getters and setters
    public int getX() { return x; }
    public void setX(int x) { this.x = x; }
    public int getY() { return y; }
    public void setY(int y) { this.y = y; }
    public int getStatus() { return status; }
    public void setStatus(int status) { this.status = status; }
    public String getArea() { return area; }
    public void setArea(String area) { this.area = area; }

    // Optional: Override toString() for easier printing
    @Override
    public String toString() {
        return "Seat{" +
                "x=" + x +
                ", y=" + y +
                ", status=" + status +
                ", area='" + area + '\'' +
                '}';
    }
}
