package org.jeecg.modules.applyForm.controller;

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

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.utils.DictEnum;
import org.jeecg.common.utils.DictModelConstant;
import org.jeecg.modules.applyForm.entity.ApplyForm;
import org.jeecg.modules.applyForm.service.IApplyFormService;

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.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: apply_form
 * @Author: jeecg-boot
 * @Date:   2021-08-23
 * @Version: V1.0
 */
@Api(tags="apply_form")
@RestController
@RequestMapping("/applyform")
@Slf4j
public class ApplyFormController extends JeecgController<ApplyForm, IApplyFormService> {
	@Autowired
	private IApplyFormService applyFormService;
	
	/**
	 * 分页列表查询
	 *
	 * @param applyForm
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "apply_form-分页列表查询")
	@ApiOperation(value="apply_form-分页列表查询", notes="apply_form-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(ApplyForm applyForm,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<ApplyForm> queryWrapper = QueryGenerator.initQueryWrapper(applyForm, req.getParameterMap());
		Page<ApplyForm> page = new Page<ApplyForm>(pageNo, pageSize);
		IPage<ApplyForm> pageList = applyFormService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  *  获取表单keys
	  */
	 @AutoLog(value = "apply_form-获取表单keys")
	 @ApiOperation(value="apply_form-获取表单keys", notes="apply_form-获取表单keys")
	 @GetMapping(value = "/formKeys")
	 public Result<?> listFormKeys() {
		 return Result.OK(DictEnum.getDictVOList(DictModelConstant.MODEL_APPLY_CONFIG, DictModelConstant.MODEL_APPLY_CONFIG_FORMKEY));
	 }


	/**
	 *   添加
	 *
	 * @param applyForm
	 * @return
	 */
	@AutoLog(value = "apply_form-添加")
	@ApiOperation(value="apply_form-添加", notes="apply_form-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody ApplyForm applyForm) {
		applyFormService.save(applyForm);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param applyForm
	 * @return
	 */
	@AutoLog(value = "apply_form-编辑")
	@ApiOperation(value="apply_form-编辑", notes="apply_form-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody ApplyForm applyForm) {
		applyFormService.updateById(applyForm);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "apply_form-通过id删除")
	@ApiOperation(value="apply_form-通过id删除", notes="apply_form-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		applyFormService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "apply_form-批量删除")
	@ApiOperation(value="apply_form-批量删除", notes="apply_form-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.applyFormService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "apply_form-通过id查询")
	@ApiOperation(value="apply_form-通过id查询", notes="apply_form-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		ApplyForm applyForm = applyFormService.getById(id);
		if(applyForm == null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(applyForm);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param applyForm
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ApplyForm applyForm) {
        return super.exportXls(request, applyForm, ApplyForm.class, "apply_form");
    }

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

 }
