package com.taais.mf.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.taais.common.core.core.domain.CommonResult;
import com.taais.common.excel.utils.ExcelUtil;
import com.taais.common.log.annotation.Log;
import com.taais.common.log.enums.BusinessType;
import com.taais.common.orm.core.page.TableDataInfo;
import com.taais.common.web.annotation.RepeatSubmit;
import com.taais.common.web.core.BaseController;
import com.taais.mf.domain.bo.MfStudentBo;
import com.taais.mf.domain.vo.MfStudentVo;
import com.taais.mf.service.IMfStudentService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 学生信息表Controller
 *
 * @author wgk
 * 2024-01-05
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/mf/student")
public class MfStudentController extends BaseController {
    @Resource
    private IMfStudentService mfStudentService;

    /**
     * 查询学生信息表列表
     */
    @SaCheckPermission("mf:student:list")
    @GetMapping("/list")
    public TableDataInfo<MfStudentVo> list(MfStudentBo mfStudentBo) {
        return mfStudentService.selectPage(mfStudentBo);
    }

    /**
     * 导出学生信息表列表
     */
    @SaCheckPermission("mf:student:export")
    @Log(title = "学生信息表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MfStudentBo mfStudentBo) {
        List<MfStudentVo> list = mfStudentService.selectList(mfStudentBo);
        ExcelUtil.exportExcel(list, "学生信息表", MfStudentVo.class, response);
    }

    /**
     * 获取学生信息表详细信息
     */
    @SaCheckPermission("mf:student:query")
    @GetMapping(value = "/{studentId}")
    public CommonResult<MfStudentVo> getInfo(@PathVariable Long studentId) {
        return CommonResult.success(mfStudentService.selectById(studentId));
    }

    /**
     * 新增学生信息表
     */
    @SaCheckPermission("mf:student:add")
    @Log(title = "学生信息表", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping
    public CommonResult<Void> add(@Validated @RequestBody MfStudentBo mfStudentBo) {
        boolean inserted = mfStudentService.insert(mfStudentBo);
        if (!inserted) {
            return CommonResult.fail("新增学生信息表记录失败！");
        }
        return CommonResult.success();
    }

    /**
     * 修改学生信息表
     */
    @SaCheckPermission("mf:student:edit")
    @Log(title = "学生信息表", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping
    public CommonResult<Void> edit(@Validated @RequestBody MfStudentBo mfStudentBo) {
        Boolean updated = mfStudentService.update(mfStudentBo);
        if (!updated) {
            return CommonResult.fail("修改学生信息表记录失败!");
        }
        return CommonResult.success();
    }

    /**
     * 删除学生信息表
     */
    @SaCheckPermission("mf:student:remove")
    @Log(title = "学生信息表", businessType = BusinessType.DELETE)
    @DeleteMapping("/{studentIds}")
    public CommonResult<Void> remove(@PathVariable Long[] studentIds) {
        boolean deleted = mfStudentService.deleteByIds(studentIds);
        if (!deleted) {
            return CommonResult.fail("删除学生信息表记录失败!");
        }
        return CommonResult.success();
    }
}
