package org.jeecg.modules.system.app.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 io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
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.common.system.query.QueryGenerator;
import org.jeecg.modules.system.app.dto.AppTeachingTimeDTO;
import org.jeecg.modules.system.app.entity.AppTeachingTime;
import org.jeecg.modules.system.app.service.IAppTeachingTimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

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

/**
 * @Description: 教学/非教学维护表
 * @Author: jeecg-boot
 * @Date:   2025-07-10
 * @Version: V1.0
 */
@Tag(name="教学/非教学维护表")
@RestController
@RequestMapping("/app/appTeachingTime")
@Slf4j
public class AppTeachingTimeController extends JeecgController<AppTeachingTime, IAppTeachingTimeService> {
	@Autowired
	private IAppTeachingTimeService appTeachingTimeService;
	
	/**
	 * 分页列表查询
	 *
	 * @param appTeachingTime
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "教学/非教学维护表-分页列表查询")
	@Operation(summary="教学/非教学维护表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<AppTeachingTime>> queryPageList(AppTeachingTime appTeachingTime,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {


        QueryWrapper<AppTeachingTime> queryWrapper = QueryGenerator.initQueryWrapper(appTeachingTime, req.getParameterMap());
		Page<AppTeachingTime> page = new Page<AppTeachingTime>(pageNo, pageSize);
		IPage<AppTeachingTime> pageList = appTeachingTimeService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param appTeachingTime
	 * @return
	 */
	@AutoLog(value = "教学/非教学维护表-添加")
	@Operation(summary="教学/非教学维护表-添加")
	@RequiresPermissions("org.jeecg.modules.system.app:nm_teaching_time:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody AppTeachingTime appTeachingTime) {
		appTeachingTimeService.save(appTeachingTime);

		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param appTeachingTime
	 * @return
	 */
	@AutoLog(value = "教学/非教学维护表-编辑")
	@Operation(summary="教学/非教学维护表-编辑")
	@RequiresPermissions("org.jeecg.modules.system.app:nm_teaching_time:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody AppTeachingTime appTeachingTime) {
		appTeachingTimeService.updateById(appTeachingTime);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "教学/非教学维护表-通过id删除")
	@Operation(summary="教学/非教学维护表-通过id删除")
	@RequiresPermissions("org.jeecg.modules.system.app:nm_teaching_time:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		appTeachingTimeService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "教学/非教学维护表-批量删除")
	@Operation(summary="教学/非教学维护表-批量删除")
	@RequiresPermissions("org.jeecg.modules.system.app:nm_teaching_time:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.appTeachingTimeService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "教学/非教学维护表-通过id查询")
	@Operation(summary="教学/非教学维护表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<AppTeachingTime> queryById(@RequestParam(name="id",required=true) String id) {
		AppTeachingTime appTeachingTime = appTeachingTimeService.getById(id);
		if(appTeachingTime==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(appTeachingTime);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param appTeachingTime
    */
    @RequiresPermissions("org.jeecg.modules.system.app:nm_teaching_time:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, AppTeachingTime appTeachingTime) {
        return super.exportXls(request, appTeachingTime, AppTeachingTime.class, "教学/非教学维护表");
    }

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

	 /**
	  * 通过tenantId查询
	  *
	  * @param orgCode
	  * @return
	  */
	 //@AutoLog(value = "教学/非教学维护表-通过id查询")
	 @Operation(summary="教学/非教学维护表-通过tenantId查询当前月与下月-教学/非教学")
	 @GetMapping(value = "/queryByOrgCode")
	 public Result<List<AppTeachingTimeDTO>> queryByOrgCode(@RequestParam(name = "orgCode", required = true) String orgCode) {
		 return Result.OK(appTeachingTimeService.queryListByOrgCode(orgCode));
	 }

}
