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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 
 
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
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.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.system.service.ISysDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.jeecg.modules.stm.problem.entity.StProblemCheck;
import org.jeecg.modules.stm.problem.service.IStProblemCheckService;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 问题整改
 * @Author: jeecg-boot
 * @Date:   2025-08-01
 * @Version: V1.0
 */
  
@RestController
@RequestMapping("/problem/stProblemCheck")
@Slf4j
public class StProblemCheckController extends JeecgController<StProblemCheck, IStProblemCheckService> {
	@Autowired
	private IStProblemCheckService stProblemCheckService;
	@Autowired
	private ISysDictService iSysDictService;
	
	/**
	 * 分页列表查询
	 *
	 * @param stProblemCheck
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "问题整改-分页列表查询")
	  
	@GetMapping(value = "/list")
	public Result<IPage<StProblemCheck>> queryPageList(StProblemCheck stProblemCheck,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<StProblemCheck> queryWrapper = QueryGenerator.initQueryWrapper(stProblemCheck, req.getParameterMap());
		Page<StProblemCheck> page = new Page<StProblemCheck>(pageNo, pageSize);
		IPage<StProblemCheck> pageList = stProblemCheckService.page(page, queryWrapper);

		List<StProblemCheck> list = pageList.getRecords();
		if(CollectionUtils.isEmpty(list)){

		}else{
			for(StProblemCheck stPlanMan:pageList.getRecords()){
				String tableName = getTableNameByMyBatisPlus();
				if(tableName!=null&&!tableName.equals("")){

					//需要根据 tableName 反查DeployId
					String deployId=iSysDictService.queryDictTextByKeyOfformTableName(tableName);
					stPlanMan.setDeployId(deployId);

				}

			}
		}

		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param stProblemCheck
	 * @return
	 */
	@AutoLog(value = "问题整改-添加")
	  
	//@RequiresPermissions("problem:st_problem_check:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody StProblemCheck stProblemCheck) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String userid=sysUser.getId();
		String username=sysUser.getUsername();
		stProblemCheck.setCreatedUser(username);
		stProblemCheckService.save(stProblemCheck);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param stProblemCheck
	 * @return
	 */
	@AutoLog(value = "问题整改-编辑")
	  
	//@RequiresPermissions("problem:st_problem_check:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody StProblemCheck stProblemCheck) {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		String userid=sysUser.getId();
		String username=sysUser.getUsername();
		stProblemCheck.setUpdatedUser(username);
		stProblemCheckService.updateById(stProblemCheck);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "问题整改-通过id删除")
	  
	//@RequiresPermissions("problem:st_problem_check:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		stProblemCheckService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "问题整改-批量删除")
	  
	//@RequiresPermissions("problem:st_problem_check:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.stProblemCheckService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "问题整改-通过id查询")
	  
	@GetMapping(value = "/queryById")
	public Result<StProblemCheck> queryById(@RequestParam(name="id",required=true) String id) {
		StProblemCheck stProblemCheck = stProblemCheckService.getById(id);
		if(stProblemCheck==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(stProblemCheck);
	}

    /**
    * 导出excel
    * @param request
    * @param stProblemCheck
    */
    //@RequiresPermissions("problem:st_problem_check:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, StProblemCheck stProblemCheck) {
        return super.exportXls(request, stProblemCheck, StProblemCheck.class, "问题整改");
    }

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


	 /**
	  * 使用 MyBatis-Plus 的 TableInfoHelper 获取表名
	  */
	 public  String getTableNameByMyBatisPlus() {
		 TableInfo tableInfo = TableInfoHelper.getTableInfo(StProblemCheck.class);
		 if (tableInfo != null) {
			 return tableInfo.getTableName();
		 }
		 return null;
	 }

}
