package com.tofly.water.service.impl;


import com.haiqiu.tools.excel.utils.ExcelUtils;
import com.tofly.water.entity.BasePumpDetail;
import com.tofly.water.service.BasePumpDetailService;
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.PlanPumpMapper;
import com.tofly.water.entity.PlanPump;
import com.tofly.water.entity.dto.PlanPumpDto;
import com.tofly.water.service.PlanPumpService;
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;

/**
 * 调度计划绑定水泵(PlanPump)表服务实现类
 *
 * @author HaiQiu
 * @since 2022-11-22 11:42:40
 */
@Service("planPumpService")
public class PlanPumpServiceImpl extends ServiceImpl<PlanPumpMapper, PlanPump> implements PlanPumpService {


    @Autowired
    private BasePumpDetailService pumpDetailService;

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


    /**
     * 获取水泵信息
     * @param planPump 水泵信息
     */
    private void getPumpDetail(PlanPump planPump) {
        if (!ObjectUtils.isEmpty(planPump)){
            BasePumpDetail pumpDetail = pumpDetailService.get(planPump.getPumpDetailId());
            planPump.setBasePumpDetail(pumpDetail);
        }
    }


    /**
     * 新增
     *
     * @param planPumpDto 实体
     */
    @Override
    public void insert(PlanPumpDto planPumpDto) {
        PlanPump planPump = convert(planPumpDto);
        if (!save(planPump)) {
            throw new RuntimeException("新增失败");
        }
    }

    /**
     * 修改
     *
     * @param planPumpDto 实体
     */
    @Override
    public void edit(PlanPumpDto planPumpDto) {
        PlanPump planPump = convert(planPumpDto);
        if (!updateById(planPump)) {
            throw new RuntimeException("修改失败");
        }
    }

    /**
     * 实体转换
     *
     * @param planPumpDto dto实体
     */
    private PlanPump convert(PlanPumpDto planPumpDto) {
        PlanPump planPump = new PlanPump();
        BeanUtils.copyProperties(planPumpDto, planPump);
        return planPump;
    }

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

    /**
     * 生成模板下载
     */
    @Override
    public void template() {
        PlanPump planPump = new PlanPump();
        ExcelUtils.createTemplate("调度计划绑定水泵", "调度计划绑定水泵",
                "调度计划绑定水泵", PlanPump.class, Collections.singletonList(planPump));
    }

    /**
     * 数据导入
     *
     * @param file 文件
     */
    @Override
    public void importData(MultipartFile file) {
        List<PlanPump> excelToList = ExcelUtils.getExcelToList("调度计划绑定水泵", file, PlanPump.class);
        if (CollectionUtils.isEmpty(excelToList)) {
            throw new RuntimeException("数据解析为空");
        }
        saveBatch(excelToList);
    }

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

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

    /**
     * 查询所有数据
     *
     * @param planPump 查询条件
     * @return 查询所有数据
     */
    @Override
    public List<PlanPump> allList(PlanPump planPump) {
        List<PlanPump> list = list(new QueryWrapper<>(planPump));
        if (!CollectionUtils.isEmpty(list)){
            for (PlanPump pump : list) {
                getPumpDetail(pump);
            }
        }
        return list;
    }
}

