package org.dromara.ygpassport.controller;

import java.util.ArrayList;
import java.util.List;

import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.dromara.common.excel.core.ExcelResult;
import org.dromara.ygpassport.domain.vo.StudentImportVo;
import org.dromara.ygpassport.listener.InternshipsListener;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.web.core.BaseController;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.ygpassport.domain.vo.InternshipsVo;
import org.dromara.ygpassport.domain.bo.InternshipsBo;
import org.dromara.ygpassport.service.IInternshipsService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 实习情况
 *
 * @author Lion Li
 * @date 2024-03-16
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ygpassport/internships")
public class InternshipsController extends BaseController {

    private final IInternshipsService internshipsService;

    /**
     * 查询实习情况列表
     */
    @SaCheckPermission("ygpassport:internships:list")
    @GetMapping("/list")
    public TableDataInfo<InternshipsVo> list(InternshipsBo bo, PageQuery pageQuery) {
        return internshipsService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出实习情况列表
     */
    @SaCheckPermission("ygpassport:internships:export")
    @Log(title = "实习情况", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(InternshipsBo bo, HttpServletResponse response) {
        List<InternshipsVo> list = internshipsService.queryList(bo);
        ExcelUtil.exportExcel(list, "实习情况", InternshipsVo.class, response);
    }

    /**
     * 导入实习记录数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "实习记录信息", businessType = BusinessType.IMPORT)
    @SaCheckPermission("ygpassport:internships:import")
    @PostMapping(value = "/importInternshipsData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        InternshipsListener listener = new InternshipsListener(updateSupport);
        ExcelResult<InternshipsVo> result = ExcelUtil.importExcel(file.getInputStream(), InternshipsVo.class, listener);
        return R.ok(result.getAnalysis());
    }

    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "实习记录信息", InternshipsVo.class, response);
    }

    /**
     * 获取实习情况详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("ygpassport:internships:query")
    @GetMapping("/{id}")
    public R<InternshipsVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(internshipsService.queryById(id));
    }

    /**
     * 新增实习情况
     */
    @SaCheckPermission("ygpassport:internships:add")
    @Log(title = "实习情况", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody InternshipsBo bo) {
        return toAjax(internshipsService.insertByBo(bo));
    }

    /**
     * 修改实习情况
     */
    @SaCheckPermission("ygpassport:internships:edit")
    @Log(title = "实习情况", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody InternshipsBo bo) {
        return toAjax(internshipsService.updateByBo(bo));
    }

    /**
     * 删除实习情况
     *
     * @param ids 主键串
     */
    @SaCheckPermission("ygpassport:internships:remove")
    @Log(title = "实习情况", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(internshipsService.deleteWithValidByIds(List.of(ids), true));
    }
}
