package org.dromara.ygpassport.controller;

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

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.toolkit.MPJWrappers;
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.ArchivesGo;
import org.dromara.ygpassport.domain.StudentList;
import org.dromara.ygpassport.domain.dto.StuArchivesGoDto;
import org.dromara.ygpassport.listener.ArchivesGoImportListener;
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.ArchivesGoVo;
import org.dromara.ygpassport.domain.bo.ArchivesGoBo;
import org.dromara.ygpassport.service.IArchivesGoService;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 档案去向
 *
 * @author Lion Li
 * @date 2024-01-11
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/ygpassport/archivesGo")
public class ArchivesGoController extends BaseController {

    private final IArchivesGoService archivesGoService;

    /**
     * 查询档案去向列表 old
     */
    @SaCheckPermission("ygpassport:archivesGo:list")
    @GetMapping("/list")
    public TableDataInfo<ArchivesGoVo> list(ArchivesGoBo bo, PageQuery pageQuery) {
        return archivesGoService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询档案去向列表 new
     */
    @SaCheckPermission("ygpassport:archivesGo:list")
    @GetMapping("/listNew")
    public IPage<StuArchivesGoDto> listNew(PageQuery pageQuery, StuArchivesGoDto stuArchivesGoDto) {
        return archivesGoService.selectJoinListPage(new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize()),StuArchivesGoDto.class,
            MPJWrappers.<ArchivesGo>lambdaJoin()
                .selectAll(ArchivesGo.class)
                .select(StudentList::getStuName)
                .leftJoin(StudentList.class, StudentList :: getStuNo, ArchivesGo:: getStuNo));
    }

    /**
     * 导出档案去向列表
     */
    @SaCheckPermission("ygpassport:archivesGo:export")
    @Log(title = "档案去向", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ArchivesGoBo bo, HttpServletResponse response) {
        List<ArchivesGoVo> list = archivesGoService.queryList(bo);
        ExcelUtil.exportExcel(list, "档案去向", ArchivesGoVo.class, response);
    }

    /**
     * 导入成绩数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "档案去向", businessType = BusinessType.IMPORT)
    @SaCheckPermission("ygpassport:archivesGo:import")
    @PostMapping(value = "/importarchivesGoData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ArchivesGoImportListener listener = new ArchivesGoImportListener(updateSupport);
        ExcelResult<ArchivesGoVo> result = ExcelUtil.importExcel(file.getInputStream(), ArchivesGoVo.class, listener);
        return R.ok(result.getAnalysis());
    }

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

    /**
     * 获取档案去向详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("ygpassport:archivesGo:query")
    @GetMapping("/{id}")
    public R<ArchivesGoVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(archivesGoService.queryById(id));
    }

    /**
     * 新增档案去向
     */
    @SaCheckPermission("ygpassport:archivesGo:add")
    @Log(title = "档案去向", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ArchivesGoBo bo) {
        return toAjax(archivesGoService.insertByBo(bo));
    }

    /**
     * 修改档案去向
     */
    @SaCheckPermission("ygpassport:archivesGo:edit")
    @Log(title = "档案去向", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ArchivesGoBo bo) {
        return toAjax(archivesGoService.updateByBo(bo));
    }

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