package com.zcloud.fire.check.domain.model;

import cn.hutool.core.collection.CollUtil;
import com.alibaba.cola.exception.BizException;
import com.jjb.saas.framework.auth.utils.AuthContext;
import com.jjb.saas.framework.domain.model.BaseE;
import com.zcloud.gbscommon.excelEntity.FireCheckStandardExcelImportEntity;
import com.zcloud.gbscommon.excelEntity.RiskUnitExcelImportEntity;
import com.zcloud.gbscommon.utils.ExcelUtils;
import com.zcloud.gbscommon.utils.UuidUtil;
import lombok.Data;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotEmpty;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * web-domain
 *
 * @Author zhaokai
 * @Date 2025-11-26 10:12:14
 */
@Data
public class FireCheckStandardE extends BaseE {
    //主键
    private Long id;
    //业务主键id
    private String fireCheckStandardId;
    //企业id
    private Long corpinfoId;
    //消防器材类型
    private String fireDeviceType;
    //消防器材类型名称
    private String fireDeviceTypeName;
    //消防器材名称
    private String deviceName;
    //单位名称
    private String employer;
    //检查项
    private String fireCheckStandardItem;
    //乐观锁
    private Integer version;
    //创建人
    private Long createId;
    //创建人姓名
    private String createName;
    //创建时间
    private LocalDateTime createTime;
    //更新人
    private Long updateId;
    //修改人名称
    private String updateName;
    //更新时间
    private LocalDateTime updateTime;
    //描述
    private String remarks;
    //是否删除
    private String deleteEnum;
    //租户ID
    private Long tenantId;
    //机构ID
    private Long orgId;
    //环境
    private String env;

    public static List<FireCheckStandardE> importFireCheckStandardList(MultipartFile file, @NotEmpty(message = "消防器材类型不能为空") String fireDeviceType, @NotEmpty(message = "消防器材类型名称不能为空") String fireDeviceTypeName) {
        List<FireCheckStandardE> fireCheckStandardEList = new ArrayList<>();
        List<FireCheckStandardExcelImportEntity> list = new ArrayList<>();
        try {
            ExcelUtils.readBatch(file, FireCheckStandardExcelImportEntity.class, list);
            if (list.isEmpty()) {
                throw new BizException("文件为空");
            }
        } catch (Exception e) {
            throw new BizException("文件解析失败");
        }
        // 错误消息集合
        List<String> errList = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            FireCheckStandardExcelImportEntity entity = list.get(i);
            if(entity == null){
                errList.add("第" + (i+2) + "行数据不能为空。");
                continue;
            }
            // 部门
            if(StringUtils.isEmpty(entity.getDeviceName())){
                errList.add("第" + (i+2) + "行消防器材名称不能为空。");
            }
            // 岗位
            if(StringUtils.isEmpty(entity.getEmployer())){
                errList.add("第" + (i+2) + "行单位名称不能为空。");
            }
            // 风险点(单元)
            if(StringUtils.isEmpty(entity.getFireCheckStandardItem())){
                errList.add("第" + (i+2) + "行检查项不能为空。");
            }
        }

        if(CollUtil.isNotEmpty(errList)){
            throw new BizException("导入信息有误，请检查后重新导入。错误信息:" + String.join("", errList));
        }
        for (int i = 0; i < list.size(); i++) {
            FireCheckStandardExcelImportEntity entity = list.get(i);
            FireCheckStandardE fireCheckStandardE = new FireCheckStandardE();
            fireCheckStandardE.setFireDeviceType(fireDeviceType);
            fireCheckStandardE.setFireDeviceTypeName(fireDeviceTypeName);
            fireCheckStandardE.setFireCheckStandardId(UuidUtil.get32UUID());
            fireCheckStandardE.setCorpinfoId(AuthContext.getTenantId());
            fireCheckStandardE.setDeviceName(entity.getDeviceName());
            fireCheckStandardE.setEmployer(entity.getEmployer());
            fireCheckStandardE.setFireCheckStandardItem(entity.getFireCheckStandardItem());
            fireCheckStandardEList.add(fireCheckStandardE);
        }

        return fireCheckStandardEList;
    }
}

