package com.ruoyi.project.business.controller;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.business.domain.TcProjectEquipment;
import com.ruoyi.project.business.domain.vo.project.ProjectEquipmentReq;
import com.ruoyi.project.business.domain.vo.project.ProjectSaveOrUpdateEquipmentReq;
import com.ruoyi.project.business.service.ITcProjectEquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 项目设备信息Controller
 *
 * @author ruoyi
 * @date 2024-06-28
 */
@RestController
@RequestMapping("/business/equipment")
public class TcProjectEquipmentController extends BaseController {
    @Autowired
    private ITcProjectEquipmentService tcProjectEquipmentService;

    /**
     * 查询项目设备信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ProjectEquipmentReq req) {
        startPage();
        List<TcProjectEquipment> list = tcProjectEquipmentService.selectTcProjectEquipmentList(req);
        TableDataInfo dataTable = getDataTable(list);
        // 合计
        Map<String, Object> data = tcProjectEquipmentService.selectTotal(req);
        dataTable.setData(data);
        return dataTable;
    }

    /**
     * 查询项目设备信息列表
     */
    @GetMapping("/list-project")
    public TableDataInfo listProject(ProjectEquipmentReq req) {
        startPage();
        List<TcProjectEquipment> list = tcProjectEquipmentService.selectTcProjectEquipmentListProject(req);
        TableDataInfo dataTable = getDataTable(list);
        // 合计
        Map<String, Object> data = tcProjectEquipmentService.selectTotalProject(req);
        dataTable.setData(data);
        return dataTable;
    }


    /**
     * 导出项目设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:equipment:export')")
    @Log(title = "项目设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProjectEquipmentReq req) {
        List<TcProjectEquipment> list = tcProjectEquipmentService.selectTcProjectEquipmentList(req);
        ExcelUtil<TcProjectEquipment> util = new ExcelUtil<TcProjectEquipment>(TcProjectEquipment.class);
        util.exportExcel(response, list, "项目设备信息数据");
    }

    /**
     * 获取项目设备信息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(tcProjectEquipmentService.selectTcProjectEquipmentById(id));
    }

    /**
     * 新增项目设备信息
     */
    @Log(title = "项目设备信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TcProjectEquipment tcProjectEquipment) {
        return toAjax(tcProjectEquipmentService.insertTcProjectEquipment(tcProjectEquipment));
    }

    /**
     * 修改项目设备信息
     */
    @Log(title = "项目设备信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TcProjectEquipment tcProjectEquipment) {
        return toAjax(tcProjectEquipmentService.updateTcProjectEquipment(tcProjectEquipment));
    }

    /**
     * 删除项目设备信息
     */
    @Log(title = "项目设备信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(tcProjectEquipmentService.deleteTcProjectEquipmentByIds(ids));
    }


    /**
     * 修改项目设备信息
     */
    @Log(title = "项目设备信息", businessType = BusinessType.UPDATE)
    @PutMapping("saveOrUpdate")
    public AjaxResult saveOrUpdateEquipment(@RequestBody ProjectSaveOrUpdateEquipmentReq req) {
        return toAjax(tcProjectEquipmentService.saveOrUpdateEquipment(req));
    }
}
