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

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.apache.shiro.authz.annotation.RequiresPermissions;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.jeecg.modules.stm.audit.entity.AuditRiskEvent;
import org.jeecg.modules.stm.audit.entity.AuditRiskScenario;
import org.jeecg.modules.stm.audit.service.IAuditRiskEventService;
import org.jeecg.modules.stm.audit.service.IAuditRiskScenarioService;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

 /**
 * @Description: 风险场景
 * @Author: jeecg-boot
 * @Date:   2024-08-26
 * @Version: V1.0
 */
  
@RestController
@RequestMapping("/audit/auditRiskScenario")
@Slf4j
public class AuditRiskScenarioController extends JeecgController<AuditRiskScenario, IAuditRiskScenarioService> {
	@Autowired
	private IAuditRiskScenarioService auditRiskScenarioService;
	 @Autowired
	 private IAuditRiskEventService auditRiskEventService;
	/**
	 * 分页列表查询
	 *
	 * @param auditRiskScenario
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "风险场景-分页列表查询")
	  
	@GetMapping(value = "/list")
	public Result<IPage<AuditRiskScenario>> queryPageList(AuditRiskScenario auditRiskScenario,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<AuditRiskScenario> queryWrapper = QueryGenerator.initQueryWrapper(auditRiskScenario, req.getParameterMap());
		Page<AuditRiskScenario> page = new Page<AuditRiskScenario>(pageNo, pageSize);
		IPage<AuditRiskScenario> pageList = auditRiskScenarioService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @GetMapping(value = "/allList")
	 public Result<List<AuditRiskScenario>> allList(AuditRiskScenario auditRiskScenario,HttpServletRequest req) {
		 QueryWrapper<AuditRiskScenario> queryWrapper = QueryGenerator.initQueryWrapper(auditRiskScenario, req.getParameterMap());
		 List<AuditRiskScenario> list = auditRiskScenarioService.list(queryWrapper);
		 return Result.OK(list);
	 }

	/**
	 * @param auditRiskScenario
	 * @return
	 */
	@AutoLog(value = "风险场景-添加")
	  
	@PostMapping(value = "/add")
	public Result<AuditRiskScenario> add(@RequestBody AuditRiskScenario auditRiskScenario) {
		auditRiskScenario.setHasChild(false);
		auditRiskScenarioService.save(auditRiskScenario);
		AuditRiskScenario pRisk = auditRiskScenarioService.getById(auditRiskScenario.getPid());
		if(pRisk!=null) {
			pRisk.setHasChild(true);
			auditRiskScenarioService.updateById(pRisk);
		}
		return Result.OK(auditRiskScenario);
	}
	/**
	 *  编辑
	 *
	 * @param auditRiskScenario
	 * @return
	 */
	@AutoLog(value = "风险场景-编辑")
	  
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody AuditRiskScenario auditRiskScenario) {
		auditRiskScenarioService.updateById(auditRiskScenario);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "风险场景-通过id删除")
	  
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		QueryWrapper<AuditRiskEvent> queryWrapper = new QueryWrapper();
		queryWrapper.eq("scid",id);
		List<AuditRiskEvent> auditRiskEventList = auditRiskEventService.list(queryWrapper);
		if(auditRiskEventList!=null&&auditRiskEventList.size()>0){
			for(AuditRiskEvent auditRiskEvent : auditRiskEventList){
				auditRiskEvent.setScid(null);
			}
			auditRiskEventService.updateBatchById(auditRiskEventList);
		}
		auditRiskScenarioService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "风险场景-批量删除")
	  
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        List<String> idList = Arrays.asList(ids.split(","));

		QueryWrapper<AuditRiskEvent> queryWrapper = new QueryWrapper();
		queryWrapper.in("scid",idList);
		List<AuditRiskEvent> auditRiskEventList = auditRiskEventService.list(queryWrapper);
		if(auditRiskEventList!=null&&auditRiskEventList.size()>0){
			for(AuditRiskEvent auditRiskEvent : auditRiskEventList){
				auditRiskEvent.setScid(null);
			}
			auditRiskEventService.updateBatchById(auditRiskEventList);
		}

		this.auditRiskScenarioService.removeByIds(idList);
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "风险场景-通过id查询")
	  
	@GetMapping(value = "/queryById")
	public Result<AuditRiskScenario> queryById(@RequestParam(name="id",required=true) String id) {
		AuditRiskScenario auditRiskScenario = auditRiskScenarioService.getById(id);
		if(auditRiskScenario==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(auditRiskScenario);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param auditRiskScenario
    */
    @RequiresPermissions("audit:audit_risk_scenario:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AuditRiskScenario auditRiskScenario) {
        return super.exportXls(request, auditRiskScenario, AuditRiskScenario.class, "风险场景");
    }

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

}
