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.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.AuditActRelOthers;
import org.jeecg.modules.stm.audit.entity.AuditProblem;
import org.jeecg.modules.stm.audit.service.IAuditActRelOthersService;
import org.jeecg.modules.stm.audit.service.IAuditLabelRelLibService;
import org.jeecg.modules.stm.audit.service.IAuditProblemService;
import org.jeecg.modules.stm.utils.StmConstans;

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

/**
 * @Description: 问题库
 * @Author: jeecg-boot
 * @Date:   2024-08-26
 * @Version: V1.0
 */
  
@RestController
@RequestMapping("/audit/auditProblem")
@Slf4j
public class AuditProblemController extends JeecgController<AuditProblem, IAuditProblemService> {
	@Autowired
	private IAuditProblemService auditProblemService;
	 @Autowired
	 private IAuditLabelRelLibService stLabelRelLibService;
	@Autowired
	private IAuditActRelOthersService stActRelOthersService;
	
	/**
	 * 分页列表查询
	 *
	 * @param auditProblem
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "问题库-分页列表查询")
	  
	@GetMapping(value = "/list")
	public Result<IPage<AuditProblem>> queryPageList(AuditProblem auditProblem,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<AuditProblem> queryWrapper = QueryGenerator.initQueryWrapper(auditProblem, req.getParameterMap());
		Page<AuditProblem> page = new Page<AuditProblem>(pageNo, pageSize);
		IPage<AuditProblem> pageList = auditProblemService.page(page, queryWrapper);
		return Result.OK(pageList);
	}


	@GetMapping(value = "/listAll")
	public Result<List<AuditProblem>> queryPageList(AuditProblem auditProblem,
													 HttpServletRequest req) {
		QueryWrapper<AuditProblem> queryWrapper = QueryGenerator.initQueryWrapper(auditProblem, req.getParameterMap());
		List<AuditProblem> list = auditProblemService.list(queryWrapper);
		return Result.OK(list);
	}

	@GetMapping(value = "/getAllProblems")
	public Result<List<AuditProblem>> getAllProblems(AuditProblem auditProblem,
													 HttpServletRequest req) {
		List<AuditProblem> retList = auditProblemService.list();
		return Result.OK(retList);
	}
	
	/**
	 *   添加
	 *
	 * @param auditProblem
	 * @return
	 */
	@AutoLog(value = "问题库-添加")
	  
	@PostMapping(value = "/add")
	public Result<AuditProblem> add(@RequestBody AuditProblem auditProblem) {
		auditProblemService.save(auditProblem);
		String labels = auditProblem.getLabels();
		Integer tid =  auditProblem.getId();

		if(labels!=null&&!labels.equals("")) {
			stLabelRelLibService.saveLabelRel(labels,tid, StmConstans.Audit_TP_PROBLEM);
		}
		String actIds = auditProblem.getActIds();
		if( actIds!=null && !actIds.equals("")) {
			List<AuditActRelOthers> actRelOthers = new ArrayList<>();
			stActRelOthersService.setActRelations(actRelOthers,actIds,tid,StmConstans.Audit_TP_PROBLEM);
			stActRelOthersService.saveOrUpdateBatch(actRelOthers);

		}
		return Result.OK(auditProblem);
	}


	
	/**
	 *  编辑
	 *
	 * @param auditProblem
	 * @return
	 */
	@AutoLog(value = "问题库-编辑")
	  
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody AuditProblem auditProblem) {
		auditProblemService.updateById(auditProblem);
		String labels = auditProblem.getLabels();
		Integer tid =  auditProblem.getId();
		if(labels!=null) {
			stLabelRelLibService.deleteLabelRel(tid, StmConstans.Audit_TP_PROBLEM);
			if (labels.trim().length() > 0) {
				stLabelRelLibService.saveLabelRel(labels, tid, StmConstans.Audit_TP_PROBLEM);
			}
		}

		String actIds = auditProblem.getActIds();
		if(actIds!=null) {
			stActRelOthersService.deleteActRel(tid.toString(), StmConstans.Audit_TP_PROBLEM);
			if (actIds.trim().length() > 0) {
				List<AuditActRelOthers> actRelOthers = new ArrayList<>();
				stActRelOthersService.setActRelations(actRelOthers,actIds,tid,StmConstans.Audit_TP_PROBLEM);
				stActRelOthersService.saveOrUpdateBatch(actRelOthers);
			}
		}
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "问题库-通过id删除")
	  
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		auditProblemService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "问题库-批量删除")
	  
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.auditProblemService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "问题库-通过id查询")
	  
	@GetMapping(value = "/queryById")
	public Result<AuditProblem> queryById(@RequestParam(name="id",required=true) String id) {
		AuditProblem auditProblem = auditProblemService.getById(id);
		if(auditProblem==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(auditProblem);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param auditProblem
    */
    //@RequiresPermissions("audit:audit_problem:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AuditProblem auditProblem) {
        return super.exportXls(request, auditProblem, AuditProblem.class, "问题库");
    }

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

	private void setActRelations(List<AuditActRelOthers> saveList, String actIds, Integer tid) {
		if(actIds==null||tid==null) return;
		String[] actIdAry = actIds.split(",");
		for(String actId : actIdAry) {
			if(actId==null||actId.equals("")) continue;
			AuditActRelOthers actRel = new AuditActRelOthers();
			actRel.setTp(StmConstans.Audit_TP_CHECK_METHOD);
			actRel.setActId(Integer.valueOf(actId));
			actRel.setTid(tid);
			saveList.add(actRel);
		}

	}


}
