package com.ruoyi.web.controller.map.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.map.domain.Model;
import com.ruoyi.map.service.IModelService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 模型Controller
 * 
 * @author ruoyi
 * @date 2023-07-03
 */
@RestController
@RequestMapping("/map/model")
public class ModelController extends BaseController
{
    @Autowired
    private IModelService modelService;

    @Value("${ruoyi.profile}")
    private String upLoadPath;

    /**
     * 查询模型列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Model model)
    {
        startPage();
        List<Model> list = modelService.selectModelList(model);
        return getDataTable(list);
    }

    /**
     * 导出模型列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, Model model)
    {
        List<Model> list = modelService.selectModelList(model);
        ExcelUtil<Model> util = new ExcelUtil<Model>(Model.class);
        util.exportExcel(response, list, "模型数据");
    }

    /**
     * 获取模型详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Integer id)
    {
        return success(modelService.selectModelById(id));
    }

    /**
     * 新增模型
     */
    @PostMapping
    public AjaxResult add(@RequestBody Model model)
    {
        if (model.getAddress() == null)
            model.setAddress(System.getProperty("user.dir").split("\\\\")[0] + upLoadPath + "/" + model.getName().split(" ")[1] + "." + model.getName().split(" ")[0].toLowerCase());
        return toAjax(modelService.insertModel(model));
    }

    /**
     * 修改模型
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Model model)
    {
        return toAjax(modelService.updateModel(model));
    }

    /**
     * 删除模型
     */
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Integer[] ids)
    {
        return toAjax(modelService.deleteModelByIds(ids));
    }
}
