package com.zcloud.fire.check.command.query;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.cola.dto.PageResponse;
import com.alibaba.cola.exception.BizException;
import com.zcloud.fire.check.command.convertor.FireCheckStandardCoConvertor;
import com.zcloud.fire.check.domain.model.FireCheckStandardE;
import com.zcloud.fire.check.dto.FireCheckStandardImportCmd;
import com.zcloud.fire.check.dto.FireCheckStandardPageQry;
import com.zcloud.fire.check.dto.clientobject.FireCheckStandardCO;
import com.zcloud.fire.check.persistence.dataobject.FireCheckStandardDO;
import com.zcloud.fire.check.persistence.repository.FireCheckStandardRepository;
import com.zcloud.gbscommon.utils.PageQueryHelper;
import lombok.AllArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.Collection;
import java.util.List;
import java.util.Map;


/**
 * web-app
 *
 * @Author zhaokai
 * @Date 2025-11-26 10:12:15
 */
@Component
@AllArgsConstructor
public class FireCheckStandardQueryExe {
    private final FireCheckStandardRepository fireCheckStandardRepository;
    private final FireCheckStandardCoConvertor fireCheckStandardCoConvertor;

    /**
     * 分页
     *
     * @param fireCheckStandardPageQry
     * @return
     */
    public PageResponse<FireCheckStandardCO> execute(FireCheckStandardPageQry fireCheckStandardPageQry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(fireCheckStandardPageQry);
        PageResponse<FireCheckStandardDO> pageResponse = fireCheckStandardRepository.listPage(params);
        List<FireCheckStandardCO> examCenterCOS = fireCheckStandardCoConvertor.converDOsToCOs(pageResponse.getData());
        return PageResponse.of(examCenterCOS, pageResponse.getTotalCount(), pageResponse.getPageSize(), pageResponse.getPageIndex());
    }

    public FireCheckStandardCO getInfoById(Long id) {
        FireCheckStandardDO fireCheckStandardDO = fireCheckStandardRepository.getById(id);
        if (fireCheckStandardDO == null) {
            return null;
        }
        FireCheckStandardCO fireCheckStandardCO = new FireCheckStandardCO();
        BeanUtils.copyProperties(fireCheckStandardDO, fireCheckStandardCO);
        return fireCheckStandardCO;
    }

    public Collection<FireCheckStandardCO> listAll(FireCheckStandardPageQry fireCheckStandardPageQry) {
        Map<String, Object> params = PageQueryHelper.toHashMap(fireCheckStandardPageQry);
        List<FireCheckStandardDO> pageResponse = fireCheckStandardRepository.listAll(params);
        List<FireCheckStandardCO> examCenterCOS = fireCheckStandardCoConvertor.converDOsToCOs(pageResponse);
        return examCenterCOS;
    }


    public void importFireCheckStandardList(FireCheckStandardImportCmd fireCheckStandardImportCmd) {
        List<FireCheckStandardE> fireCheckStandardEs = FireCheckStandardE.importFireCheckStandardList(fireCheckStandardImportCmd.getFile(), fireCheckStandardImportCmd.getFireDeviceType(), fireCheckStandardImportCmd.getFireDeviceTypeName());
        // 批量插入
        boolean res = false;
        try {
            res = fireCheckStandardRepository.addBatch(BeanUtil.copyToList(fireCheckStandardEs, FireCheckStandardDO.class));
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        if (!res) {
            throw new BizException("导入失败");
        }

    }
}

