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.GraduationAssessment;
import com.xiaoniu.subsystem.business.domain.TeacherInfo;
import com.xiaoniu.subsystem.business.domain.TransferStation;
import com.xiaoniu.subsystem.business.domain.dto.TransferStationDTO;
import com.xiaoniu.subsystem.business.domain.dto.TransferStationPageDTO;
import com.xiaoniu.subsystem.business.domain.vo.TransferStationPageVO;
import com.xiaoniu.subsystem.business.service.ITransferStationService;
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("/transferStation")
@Tag(name = "接送站信息统计表", description = "接送站信息统计")
public class TransferStationController {
    private final ITransferStationService transferStationService;
    private final IUnitService unitService;


    /**
     * 分页 考试题库表
     */
    @GetMapping("/page")
    @ApiOperationSupport(order = 2)
    @Operation(summary = "分页", description = "传入TransferStationPageDTO")
    public R<IPage<TransferStationPageVO>> page(@Validated TransferStationPageDTO dto) {
        IPage<TransferStation> pages = transferStationService.page(new Page<>(dto.getPageNum(), dto.getPageSize()),
                new LambdaQueryWrapper<TransferStation>()
                        .orderByDesc(TransferStation::getCreateTime)
                        .like(StringUtils.isNotBlank(dto.getUserName()), TransferStation::getUserName, dto.getUserName())
                        .like(StringUtils.isNotBlank(dto.getSendingUnitName()), TransferStation::getSendingUnitName, dto.getSendingUnitName())
                        .like(StringUtils.isNotBlank(dto.getSendingAddress()),TransferStation::getSendingAddress,dto.getSendingAddress())
                        .like(StringUtils.isNotBlank(dto.getPhonenumber()),TransferStation::getPhonenumber,
                                dto.getPhonenumber())
                        .eq(null != dto.getFlightId(), TransferStation::getFlightId,dto.getFlightId())
                        .eq(StringUtils.isNotBlank(dto.getFlightName()),TransferStation::getFlightName,dto.getFlightName())
        );
        IPage<TransferStationPageVO> copy = BeanUtil.copy(pages, TransferStationPageVO.class);
        return R.data(copy);
    }

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

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

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


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

    @GetMapping("/importTemplate")
    @ApiOperationSupport(order = 10)
    @Operation(summary = "导入模版下载")
    public void importTemplate(HttpServletResponse response) {
        System.out.println("进入导入模版下载");
        ExcelUtil<TransferStation> util = new ExcelUtil<>(TransferStation.class);
        util.importTemplateExcel(response, "接送站信息统计导入模版");
    }
}
