package com.ruoyi.drill.service.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.drill.domain.DrillAnnualEmergency;
import com.ruoyi.drill.mapper.DrillAnnualEmergencyMapper;
import com.ruoyi.drill.service.IDrillAnnualEmergencyService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@Service
@Slf4j
public class DrillAnnualEmergencyServiceImpl extends ServiceImpl<DrillAnnualEmergencyMapper, DrillAnnualEmergency> implements IDrillAnnualEmergencyService {

    @Override
    @Transactional(rollbackFor = {Exception.class})
    public AjaxResult saveAnnualEmergency(Long year, MultipartFile file, Long directorBy) throws RuntimeException {
        Map<String, String> map = new HashMap<>();
        List<DrillAnnualEmergency> userList = null;
        try {
            // 1.解析excel
            ExcelUtil<DrillAnnualEmergency> util = new ExcelUtil<>(DrillAnnualEmergency.class);
            userList = util.importExcel(file.getInputStream());
        } catch (Exception e) {
            log.error("上传年度演习计划解析失败--->", e);
            return AjaxResult.error("解析失败，请刷新后重试！");
        }

        // 2.如果解析数据为空则返回错误信息
        if (CollectionUtils.isEmpty(userList)) {
            return AjaxResult.warn("上传数据为空，请填写");
        }
        long count = 0L;
        // 3.校验数据是否正确
        for (DrillAnnualEmergency dto : userList) {
            // 计划内容
            if (StringUtils.isEmpty(dto.getPlanName())) {
                return AjaxResult.warn("项目内容不能为空");
            }
            int planNameNum = 50;
            if (dto.getPlanName().length() > planNameNum) {
                return AjaxResult.warn("项目内容长度不能超过" + planNameNum + "个字符");
            }
            // 计划月份
            if (Objects.isNull(dto.getPlanMonth())) {
                return AjaxResult.warn("计划月份请填写数字");
            }
            if (dto.getPlanMonth() > 12L || dto.getPlanMonth() <= 0L) {
                return AjaxResult.warn("计划月份应在1-12之间");
            }
            long years = Calendar.getInstance().get(Calendar.YEAR);
            // 演习种类
            if (map.containsKey(dto.getPlanName() + years + dto.getPlanMonth())) {
                return AjaxResult.warn("演习名称:" + dto.getPlanName() + "重复");
            } else {
                map.put(dto.getPlanName() + years + dto.getPlanMonth(), dto.getPlanName());
            }

            dto.setPlanYear(year);
            dto.setDirectorBy(String.valueOf(directorBy));
            dto.setCreateDefault();
            dto.setSortdNum(count);
            count++;
        }
        // 4.根据年份删除历史数据
        this.remove(Wrappers.<DrillAnnualEmergency>lambdaQuery().eq(DrillAnnualEmergency::getPlanYear, year));
        // 5.保存数据并返回
        return this.saveBatch(userList) ? AjaxResult.success() : AjaxResult.error();
    }

}
