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.StudentInfo;
import com.xiaoniu.subsystem.business.domain.TeacherInfo;
import com.xiaoniu.subsystem.business.domain.dto.TeacherInfoDTO;
import com.xiaoniu.subsystem.business.domain.dto.TeacherInfoPageDTO;
import com.xiaoniu.subsystem.business.domain.vo.AdmissionExaminationVO;
import com.xiaoniu.subsystem.business.domain.vo.TeacherInfoPageVO;
import com.xiaoniu.subsystem.business.domain.vo.TeacherInfoVO;
import com.xiaoniu.subsystem.business.service.ITeacherInfoService;
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 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("/teacher")
@Tag(name = "教员信息统计表", description = "教员信息统计")
public class TeacherInfoController {

    private final IUnitService unitService;
    private final ITeacherInfoService teacherInfoService;



    /**
     * 详情
     */
    @GetMapping("/detail")
    @ApiOperationSupport(order = 1)
    @Operation(summary = "详情", description = "传入teacherId")
    public R<TeacherInfoVO> detail(@Parameter(description = "主键", required = true) @RequestParam Long teacherId) {
        TeacherInfo detail = teacherInfoService.getById(teacherId);
        return R.data(BeanUtil.copy(detail, TeacherInfoVO.class));
    }
    /*
    * 分页
    * */
    @GetMapping("/page")
    @ApiOperationSupport(order=2)
    @Operation(summary = "分页",description="传入TeacherInfoPageDTO")
    public R<IPage<TeacherInfoPageVO>> page(@Validated  TeacherInfoPageDTO dto){
        Page<TeacherInfo> page = teacherInfoService.page(new Page<>(dto.getPageNum(), dto.getPageSize()),
                new LambdaQueryWrapper<TeacherInfo>()
                        .eq(TeacherInfo::getFlightId,dto.getFlightId())
                        .like(StringUtils.isNotBlank(dto.getUserName()), TeacherInfo::getUserName, dto.getUserName())
                        .like(StringUtils.isNotBlank(dto.getNation()), TeacherInfo::getNation, dto.getNation())
                        .eq(StringUtils.isNotBlank(dto.getSex()), TeacherInfo::getSex, dto.getSex())
        );
        IPage<TeacherInfoPageVO> copy = BeanUtil.copy(page,TeacherInfoPageVO.class);
        return R.data(copy);
    }

    /*
    * 新增
    * */
    @PostMapping("/save")
    @ApiOperationSupport(order=4)
    @Operation(summary = "新增",description = "传入TeacherInfoDTO")
    public R save(@Validated @RequestBody TeacherInfoDTO dto){
        TeacherInfo copy = BeanUtil.copy(dto, TeacherInfo.class)
                .setCreateDept(getUnitId())
                .setCreateUser(getUserId())
                .setCreateTime(new Date());
        List<TeacherInfo> list = teacherInfoService.list(new LambdaQueryWrapper<TeacherInfo>()
                .eq(TeacherInfo::getFlightId,dto.getFlightId())
                .eq(TeacherInfo::getIdCard, dto.getIdCard())
        );
        if (CollectionUtils.isNotEmpty(list)){
            return R.fail("教员数据已存在");
        }
        return R.status(teacherInfoService.save(copy));
    }

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

    /*
    * 删除
    * */
    @PostMapping("/remove")
    @ApiOperationSupport(order = 7)
    @Operation(summary = "删除",description = "传入ids")
    public R remove(@Validated String ids){
        return R.status(teacherInfoService.removeByIds(Arrays.stream(ids.split(",")).toList()));
    }

    /**
     * 导出
     */
    @GetMapping("/export")
    @ApiOperationSupport(order = 3)
    @Log(title = "导出", businessType = BusinessType.EXPORT)
    @Operation(summary = "导出", description = "传入TeacherInfoPageDTO")
    public void export(HttpServletResponse response, @Validated TeacherInfoPageDTO dto) {
        List<TeacherInfo> list = new ArrayList<>();
        if (StringUtils.isNotBlank(dto.getIds())) {
            list.addAll(teacherInfoService.list(new LambdaQueryWrapper<TeacherInfo>()
                    .orderByDesc(TeacherInfo::getCreateTime)
                    .in(StringUtils.isNotBlank(dto.getIds()), TeacherInfo::getTeacherId,
                            Arrays.stream(dto.getIds().split(",")).toList())));
        } else {
            list.addAll(teacherInfoService.list(new LambdaQueryWrapper<TeacherInfo>()
                    .orderByDesc(TeacherInfo::getCreateTime)
                    .eq(TeacherInfo::getFlightId,dto.getFlightId())
                    .like(StringUtils.isNotBlank(dto.getUserName()), TeacherInfo::getUserName, dto.getUserName())
                    .like(StringUtils.isNotBlank(dto.getNation()), TeacherInfo::getNation, dto.getNation())
                    .eq(StringUtils.isNotBlank(dto.getSex()), TeacherInfo::getSex, dto.getSex())
            ));
        }
        ExcelUtil<TeacherInfo> excelUtil = new ExcelUtil<>(TeacherInfo.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<TeacherInfo> util = new ExcelUtil<>(TeacherInfo.class);
        List<TeacherInfo> list = util.importExcel(file.getInputStream());
        return R.success(teacherInfoService.importData(list, updateSupport));
    }

    /**
     * 模版导入
     */
    @GetMapping("/importTemplate")
    @ApiOperationSupport(order = 10)
    @Operation(summary = "导入模版下载")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<TeacherInfo> util = new ExcelUtil<>(TeacherInfo.class);
        util.importTemplateExcel(response, "教员信息统计导入模版");
    }

}
