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.Scholarship;
import org.dromara.ygpassport.domain.StudentList;
import org.dromara.ygpassport.domain.dto.StuScholarshipDto;
import org.dromara.ygpassport.listener.ScholarshipImportListener;
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.ScholarshipVo;
import org.dromara.ygpassport.domain.bo.ScholarshipBo;
import org.dromara.ygpassport.service.IScholarshipService;
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/scholarship")
public class ScholarshipController extends BaseController {

    private final IScholarshipService scholarshipService;

    /**
     * 查询奖学金获评记录列表 old
     */
    @SaCheckPermission("ygpassport:scholarship:list")
    @GetMapping("/list")
    public TableDataInfo<ScholarshipVo> list(ScholarshipBo bo, PageQuery pageQuery) {
        return scholarshipService.queryPageList(bo, pageQuery);
    }

    /**
     * 查询奖学金获评记录列表 new
     */
    @SaCheckPermission("ygpassport:scholarship:list")
    @GetMapping("/listNew")
    public IPage<StuScholarshipDto> listNew(PageQuery pageQuery, StuScholarshipDto stuScholarshipDto) {
        return scholarshipService.selectJoinListPage(new Page<>(pageQuery.getPageNum(), pageQuery.getPageSize()),StuScholarshipDto.class,
            MPJWrappers.<Scholarship>lambdaJoin()
                .selectAll(Scholarship.class)
                .select(StudentList::getStuName)
                .leftJoin(StudentList.class, StudentList :: getStuNo, Scholarship:: getStuNo));
    }

    /**
     * 导出奖学金获评记录列表
     */
    @SaCheckPermission("ygpassport:scholarship:export")
    @Log(title = "奖学金获评记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ScholarshipBo bo, HttpServletResponse response) {
        List<ScholarshipVo> list = scholarshipService.queryList(bo);
        ExcelUtil.exportExcel(list, "奖学金获评记录", ScholarshipVo.class, response);
    }

    /**
     * 导入奖学金获评数据
     *
     * @param file          导入文件
     * @param updateSupport 是否更新已存在数据
     */
    @Log(title = "奖学金获评", businessType = BusinessType.IMPORT)
    @SaCheckPermission("ygpassport:scholarship:import")
    @PostMapping(value = "/importScholarshipData", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> importData(@RequestPart("file") MultipartFile file, boolean updateSupport) throws Exception {
        ScholarshipImportListener listener = new ScholarshipImportListener(updateSupport);
        ExcelResult<ScholarshipVo> result = ExcelUtil.importExcel(file.getInputStream(), ScholarshipVo.class, listener);
        return R.ok(result.getAnalysis());
    }

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

    /**
     * 获取奖学金获评记录详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("ygpassport:scholarship:query")
    @GetMapping("/{id}")
    public R<ScholarshipVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(scholarshipService.queryById(id));
    }

    /**
     * 新增奖学金获评记录
     */
    @SaCheckPermission("ygpassport:scholarship:add")
    @Log(title = "奖学金获评记录", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ScholarshipBo bo) {
        return toAjax(scholarshipService.insertByBo(bo));
    }

    /**
     * 修改奖学金获评记录
     */
    @SaCheckPermission("ygpassport:scholarship:edit")
    @Log(title = "奖学金获评记录", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ScholarshipBo bo) {
        return toAjax(scholarshipService.updateByBo(bo));
    }

    /**
     * 删除奖学金获评记录
     *
     * @param ids 主键串
     */
    @SaCheckPermission("ygpassport:scholarship:remove")
    @Log(title = "奖学金获评记录", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(scholarshipService.deleteWithValidByIds(List.of(ids), true));
    }
}
