package com.vctgo.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vctgo.common.core.exception.ServiceException;
import com.vctgo.common.core.utils.poi.ExcelUtil;
import com.vctgo.common.core.web.controller.BaseController;
import com.vctgo.common.core.web.domain.AjaxResult;
import com.vctgo.common.log.annotation.Log;
import com.vctgo.common.log.enums.BusinessType;
import com.vctgo.common.security.annotation.Logical;
import com.vctgo.common.security.annotation.RequiresPermissions;
import com.vctgo.system.domain.SysChildhoodManage;
import com.vctgo.system.service.ISysChildhoodManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * 幼儿管理对象Controller
 *
 * @author wanglonghua
 * @date 2023-10-08
 */
@RestController
@RequestMapping("/childhood")
public class SysChildhoodManageController extends BaseController
{
    @Autowired
    private ISysChildhoodManageService sysChildhoodManageService;

    /**
     * 查询幼儿管理对象分页列表
     */
    @RequiresPermissions("system:childhood:page")
    @GetMapping("/page")
    public AjaxResult page(SysChildhoodManage sysChildhoodManage)
    {
        IPage<SysChildhoodManage> pageList = sysChildhoodManageService.selectSysChildhoodManagePage(sysChildhoodManage);
        return AjaxResult.success(pageList);
    }

    /**
     * 查询幼儿管理对象列表
     */
    @RequiresPermissions("system:childhood:list")
    @GetMapping("/list")
    public AjaxResult list(SysChildhoodManage sysChildhoodManage)
    {
        List<SysChildhoodManage> list = sysChildhoodManageService.selectSysChildhoodManageList(sysChildhoodManage);
        return AjaxResult.success(list);
    }

    /**
     * 导出幼儿管理对象列表
     */
    @RequiresPermissions("system:childhood:export")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysChildhoodManage sysChildhoodManage)
    {
        List<SysChildhoodManage> list = sysChildhoodManageService.selectSysChildhoodManageList(sysChildhoodManage);
        ExcelUtil<SysChildhoodManage> util = new ExcelUtil<SysChildhoodManage>(SysChildhoodManage.class);
        util.exportExcel(response, list, "幼儿管理对象数据");
    }

    /**
     * 获取幼儿管理对象详细信息
     */
    @RequiresPermissions(value = {"system:childhood:query","system:childhood:edit"}, logical = Logical.OR)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sysChildhoodManageService.selectSysChildhoodManageById(id));
    }

    /**
     * 新增幼儿管理对象
     */
    @RequiresPermissions("system:childhood:add")
    @PostMapping
    public AjaxResult add(@RequestBody SysChildhoodManage sysChildhoodManage)
    {
        return toAjax(sysChildhoodManageService.insertSysChildhoodManage(sysChildhoodManage));
    }

    /**
     * 修改幼儿管理对象
     */
    @RequiresPermissions("system:childhood:edit")
    @PutMapping
    public AjaxResult edit(@RequestBody SysChildhoodManage sysChildhoodManage)
    {
        if(sysChildhoodManage.getClassId() != null &&
                ("1".equals(sysChildhoodManage.getInKindergartenStatus()) || "2".equals(sysChildhoodManage.getInKindergartenStatus()))){
            throw new ServiceException("该幼儿已绑定班级，不可退园/不可转园");
        }
        return toAjax(sysChildhoodManageService.updateSysChildhoodManage(sysChildhoodManage));
    }

    /**
     * 逻辑删除幼儿管理对象
     */
    @RequiresPermissions("system:childhood:abandon")
	@PutMapping("/abandon/{ids}")
    public AjaxResult abandon(@PathVariable Long[] ids)
    {
        return toAjax(sysChildhoodManageService.logicalDeleteSysChildhoodManageByIds(ids));
    }

    /**
     * 删除幼儿管理对象
     */
    @RequiresPermissions("system:childhood:remove")
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sysChildhoodManageService.deleteSysChildhoodManageByIds(ids));
    }
}
