package cn.cecep.talroad.controller.analysis;

import cn.cecep.talroad.common.core.exception.CommonException;
import cn.cecep.talroad.common.core.utils.poi.ExcelUtil;
import cn.cecep.talroad.common.core.web.controller.BasesController;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.log.annotation.Log;
import cn.cecep.talroad.common.log.enums.BusinessType;
import cn.cecep.talroad.common.security.annotation.RequiresPermissions;
import cn.cecep.talroad.domain.analysis.SRaActionReportsAudit;
import cn.cecep.talroad.service.analysis.ISRaActionReportsAuditService;
import cn.cecep.talroad.vo.analysis.SRaActionReportsAuditVo;
import cn.cecep.talroad.vo.query.analysis.SRaActionReportsAuditQuery;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.security.InvalidParameterException;
import java.util.Arrays;
import java.util.List;


/**
 * * 开发公司：中节能天融科技有限公司
 * 执行报告规范性审核Controller 控制层
 *
 * @author szhpt
 * @date 2023-03-24
 */
@Api(tags = "执行报告规范性审核")
@RestController
@RequestMapping("/audit")
public class SRaActionReportsAuditController extends BasesController {
    @Autowired
    private ISRaActionReportsAuditService sRaActionReportsAuditService;

    /**
     * 查询执行报告规范性审核列表
     */
    @ApiOperation(value = "查询执行报告规范性审核")
    @GetMapping("/list")
    public AjaxResults<IPage<SRaActionReportsAudit>> list(SRaActionReportsAuditQuery sRaActionReportsAudit) {
        Page<SRaActionReportsAudit> page = new Page<>(sRaActionReportsAudit.getPageNum(), sRaActionReportsAudit.getPageSize());
        IPage<SRaActionReportsAudit> list = sRaActionReportsAuditService.page(page, SRaActionReportsAuditQuery.createLambdaQueryWrapper(sRaActionReportsAudit));
        return AjaxResults.success(list);
    }


    /**
     * 导出执行报告规范性审核列表
     */
    @ApiOperation(value = "导出执行报告规范性审核")
    @Log(title = "执行报告规范性审核", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SRaActionReportsAudit sRaActionReportsAudit) {
        List<SRaActionReportsAudit> list = sRaActionReportsAuditService.selectSRaActionReportsAuditList(sRaActionReportsAudit);
        ExcelUtil<SRaActionReportsAudit> util = new ExcelUtil<SRaActionReportsAudit>(SRaActionReportsAudit.class);
        util.exportExcel(response, list, "执行报告规范性审核数据");
    }

    /**
     * 获取执行报告规范性审核详细信息
     */
    @ApiOperation(value = "获取执行报告规范性审核详细信息")
    @PostMapping(value = "/{id}")
    public AjaxResults<SRaActionReportsAudit> getInfo(@PathVariable("id") Integer id) {
        return AjaxResults.success(sRaActionReportsAuditService.getById(id));
    }

    /**
     * 新增执行报告规范性审核
     */
    @ApiOperation(value = "新增执行报告规范性审核")
    @Log(title = "执行报告规范性审核", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody SRaActionReportsAudit sRaActionReportsAudit) {
        return toAjax(sRaActionReportsAuditService.save(sRaActionReportsAudit));
    }

    /**
     * 修改执行报告规范性审核
     */
    @ApiOperation(value = "修改执行报告规范性审核")
    @Log(title = "执行报告规范性审核", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody SRaActionReportsAuditVo sRaActionReportsAuditVo) {
        if (ObjectUtil.isEmpty(sRaActionReportsAuditVo)) {
            throw new InvalidParameterException("参数为空");
        }
        if (ObjectUtil.isEmpty(sRaActionReportsAuditVo.getId())) {
            throw new InvalidParameterException("参数id为空");
        }
        SRaActionReportsAudit sRaActionReportsAudit = sRaActionReportsAuditService.getById(sRaActionReportsAuditVo.getId());
        if (ObjectUtil.isEmpty(sRaActionReportsAudit)) {
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", sRaActionReportsAuditVo.getId()));
        }
        //赋值
        BeanUtil.copyProperties(sRaActionReportsAuditVo, sRaActionReportsAudit);
        return toAjax(sRaActionReportsAuditService.saveOrUpdate(sRaActionReportsAudit));
    }

    /**
     * 删除执行报告规范性审核
     */
    @ApiOperation(value = "删除执行报告规范性审核")
    @RequiresPermissions("talroad:audit:remove")
    @Log(title = "执行报告规范性审核", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public AjaxResults<Boolean> remove(@PathVariable String[] ids) {
        return toAjax(sRaActionReportsAuditService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 执行报告未提交企业列表下载
     */
    @ApiOperation(value = "执行报告未提交企业列表下载")
    @Log(title = "执行报告未提交企业列表下载", businessType = BusinessType.EXPORT)
    @PostMapping("/reportDownload/{reportId}")
    public void reportDownload(@PathVariable("reportId")String reportId ,HttpServletResponse response) {
        sRaActionReportsAuditService.reportDownload(reportId,response);
    }

}
