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

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.modules.stm.problem.service.IStProblemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import org.jeecg.modules.stm.audit.service.IAuditActRelOthersService;
import org.jeecg.modules.stm.problem.entity.StProblem;
import org.jeecg.modules.stm.problem.entity.StProblemCa;
import org.jeecg.modules.stm.problem.vo.StProblemVo;

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


/**
 * @Description: st_problem
 * @Author: yzh
 * @Date:   2023-03-04
 * @Version: V1.0
 */
  
@RestController
@RequestMapping("/problem/stProblem")
@Slf4j
public class StProblemController extends JeecgController<StProblem, IStProblemService> {
	@Autowired
	private IStProblemService stProblemService;
	@Autowired
	private IAuditActRelOthersService stActRelOthersService;
	/**
	 * 分页列表查询
	 *
	 * @param stProblem
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "问题库-分页列表查询")
	  
	@GetMapping(value = "/list")
	public Result<IPage<StProblem>> queryPageList(StProblem stProblem,
												  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												  HttpServletRequest req) {
		IPage<StProblem> pageList = stProblemService.defPage(stProblem,pageNo,pageSize,req.getParameterMap());
		return Result.OK(pageList);
	}

	  
	@GetMapping(value = "/listca")
	public Result<IPage<StProblemVo>> queryCaPageList(StProblemVo stProblemVo,
													  @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
													  @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
													  HttpServletRequest req) {

		if(!ObjectUtils.isEmpty(stProblemVo.getProblemExample())){
			stProblemVo.setProblemExample(stProblemVo.getProblemExample().replace("*",""));
		}
		if(!ObjectUtils.isEmpty(stProblemVo.getProblemItem())){
			stProblemVo.setProblemItem(stProblemVo.getProblemItem().replace("*",""));
		}
		if(!ObjectUtils.isEmpty(stProblemVo.getLableCodes())){
			stProblemVo.setCodes(stProblemVo.getLableCodes().split(","));
		}
//		if(stProblemVo.getId()!=null) {
//			stProblemVo.setId(stProblemVo.getId());
//		}

		Page<StProblem> page = new Page<StProblem>(pageNo, pageSize);
		IPage<StProblemVo> pageList = stProblemService.queryProblemWithPage(page, stProblemVo,req.getParameterMap());
		return Result.OK(pageList);
	}


	/**
	 *   添加
	 *
	 * @param stProblem
	 * @return
	 */
	@AutoLog(value = "问题库-添加")
	  
	@RequiresPermissions("problem:st_problem:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody StProblem stProblem) {
		stProblemService.save(stProblem);
		StProblemCa stProblemCa = new StProblemCa();
		stProblemCa.setProblemid(stProblem.getId());
		stProblemCa.setCaNo(stProblem.getCaNo());
		stProblemService.insertProblemCa(stProblemCa);
		return Result.OK("添加成功！");
	}

	@PostMapping(value = "/addProblemCa")
	public Result<String> addPromblemCa(@RequestBody StProblemCa stProblemCa) {;
		String caNos = stProblemCa.getCaNo();
		if(caNos!=null && !caNos.equals("")) {
			if(stProblemCa.getProblemid()!=null) {
				stProblemService.deleteProblemCa(stProblemCa);
				String[] tmpAry = caNos.split(",");
				for (int i = 0; i < tmpAry.length; i++) {
					StProblemCa problemCa = new StProblemCa();
					problemCa.setCaNo(tmpAry[i]);
					problemCa.setProblemid(stProblemCa.getProblemid());
					stProblemService.insertProblemCa(problemCa);
				}
			}
		}
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param stProblem
	 * @return
	 */
	@AutoLog(value = "问题库-编辑")
	  
	@RequiresPermissions("problem:st_problem:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody StProblem stProblem) {
		stProblemService.updateById(stProblem);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "问题库-通过id删除")
	  
	@RequiresPermissions("problem:st_problem:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		stProblemService.removeById(id);
		return Result.OK("删除成功!");
	}

	@AutoLog(value = "问题库-通过id删除合规问题关系")
	  
	@RequiresPermissions("problem:st_problem:delete")
	@DeleteMapping(value = "/deletecompliane")
	public Result<String> deletecompliane(@RequestParam(name="id",required=true) String id) {
		stProblemService.deletecompliane(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "问题库-批量删除")
	  
	@RequiresPermissions("problem:st_problem:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.stProblemService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "问题库-通过id查询")
	  
	@GetMapping(value = "/queryById")
	public Result<StProblem> queryById(@RequestParam(name="id",required=true) String id) {
		StProblem stProblem = stProblemService.getById(id);
		if(stProblem==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(stProblem);
	}

	/**
	 * 导出excel
	 *
	 * @param request
	 * @param stProblem
	 */
	@RequiresPermissions("problem:st_problem:exportXls")
	@RequestMapping(value = "/exportXls")
	public ModelAndView exportXls(HttpServletRequest request, StProblem stProblem) {
		return super.exportXls(request, stProblem, StProblem.class, "st_problem");
	}

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

	  
	@GetMapping(value = "/listByProjectId")
	public Result<IPage<StProblem>> queryPageListByProjectId(StProblem stProblem,
				 @RequestParam(name="projectCreId") Integer projectCreId,
				 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
				 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
				 HttpServletRequest req) {

		IPage<StProblem> pageList = stProblemService.queryPageListByProjectId(stProblem,projectCreId,pageNo,pageSize,req.getParameterMap());
		return Result.OK(pageList);
	}


}
