package com.tofly.water.service.impl;


import com.haiqiu.tools.excel.utils.ExcelUtils;
import com.tofly.common.service.IFileInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.CollectionUtils;
import org.springframework.beans.BeanUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.water.mapper.SafeMonitorMapper;
import com.tofly.water.entity.SafeMonitor;
import com.tofly.water.entity.dto.SafeMonitorDto;
import com.tofly.water.service.SafeMonitorService;
import org.springframework.stereotype.Service;

import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.io.Serializable;

/**
 * 安防监控（门禁管理）(SafeMonitor)表服务实现类
 *
 * @author HaiQiu
 * @since 2022-11-22 11:39:36
 */
@Service("safeMonitorService")
public class SafeMonitorServiceImpl extends ServiceImpl<SafeMonitorMapper, SafeMonitor> implements SafeMonitorService {


    @Value("${safeMonitor.ftp.uploads:/dcsw/project/safeMonitor}")
    private String path;
    @Autowired
    private IFileInfoService fileInfoService;

    /**
     * 根据ID查询
     *
     * @param id id
     * @return 数据
     */
    @Override
    public SafeMonitor get(Serializable id) {
        return getById(id);
    }


    /**
     * 新增
     *
     * @param safeMonitorDto 实体
     * @param files          上传文件附件
     */
    @Override
    public void insert(SafeMonitorDto safeMonitorDto, List<MultipartFile> files) {
        SafeMonitor safeMonitor = convert(safeMonitorDto);
        safeMonitor.setAnnex(fileInfoService.uploadFiles(files, safeMonitor.getAnnex()
                , safeMonitor.getAnnex(), path, "TF_SAFE_MONITOR"));
        if (!save(safeMonitor)) {
            throw new RuntimeException("新增失败");
        }
    }

    /**
     * 修改
     *
     * @param safeMonitorDto 实体
     * @param files          上传文件附件
     */
    @Override
    public void edit(SafeMonitorDto safeMonitorDto, List<MultipartFile> files) {
        SafeMonitor safeMonitor = convert(safeMonitorDto);
        safeMonitor.setAnnex(fileInfoService.uploadFiles(files, safeMonitor.getAnnex()
                , safeMonitor.getAnnex(), path, "TF_SAFE_MONITOR"));
        if (!updateById(safeMonitor)) {
            throw new RuntimeException("修改失败");
        }
    }

    /**
     * 实体转换
     *
     * @param safeMonitorDto dto实体
     */
    private SafeMonitor convert(SafeMonitorDto safeMonitorDto) {
        SafeMonitor safeMonitor = new SafeMonitor();
        BeanUtils.copyProperties(safeMonitorDto, safeMonitor);
        return safeMonitor;
    }

    /**
     * 批量删除
     *
     * @param idList 数据ID集合
     */
    @Override
    public void delete(List<Long> idList) {
        if (!removeByIds(idList)) {
            throw new RuntimeException("删除失败");
        }
    }

    /**
     * 生成模板下载
     */
    @Override
    public void template() {
        SafeMonitor safeMonitor = new SafeMonitor();
        ExcelUtils.createTemplate("安防监控（门禁管理）", "安防监控（门禁管理）",
                "安防监控（门禁管理）", SafeMonitor.class, Collections.singletonList(safeMonitor));
    }

    /**
     * 数据导入
     *
     * @param file 文件
     */
    @Override
    public void importData(MultipartFile file) {
        List<SafeMonitor> excelToList = ExcelUtils.getExcelToList("安防监控（门禁管理）", file, SafeMonitor.class);
        if (CollectionUtils.isEmpty(excelToList)) {
            throw new RuntimeException("数据解析为空");
        }
        saveBatch(excelToList);
    }

    /**
     * 分页查询
     *
     * @param page        分页
     * @param safeMonitor 查询wrapper
     * @return 数据集合
     */
    @Override
    public Page<SafeMonitor> pageList(Page page, SafeMonitor safeMonitor) {
        return page(page, new QueryWrapper<>(safeMonitor)).addOrder(page.getOrders());
    }

    /**
     * 数据导出
     *
     * @param page        分页
     * @param safeMonitor 查询模型
     * @param isAll       是否导出全部数据，true是，false否
     */
    @Override
    public void export(Page page, SafeMonitor safeMonitor, Boolean isAll) {
        Page<SafeMonitor> pageData = new Page<>();
        if (isAll == null || !isAll) {
            pageData = page(page, new QueryWrapper<>(safeMonitor)).addOrder(page.getOrders());
        } else {
            pageData.setRecords(list(new QueryWrapper<>(safeMonitor)));
        }
        //导出数据
        ExcelUtils.getListToExcel(SafeMonitor.class, pageData.getRecords(), "安防监控（门禁管理）");
    }

    /**
     * 查询所有数据
     *
     * @param safeMonitor 查询条件
     * @return 查询所有数据
     */
    @Override
    public List<SafeMonitor> allList(SafeMonitor safeMonitor) {
        return list(new QueryWrapper<>(safeMonitor));
    }
}

