package com.xiaoniu.subsystem.business.controller;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.xiaoniu.common.core.result.R;
import com.xiaoniu.common.core.utils.BeanUtil;
import com.xiaoniu.common.core.utils.poi.ExcelUtil;
import com.xiaoniu.common.log.annotation.Log;
import com.xiaoniu.common.log.enums.BusinessType;
import com.xiaoniu.subsystem.business.domain.AdmissionExamination;
import com.xiaoniu.subsystem.business.domain.ExamQuestion;
import com.xiaoniu.subsystem.business.domain.GraduationAssessment;
import com.xiaoniu.subsystem.business.domain.dto.GraduationAssessmentDTO;
import com.xiaoniu.subsystem.business.domain.dto.GraduationAssessmentPageDTO;
import com.xiaoniu.subsystem.business.domain.vo.AdmissionExaminationVO;
import com.xiaoniu.subsystem.business.domain.vo.GraduationAssessmentPageVO;
import com.xiaoniu.subsystem.business.service.IGraduationAssessmentService;
import com.xiaoniu.subsystem.system.domain.Unit;
import com.xiaoniu.subsystem.system.service.IUnitService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static com.xiaoniu.common.security.utils.SecurityUtils.getUnitId;
import static com.xiaoniu.common.security.utils.SecurityUtils.getUserId;

/**
 * <p>
 * 结业考核表 前端控制器
 * </p>
 *
 * @author haoxuyang
 * @since 2024-12-23 05:36:11
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/graduationAssessment")
@Tag(name = "结业考核表", description = "结业考核")
public class GraduationAssessmentController {
    private final IUnitService unitService;
    private final IGraduationAssessmentService graduationAssessmentService;



    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "详情", description = "传入examinationId")
    public R<GraduationAssessment> detail(@Parameter(description = "主键", required = true) @RequestParam Long assessmentId) {
        GraduationAssessment detail = graduationAssessmentService.getById(assessmentId);
        return R.data(detail);
    }
    /**
     * 分页
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "分页", description = "传入GraduationAssessmentPageDTO")
    public R<IPage<GraduationAssessmentPageVO>> page(@Validated GraduationAssessmentPageDTO dto) {
        IPage<GraduationAssessment> pages = graduationAssessmentService.page(new Page<>(dto.getPageNum(), dto.getPageSize()),
                new LambdaQueryWrapper<GraduationAssessment>()
                        .orderByDesc(GraduationAssessment::getCreateTime)
                        .eq(dto.getFlightId() != null,GraduationAssessment::getFlightId,dto.getFlightId())
                        .like(dto.getMainUnitName() != null,GraduationAssessment::getMainUnitName,dto.getMainUnitName())
                        .like(StringUtils.isNotBlank(dto.getUserName()), GraduationAssessment::getUserName, dto.getUserName())
                        .like(StringUtils.isNotBlank(dto.getIdCard()), GraduationAssessment::getIdCard, dto.getIdCard())
                        .like(StringUtils.isNotBlank(dto.getSendingUnitName()), GraduationAssessment::getSendingUnitName, dto.getSendingUnitName())
                        .ge(dto.getTheoreticalAchievement1() != null,GraduationAssessment::getTheoreticalAchievement,dto.getTheoreticalAchievement1())
                        .le(dto.getTheoreticalAchievement2() != null,GraduationAssessment::getTheoreticalAchievement,dto.getTheoreticalAchievement2())
                        .ge(dto.getActualPerformance1() != null,GraduationAssessment::getActualPerformance,dto.getActualPerformance1())
                        .le(dto.getActualPerformance2() != null,GraduationAssessment::getActualPerformance,dto.getActualPerformance2())
                        .ge(dto.getTotalScore1() != null,GraduationAssessment::getTotalScore,dto.getTotalScore1())
                        .le(dto.getTotalScore2() != null,GraduationAssessment::getTotalScore,dto.getTotalScore2())
                        .eq(StringUtils.isNotBlank(dto.getEvaluationOrder()),GraduationAssessment::getEvaluationOrder,dto.getEvaluationOrder())
                        .eq(StringUtils.isNotBlank(dto.getIsFinishSchool()),GraduationAssessment::getIsFinishSchool,dto.getIsFinishSchool())
                        .eq(StringUtils.isNotBlank(dto.getYear()),GraduationAssessment::getYear,dto.getYear())

        );

        IPage<GraduationAssessmentPageVO> copy = BeanUtil.copy(pages, GraduationAssessmentPageVO.class);
        return R.data(copy);
    }

    /**
     * 新增
     */
    @PostMapping("/save")
    @ApiOperationSupport(order = 4)
    @Operation(summary = "新增", description = "传入GraduationAssessmentDTO")
    public R save(@Valid @RequestBody GraduationAssessmentDTO dto) {
        Unit unit = unitService.getById(getUnitId());
        GraduationAssessment copy = BeanUtil.copy(dto, GraduationAssessment.class)
                .setCreateDept(unit.getUnitId())
                .setCreateUser(getUserId())
                .setCreateTime(new Date());
        List<GraduationAssessment> one = graduationAssessmentService.list(new LambdaQueryWrapper<GraduationAssessment>()
                .eq(GraduationAssessment::getFlightId,copy.getFlightId())
                .eq(GraduationAssessment::getUserName,copy.getUserName())
                .eq(GraduationAssessment::getIdCard,copy.getIdCard()));
        if (CollectionUtils.isNotEmpty(one)) {
            return R.fail("数据已存在");
        }
        return R.status(graduationAssessmentService.save(copy));
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperationSupport(order = 5)
    @Operation(summary = "修改", description = "传入GraduationAssessmentDTO")
    public R update(@Valid @RequestBody GraduationAssessmentDTO dto) {
        GraduationAssessment copy = BeanUtil.copy(dto, GraduationAssessment.class)
                .setUpdateUser(getUserId())
                .setUpdateTime(new Date());
        return R.status(graduationAssessmentService.updateById(copy));
    }

    /**
     * 删除
     */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 7)
    @Operation(summary = "删除", description = "传入ids")
    public R remove(@Parameter(description = "主键集合", required = true) @RequestParam String ids) {
        //List<AdmissionExamination> list = admissionExaminationService.listByIds(Arrays.stream(ids.split(",")).toList());
        return R.status(graduationAssessmentService.removeByIds(Arrays.stream(ids.split(",")).toList()));
    }

    /**
     * 导出
     */
    @GetMapping("/export")
    @ApiOperationSupport(order = 3)
    @Log(title = "导出", businessType = BusinessType.EXPORT)
    @Operation(summary = "导出", description = "传入GraduationAssessmentPageDTO")
    public void export(HttpServletResponse response, @Validated GraduationAssessmentPageDTO dto) {
        List<GraduationAssessment> list = new ArrayList<>();
        if (StringUtils.isNotBlank(dto.getIds())) {
            list.addAll(graduationAssessmentService.list(new LambdaQueryWrapper<GraduationAssessment>()
                    .orderByDesc(GraduationAssessment::getCreateTime)
                    .in(StringUtils.isNotBlank(dto.getIds()), GraduationAssessment::getAssessmentId,
                            Arrays.stream(dto.getIds().split(",")).toList())));
        } else {
            list.addAll(graduationAssessmentService.list(new LambdaQueryWrapper<GraduationAssessment>()
                    .orderByDesc(GraduationAssessment::getCreateTime)
                    .eq(dto.getFlightId() != null,GraduationAssessment::getFlightId,dto.getFlightId())
                    .like(dto.getMainUnitName() != null,GraduationAssessment::getMainUnitName,dto.getMainUnitName())
                    .like(StringUtils.isNotBlank(dto.getUserName()), GraduationAssessment::getUserName, dto.getUserName())
                    .like(StringUtils.isNotBlank(dto.getIdCard()), GraduationAssessment::getIdCard, dto.getIdCard())
                    .like(StringUtils.isNotBlank(dto.getSendingUnitName()), GraduationAssessment::getSendingUnitName, dto.getSendingUnitName())
                    .ge(dto.getTheoreticalAchievement1() != null,GraduationAssessment::getTheoreticalAchievement,dto.getTheoreticalAchievement1())
                    .le(dto.getTheoreticalAchievement2() != null,GraduationAssessment::getTheoreticalAchievement,dto.getTheoreticalAchievement2())
                    .ge(dto.getActualPerformance1() != null,GraduationAssessment::getActualPerformance,dto.getActualPerformance1())
                    .le(dto.getActualPerformance2() != null,GraduationAssessment::getActualPerformance,dto.getActualPerformance2())
                    .ge(dto.getTotalScore1() != null,GraduationAssessment::getTotalScore,dto.getTotalScore1())
                    .le(dto.getTotalScore2() != null,GraduationAssessment::getTotalScore,dto.getTotalScore2())
                    .eq(StringUtils.isNotBlank(dto.getEvaluationOrder()),GraduationAssessment::getEvaluationOrder,dto.getEvaluationOrder())
                    .eq(StringUtils.isNotBlank(dto.getIsFinishSchool()),GraduationAssessment::getIsFinishSchool,dto.getIsFinishSchool())
                    .eq(StringUtils.isNotBlank(dto.getYear()),GraduationAssessment::getYear,dto.getYear())
            ));
        }
        ExcelUtil<GraduationAssessment> excelUtil = new ExcelUtil<>(GraduationAssessment.class);
        excelUtil.exportExcel(response, list, "结业考核数据");
    }

    /**
     * 导入
     */
    @PostMapping("/import")
    @ApiOperationSupport(order = 9)
    @Log(title = "结业考核导入", businessType = BusinessType.IMPORT)
    @Operation(summary = "导入")
    public R importData(MultipartFile file,
                        @Parameter(description = "是否支持更新", required = true)
                        @RequestParam boolean updateSupport) throws Exception {
        ExcelUtil<GraduationAssessment> util = new ExcelUtil<>(GraduationAssessment.class);
        List<GraduationAssessment> list = util.importExcel(file.getInputStream());
        return R.success(graduationAssessmentService.importData(list, updateSupport));
    }

    /**
     * 模版导入
     */
    @GetMapping("/importTemplate")
    @ApiOperationSupport(order = 10)
    @Operation(summary = "导入模版下载")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<GraduationAssessment> util = new ExcelUtil<>(GraduationAssessment.class);
        util.importTemplateExcel(response, "结业考核导入模版");
    }


}
