package org.jeecg.modules.school.controller;


import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.school.dto.SchoolPlanBatchMasterDTO;
import org.jeecg.modules.school.entity.SchoolPlanBatchMaster;
import org.jeecg.modules.school.service.ISchoolPlanBatchMasterService;
import org.jeecg.modules.school.vo.SchoolPlanBatchMasterVO;
import org.springframework.web.bind.annotation.*;

import org.jeecg.common.system.base.controller.JeecgController;

import java.util.Date;

/**
 * <p>
 * 作业计划批次管理表 前端控制器
 * </p>
 *
 * @author hkl
 * @since 2023-07-24
 */
@RestController
@RequestMapping("/school/master")
@Api(tags = "作业计划批次管理")
public class SchoolPlanBatchMasterController extends JeecgController<SchoolPlanBatchMaster, ISchoolPlanBatchMasterService> {
    /**
     * 作业计划批次管理列表分页查询
     *
     * @param pageNo
     * @param pageSize
     * @param dto
     * @return
     */
    @AutoLog(value = "作业计划批次管理列表分页查询")
    @ApiOperation(value = "作业计划批次管理列表分页查询", notes = "作业计划批次管理列表分页查询")
    @GetMapping(value = "/list")
    public Result<IPage<SchoolPlanBatchMasterVO>> queryPageList(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                                @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                                SchoolPlanBatchMasterDTO dto) {
        Page<SchoolPlanBatchMasterVO> pageData = new Page<>(pageNo, pageSize);
        pageData = this.service.queryPageList(pageData, dto);
        return Result.OK(pageData);
    }

    @AutoLog(value = "作业计划批次管理新增或者编辑")
    @ApiOperation(value = "作业计划批次管理新增或者编辑", notes = "作业计划批次管理新增或者编辑")
    @PostMapping(value = "/saveOrUpdate")
    @ResponseBody
    public Result<String> saveOrUpdate(@RequestBody SchoolPlanBatchMaster schoolPlanBatchMaster) {
        //获取当前登录人信息
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String realname = user.getRealname();
        schoolPlanBatchMaster.setUpdateTime(new Date());
        schoolPlanBatchMaster.setDelFlag("0");
        schoolPlanBatchMaster.setUpdateBy(realname);
        if (ObjectUtil.isEmpty(schoolPlanBatchMaster.getId())) {
            this.service.save(schoolPlanBatchMaster);
        } else {
            this.service.updateById(schoolPlanBatchMaster);
        }
        return Result.OK("操作成功!");
    }

    /**
     * 删除作业计划批次管理数据
     * @param id
     * @return
     */
    @AutoLog(value = "删除作业计划批次管理数据")
    @ApiOperation(value = "删除作业计划批次管理数据", notes = "删除作业计划批次管理数据")
    @GetMapping(value = "/delete")
    public Result<String> delete(@ApiParam(name = "作业计划批次管理id", required = true) String id) {
        //删除动静态几何尺寸数据
        LambdaUpdateWrapper<SchoolPlanBatchMaster> update = Wrappers.lambdaUpdate();
        update.set(SchoolPlanBatchMaster::getDelFlag, "1");
        update.eq(SchoolPlanBatchMaster::getId, id);
        this.service.update(update);
        return Result.OK("删除成功!");
    }
}
