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.BasicInformation;
import com.ruoyi.gysl.entity.PlanInformation;
import com.ruoyi.gysl.entity.request.BasicInformationPageReq;
import com.ruoyi.gysl.entity.request.ZwIdPageReq;
import com.ruoyi.gysl.service.BasicInformationService;
import com.ruoyi.gysl.service.PlanInformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 规划信息(PlanInformation)表控制层
 *
 * @author makejava
 * @since 2025-02-24 14:21:57
 */
@Api(tags = "规划信息")
@RestController
@RequestMapping("/gysl/planInformation")
public class PlanInformationController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private PlanInformationService planInformationService;

    @Resource
    private BasicInformationService basicInformationService;

    /**
     * 根据项目id查询规划信息
     *
     * @param zwIdPageReq 查询实体
     * @return 所有数据
     */
    @GetMapping("/selectAll")
    @ApiOperation("根据项目id查询规划信息")
    public AjaxResult selectAll(@Valid ZwIdPageReq zwIdPageReq) {
        basicInformationService.testXmId(zwIdPageReq.getXmId());
        return success(planInformationService.selectAll(zwIdPageReq));
    }


    /**
     * 新增数据
     *
     * @param planInformation 实体对象
     * @return 新增结果
     */
    @PreAuthorize("@ss.hasAnyRoles('admin,common,gov')")
    @PostMapping("/add")
    @ApiOperation("新增数据")
    public AjaxResult insert(@RequestBody PlanInformation planInformation) {

        PlanInformation one = planInformationService.lambdaQuery()
                .eq(PlanInformation::getXmId, planInformation.getId()).one();
        if (one!=null){
            throw new ServiceException("该项目已有规划!");
        }else {
            planInformationService.save(planInformation);
        }
        return success();
    }

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

    /**
     * 根据条件导出规划信息
     */
    @ApiOperation(value = "根据条件导出规划信息")
    @PostMapping(value = "/export")
    public void export(HttpServletResponse response, @Valid ZwIdPageReq zwIdPageReq) throws Exception {
        List<PlanInformation> a1 = new ArrayList<>();
        PlanInformation filteredList = planInformationService.selectAll(zwIdPageReq);
        a1.add(filteredList);
        ExcelUtil<PlanInformation> util = new ExcelUtil<>(PlanInformation.class);
        util.exportExcel(response, a1, "基本信息");
    }
}

