package com.tofly.water.service.impl;


import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.haiqiu.tools.excel.utils.ExcelUtils;
import com.tofly.scada.entity.DeviceArchiveManage;
import com.tofly.scada.service.DeviceArchiveManageService;
import com.tofly.spire.entity.IndexAllocation;
import com.tofly.spire.service.IndexAllocationService;
import com.tofly.water.entity.LaborPlan;
import com.tofly.water.entity.PlanPump;
import com.tofly.water.entity.dto.NewestDto;
import com.tofly.water.entity.vo.DeviceBusPumpsVo;
import com.tofly.water.service.OverviewService;
import com.tofly.water.service.PlanPumpService;
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.OperationPlanMapper;
import com.tofly.water.entity.OperationPlan;
import com.tofly.water.entity.dto.OperationPlanDto;
import com.tofly.water.service.OperationPlanService;
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;
import java.util.stream.Collectors;

/**
 * 调度日志(OperationPlan)表服务实现类
 *
 * @author HaiQiu
 * @since 2022-11-22 11:41:56
 */
@Service("operationPlanService")
public class OperationPlanServiceImpl extends ServiceImpl<OperationPlanMapper, OperationPlan> implements OperationPlanService {


    @Autowired
    private PlanPumpService planPumpService;

    @Autowired
    private IndexAllocationService indexAllocationService;

    @Autowired
    private DeviceArchiveManageService deviceArchiveManageService;

    @Autowired
    private OverviewService overviewService;

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


    /**
     * 新增
     *
     * @param operationPlanDto 实体
     */
    @Override
    public void insert(OperationPlanDto operationPlanDto) {
        OperationPlan operationPlan = convert(operationPlanDto);
        if (!save(operationPlan)) {
            throw new RuntimeException("新增失败");
        }
        savePlanPump(operationPlan);
    }

    /**
     * 修改
     *
     * @param operationPlanDto 实体
     */
    @Override
    public void edit(OperationPlanDto operationPlanDto) {
        OperationPlan operationPlan = convert(operationPlanDto);
        if (!updateById(operationPlan)) {
            throw new RuntimeException("修改失败");
        }
        savePlanPump(operationPlan);
    }

    private void savePlanPump(OperationPlan laborPlan) {
        if (!CollectionUtils.isEmpty(laborPlan.getPlanPumps())){
            for (PlanPump planPump : laborPlan.getPlanPumps()) {
                planPump.setPlanId(laborPlan.getId());
                planPump.setPlanType(true);
                planPump.setId(null);
            }
            //先删除
            planPumpService.remove(Wrappers.<PlanPump>lambdaQuery()
                    .eq(PlanPump::getPlanId, laborPlan.getId())
                    .eq(PlanPump::getPlanType,true));
            //再添加
            planPumpService.saveBatch(laborPlan.getPlanPumps());
        }
    }

    /**
     * 实体转换
     *
     * @param operationPlanDto dto实体
     */
    private OperationPlan convert(OperationPlanDto operationPlanDto) {
        OperationPlan operationPlan = new OperationPlan();
        BeanUtils.copyProperties(operationPlanDto, operationPlan);
        return operationPlan;
    }

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

    /**
     * 生成模板下载
     */
    @Override
    public void template() {
        OperationPlan operationPlan = new OperationPlan();
        ExcelUtils.createTemplate("调度日志", "调度日志",
                "调度日志", OperationPlan.class, Collections.singletonList(operationPlan));
    }

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


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

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

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

