package org.jeecg.modules.stm.audit.controller;

import java.util.Arrays;
import java.util.List;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.stm.audit.entity.AuditProjectReportTitle;
import org.jeecg.modules.stm.audit.service.IAuditProjectReportTitleService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;


import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 报告标题
 * @Author: jeecg-boot
 * @Date:   2025-04-25
 * @Version: V1.0
 */

@RestController
@RequestMapping("/audit/auditProjectReportTitle")
@Slf4j
public class AuditProjectReportTitleController extends JeecgController<AuditProjectReportTitle, IAuditProjectReportTitleService> {
	@Autowired
	private IAuditProjectReportTitleService auditProjectReportTitleService;
	
	/**
	 * 分页列表查询
	 *
	 * @param auditProjectReportTitle
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "报告标题-分页列表查询")
	  
	@GetMapping(value = "/list")
	public Result<IPage<AuditProjectReportTitle>> queryPageList(AuditProjectReportTitle auditProjectReportTitle,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<AuditProjectReportTitle> queryWrapper = QueryGenerator.initQueryWrapper(auditProjectReportTitle, req.getParameterMap());
		Page<AuditProjectReportTitle> page = new Page<AuditProjectReportTitle>(pageNo, pageSize);
		IPage<AuditProjectReportTitle> pageList = auditProjectReportTitleService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @GetMapping(value = "/listAll")
	 public Result<List<AuditProjectReportTitle>> listAll(AuditProjectReportTitle auditProjectReportTitle,
			 HttpServletRequest req) {
		 QueryWrapper<AuditProjectReportTitle> queryWrapper = QueryGenerator.initQueryWrapper(auditProjectReportTitle, req.getParameterMap());
		 List<AuditProjectReportTitle> pageList = auditProjectReportTitleService.list(queryWrapper);
		 return Result.OK(pageList);
	 }
	
	/**
	 *   添加
	 *
	 * @param auditProjectReportTitle
	 * @return
	 */
	@AutoLog(value = "报告标题-添加")
	  
	@RequiresPermissions("audit:audit_project_report_title:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody AuditProjectReportTitle auditProjectReportTitle) {
		if(auditProjectReportTitle.getParentId()==null) {
			auditProjectReportTitle.setParentId(0);
		}
		auditProjectReportTitleService.save(auditProjectReportTitle);
		if(auditProjectReportTitle.getParentId()!=null) {
			AuditProjectReportTitle parent = new AuditProjectReportTitle();
			parent.setHasChild(1);
			parent.setId(auditProjectReportTitle.getParentId());
			auditProjectReportTitleService.updateById(parent);
		}
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param auditProjectReportTitle
	 * @return
	 */
	@AutoLog(value = "报告标题-编辑")
	  
	@RequiresPermissions("audit:audit_project_report_title:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody AuditProjectReportTitle auditProjectReportTitle) {
		auditProjectReportTitleService.updateById(auditProjectReportTitle);
		return Result.OK("编辑成功!");
	}

	 @RequestMapping(value = "/batchSaveOrUpdate", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> batchSaveOrUpdate(@RequestBody List<AuditProjectReportTitle> listTitle) {
		 auditProjectReportTitleService.saveBatch(listTitle);
		 return Result.OK("保存成功！");
	 }
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "报告标题-通过id删除")
	  
	@RequiresPermissions("audit:audit_project_report_title:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		auditProjectReportTitleService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "报告标题-批量删除")
	  
	@RequiresPermissions("audit:audit_project_report_title:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.auditProjectReportTitleService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "报告标题-通过id查询")
	  
	@GetMapping(value = "/queryById")
	public Result<AuditProjectReportTitle> queryById(@RequestParam(name="id",required=true) String id) {
		AuditProjectReportTitle auditProjectReportTitle = auditProjectReportTitleService.getById(id);
		if(auditProjectReportTitle==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(auditProjectReportTitle);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param auditProjectReportTitle
    */
    @RequiresPermissions("audit:audit_project_report_title:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AuditProjectReportTitle auditProjectReportTitle) {
        return super.exportXls(request, auditProjectReportTitle, AuditProjectReportTitle.class, "报告标题");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("audit:audit_project_report_title:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, AuditProjectReportTitle.class);
    }

}
