package com.tofly.water.service.impl;


import com.haiqiu.tools.excel.utils.ExcelUtils;
import com.tofly.water.entity.DevicePump;
import com.tofly.water.service.DevicePumpService;
import org.springframework.beans.factory.annotation.Autowired;
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.BasePumpDetailMapper;
import com.tofly.water.entity.BasePumpDetail;
import com.tofly.water.entity.dto.BasePumpDetailDto;
import com.tofly.water.service.BasePumpDetailService;
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;

/**
 * 水泵基础信息(BasePumpDetail)表服务实现类
 *
 * @author HaiQiu
 * @since 2022-11-22 11:34:21
 */
@Service("basePumpDetailService")
public class BasePumpDetailServiceImpl extends ServiceImpl<BasePumpDetailMapper, BasePumpDetail> implements BasePumpDetailService {


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


    /**
     * 新增
     *
     * @param basePumpDetailDto 实体
     */
    @Override
    public void insert(BasePumpDetailDto basePumpDetailDto) {
        BasePumpDetail basePumpDetail = convert(basePumpDetailDto);
        if (!save(basePumpDetail)) {
            throw new RuntimeException("新增失败");
        }
    }

    /**
     * 修改
     *
     * @param basePumpDetailDto 实体
     */
    @Override
    public void edit(BasePumpDetailDto basePumpDetailDto) {
        BasePumpDetail basePumpDetail = convert(basePumpDetailDto);
        if (!updateById(basePumpDetail)) {
            throw new RuntimeException("修改失败");
        }
    }

    /**
     * 实体转换
     *
     * @param basePumpDetailDto dto实体
     */
    private BasePumpDetail convert(BasePumpDetailDto basePumpDetailDto) {
        BasePumpDetail basePumpDetail = new BasePumpDetail();
        BeanUtils.copyProperties(basePumpDetailDto, basePumpDetail);
        return basePumpDetail;
    }

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

    /**
     * 生成模板下载
     */
    @Override
    public void template() {
        BasePumpDetail basePumpDetail = new BasePumpDetail();
        ExcelUtils.createTemplate("水泵基础信息", "水泵基础信息",
                "水泵基础信息", BasePumpDetail.class, Collections.singletonList(basePumpDetail));
    }

    /**
     * 数据导入
     *
     * @param file 文件
     */
    @Override
    public void importData(MultipartFile file) {
        List<BasePumpDetail> excelToList = ExcelUtils.getExcelToList("水泵基础信息", file, BasePumpDetail.class);
        if (CollectionUtils.isEmpty(excelToList)) {
            throw new RuntimeException("数据解析为空");
        }
        saveBatch(excelToList);
    }

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

    /**
     * 数据导出
     *
     * @param page           分页
     * @param basePumpDetail 查询模型
     * @param isAll          是否导出全部数据，true是，false否
     */
    @Override
    public void export(Page page, BasePumpDetail basePumpDetail, Boolean isAll) {
        Page<BasePumpDetail> pageData = new Page<>();
        if (isAll == null || !isAll) {
            pageData = page(page, new QueryWrapper<>(basePumpDetail)).addOrder(page.getOrders());
        } else {
            pageData.setRecords(list(new QueryWrapper<>(basePumpDetail)));
        }
        //导出数据
        ExcelUtils.getListToExcel(BasePumpDetail.class, pageData.getRecords(), "水泵基础信息");
    }

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

