package com.ruoyi.gysl.controller;


import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.gysl.entity.BuildingInformation;
import com.ruoyi.gysl.entity.request.ZwIdPageReq;
import com.ruoyi.gysl.service.BasicInformationService;
import com.ruoyi.gysl.service.BuildingInformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.Serializable;
import java.util.List;

/**
 * 建筑信息表(BuildingInformation)表控制层
 *
 * @author makejava
 * @since 2025-02-24 11:10:03
 */
@Api(tags = "建筑信息表")
@RestController
@RequestMapping("/gysl/buildingInformation")
public class BuildingInformationController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private BuildingInformationService buildingInformationService;

    /**
     * 基本信息
     */
    @Resource
    private BasicInformationService basicInformationService;
    /**
     * 根据项目id查询建筑信息列表
     *
     * @param zwIdPageReq 查询实体
     * @return 所有数据
     */
    @GetMapping("/page")
    @ApiOperation(value = "根据项目id查询建筑信息列表",response = BuildingInformation.class )
    public AjaxResult selectAll(@Valid ZwIdPageReq zwIdPageReq) {
        basicInformationService.testXmId(zwIdPageReq.getXmId());
        return success(buildingInformationService.selectAll(zwIdPageReq));
    }


    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "通过主键查询单条数据",response = BuildingInformation.class )
    public AjaxResult selectOne(@PathVariable Serializable id) {
        BuildingInformation byId = buildingInformationService.getById(id);
        basicInformationService.testXmId(byId.getXmId());
        return success(byId);
    }

    /**
     * 新增数据
     *
     * @param buildingInformation 实体对象
     * @return 新增结果
     */
    @PostMapping("/add")
    @ApiOperation("新增数据")
    public AjaxResult insert(@RequestBody BuildingInformation buildingInformation) {
        basicInformationService.testXmId(buildingInformation.getXmId());
        return success(buildingInformationService.save(buildingInformation));
    }

    /**
     * 修改数据
     *
     * @param buildingInformation 实体对象
     * @return 修改结果
     */
    @PostMapping("/edit")
    @ApiOperation("修改数据")
    public AjaxResult update(@RequestBody BuildingInformation buildingInformation) {
        basicInformationService.testXmId(buildingInformation.getXmId());
        return success(buildingInformationService.updateById(buildingInformation));
    }

    /**
     * 删除数据
     *
     * @param id 主键
     * @return 删除结果
     */
    @ApiOperation("删除数据")
    @DeleteMapping("/delete")
    public AjaxResult delete(@RequestParam("idList") Long id) {
        return success(buildingInformationService.removeById(id));
    }

    /**
     * 导入项目建筑信息
     */
    @ApiOperation(value = "导入项目建筑信息")
    @PostMapping(value = "/importProjectBuilding", consumes = "multipart/form-data")
    public AjaxResult importTemplateProject(@RequestPart("file") MultipartFile file,@RequestParam Long xmId) throws Exception {
        ExcelUtil<BuildingInformation> util = new ExcelUtil<>(BuildingInformation.class);
        List<BuildingInformation> proList = util.importExcel(file.getInputStream());
        StringBuilder successMsg = new StringBuilder();
        if (proList == null || proList.isEmpty()) {
            throw new ServiceException("项目建筑信息导入数据不能为空");
        } else {
            proList.forEach(x->{
                x.setXmId(xmId);
            });
            buildingInformationService.saveBatch(proList);
            successMsg.append("导入成功");
        }
        return AjaxResult.success(successMsg);
    }

    /**
     * 根据条件导出所有楼栋信息
     */
    @ApiOperation(value = "根据条件导出所有楼栋信息")
    @PostMapping(value = "/export")
    public void export(HttpServletResponse response, @Valid ZwIdPageReq zwIdPageReq) throws Exception {
        List<BuildingInformation> filteredList = buildingInformationService.selectAll(zwIdPageReq);
        ExcelUtil<BuildingInformation> util = new ExcelUtil<>(BuildingInformation.class);
        util.exportExcel(response, filteredList, "楼栋信息");
    }

    /**
     * 模板信息
     */
    @ApiOperation("导出建筑信息模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<BuildingInformation> util = new ExcelUtil<>(BuildingInformation.class);
        util.importTemplateExcel(response, "建筑信息模板");
    }
}

