package com.tofly.water.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.haiqiu.tools.excel.utils.ExcelUtils;
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.DeviceBusinessMapper;
import com.tofly.water.entity.DeviceBusiness;
import com.tofly.water.entity.dto.DeviceBusinessDto;
import com.tofly.water.service.DeviceBusinessService;
import org.springframework.stereotype.Service;

import org.springframework.util.ObjectUtils;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 设备档案和外部主键关联(DeviceBusiness)表服务实现类
 *
 * @author HaiQiu
 * @since 2022-11-30 15:36:47
 */
@Service("deviceBusinessService")
public class DeviceBusinessServiceImpl extends ServiceImpl<DeviceBusinessMapper, DeviceBusiness> implements DeviceBusinessService {


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


    /**
     * 新增
     *
     * @param deviceBusinessDto 实体
     */
    @Override
    public void insert(DeviceBusinessDto deviceBusinessDto) {
        DeviceBusiness deviceBusiness = convert(deviceBusinessDto);
        if (!save(deviceBusiness)) {
            throw new RuntimeException("新增失败");
        }
    }

    /**
     * 修改
     *
     * @param deviceBusinessDto 实体
     */
    @Override
    public void edit(DeviceBusinessDto deviceBusinessDto) {
        DeviceBusiness deviceBusiness = convert(deviceBusinessDto);
        if (!updateById(deviceBusiness)) {
            throw new RuntimeException("修改失败");
        }
    }

    /**
     * 实体转换
     *
     * @param deviceBusinessDto dto实体
     */
    private DeviceBusiness convert(DeviceBusinessDto deviceBusinessDto) {
        DeviceBusiness deviceBusiness = new DeviceBusiness();
        BeanUtils.copyProperties(deviceBusinessDto, deviceBusiness);
        return deviceBusiness;
    }


    @Override
    public void saveBusiness(Long busId, String tableName) {
        //保存中间关联表信息
        int count = count(Wrappers.<DeviceBusiness>lambdaQuery()
                .eq(DeviceBusiness::getBusinessId, busId)
                .eq(DeviceBusiness::getOutsideTable, tableName));
        if (count == 0) {
            DeviceBusiness deviceBusiness = new DeviceBusiness();
            deviceBusiness.setBusinessId(busId);
            deviceBusiness.setOutsideTable(tableName);
            save(deviceBusiness);
        }
    }

    @Override
    public boolean removeBusiness(List<Long> busIds, String tableName) {
       return remove(Wrappers.<DeviceBusiness>lambdaQuery()
                .in(DeviceBusiness::getBusinessId, busIds)
                .eq(DeviceBusiness::getOutsideTable, tableName));
    }
    /**
     * 批量删除
     *
     * @param idList 数据ID集合
     */
    @Override
    public void delete(List<Long> idList) {
        if (!removeByIds(idList)) {
            throw new RuntimeException("删除失败");
        }
    }

    /**
     * 生成模板下载
     */
    @Override
    public void template() {
        DeviceBusiness deviceBusiness = new DeviceBusiness();
        ExcelUtils.createTemplate("设备档案和外部主键关联", "设备档案和外部主键关联",
                "设备档案和外部主键关联", DeviceBusiness.class, Collections.singletonList(deviceBusiness));
    }

    /**
     * 数据导入
     *
     * @param file 文件
     */
    @Override
    public void importData(MultipartFile file) {
        List<DeviceBusiness> excelToList = ExcelUtils.getExcelToList("设备档案和外部主键关联", file, DeviceBusiness.class);
        if (CollectionUtils.isEmpty(excelToList)) {
            throw new RuntimeException("数据解析为空");
        }
        saveBatch(excelToList);
    }

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

    /**
     * 数据导出
     *
     * @param page           分页
     * @param deviceBusiness 查询模型
     * @param isAll          是否导出全部数据，true是，false否（默认否）
     */
    @Override
    public void export(Page page, DeviceBusiness deviceBusiness, boolean isAll) {
        Page<DeviceBusiness> pageData = new Page<>();
        if (!isAll) {
            pageData = page(page, new QueryWrapper<>(deviceBusiness)).addOrder(page.getOrders());
        } else {
            pageData.setRecords(list(new QueryWrapper<>(deviceBusiness)));
        }
        //导出数据
        ExcelUtils.getListToExcel(DeviceBusiness.class, pageData.getRecords(), "设备档案和外部主键关联");
    }

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

