package org.jeecg.modules.stm.plan.main.controller;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.stm.plan.main.entity.StPlanMan;
import org.jeecg.modules.stm.plan.main.service.IStPlanManService;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
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.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;

 /**
 * @Description: 计划编制
 * @Author: jeecg-boot
 * @Date:   2023-07-15
 * @Version: V1.0
 */

@RestController
@RequestMapping("/plan.main/stPlanMan")
@Slf4j
public class StPlanManController extends JeecgController<StPlanMan, IStPlanManService> {
	@Autowired
	private IStPlanManService stPlanManService;
	 @Autowired
	 private ISysDictService iSysDictService;
	
	/**
	 * 分页列表查询
	 *
	 * @param stPlanMan
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "计划编制-分页列表查询")
	  
	@GetMapping(value = "/list")
	public Result<IPage<StPlanMan>> queryPageList(StPlanMan stPlanMan,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		IPage<StPlanMan> pageList = stPlanManService.defPage(stPlanMan, req.getParameterMap(),pageNo,pageSize);
		List<StPlanMan> list = pageList.getRecords();
		if(CollectionUtils.isEmpty(list)){

		}else{
			for(StPlanMan stPlanManben:pageList.getRecords()){
				String tableName = getTableNameByMyBatisPlus();
				if(tableName!=null&&!tableName.equals("")){
					//需要根据 tableName 反查DeployId
					String deployId=iSysDictService.queryDictTextByKeyOfformTableName(tableName);
					stPlanManben.setDeployId(deployId);
				}

			}
		}
		return Result.OK(pageList);
	}

	   
	 @GetMapping(value = "/listflow")
	 public Result<IPage<StPlanMan>> queryPageListflow(StPlanMan stPlanMan,
												   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												   HttpServletRequest req) {
		 stPlanMan.setPlanFlag("1");
		 IPage<StPlanMan> pageList = stPlanManService.defPage(stPlanMan, req.getParameterMap(),pageNo,pageSize);
		 return Result.OK(pageList);
	 }
	
	/**
	 *   添加
	 *
	 * @param stPlanMan
	 * @return
	 */
	@AutoLog(value = "计划编制-添加")
	  
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody StPlanMan stPlanMan) {
		//stPlanMan.setProjectType("0001");
		stPlanMan.setExecDepCode("1678967416676200450");
		stPlanMan.setHeadCode("test1");
		stPlanManService.save(stPlanMan);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param stPlanMan
	 * @return
	 */
	@AutoLog(value = "计划编制-编辑")
	  
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody StPlanMan stPlanMan) {
		stPlanManService.updateById(stPlanMan);
		return Result.OK("编辑成功!");
	}


	 /**
	  *  编辑
	  *
	  * @param stPlanMan
	  * @return
	  */
	 @AutoLog(value = "计划编制-编辑")
	   
	 @RequestMapping(value = "/handle", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> handle(@RequestBody StPlanMan stPlanMan) {
		 stPlanManService.handle(stPlanMan);
		 return Result.OK("操作成功!");
	 }
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "计划编制-通过id删除")
	  
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		stPlanManService.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.stPlanManService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "计划编制-通过id查询")
	  
	@GetMapping(value = "/queryById")
	public Result<StPlanMan> queryById(@RequestParam(name="id",required=true) String id) {
		StPlanMan stPlanMan = stPlanManService.getById(id);
		if(stPlanMan==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(stPlanMan);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param stPlanMan
    */
    @RequiresPermissions("plan.main:st_plan_man:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, StPlanMan stPlanMan) {
        return super.exportXls(request, stPlanMan, StPlanMan.class, "计划编制");
    }

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

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

}
