package com.schoolinout.web.controller.system;

import com.schoolinout.common.annotation.Log;
import com.schoolinout.common.core.controller.BaseController;
import com.schoolinout.common.core.domain.AjaxResult;
import com.schoolinout.common.core.page.TableDataInfo;
import com.schoolinout.common.enums.BusinessType;
import com.schoolinout.common.utils.poi.ExcelUtil;
import com.schoolinout.system.domain.Parent;
import com.schoolinout.system.domain.dto.ParentDto;
import com.schoolinout.system.service.IParentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 家长管理Controller
 *
 * @author sakura
 * @date 2024-01-09
 */
@RestController
@RequestMapping("/system/parent")
public class ParentController extends BaseController {
    @Autowired
    private IParentService parentService;

    /**
     * 查询家长管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:parent:list')")
    @GetMapping("/list")
    public TableDataInfo list(Parent parent) {
        startPage();
        List<Parent> list = parentService.selectParentList(parent);
        return getDataTable(list);
    }

    /**
     * 导出家长管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:parent:export')")
    @Log(title = "家长管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Parent parent) {
        List<Parent> list = parentService.selectParentList(parent);
        ExcelUtil<Parent> util = new ExcelUtil<Parent>(Parent.class);
        util.exportExcel(response, list, "家长管理数据");
    }

    /**
     * 获取家长管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:parent:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(parentService.selectParentById(id));
    }

    /**
     * 新增家长管理
     */
    @PreAuthorize("@ss.hasPermi('system:parent:add')")
    @Log(title = "家长管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody ParentDto parent) {
        return toAjax(parentService.saveParent(parent));
    }

    /**
     * 修改家长管理
     */
    @PreAuthorize("@ss.hasPermi('system:parent:edit')")
    @Log(title = "家长管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Parent parent) {
        return toAjax(parentService.updateParent(parent));
    }

    /**
     * 删除家长管理
     */
    @PreAuthorize("@ss.hasPermi('system:parent:remove')")
    @Log(title = "家长管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(parentService.deleteParentByIds(ids));
    }
}
