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.PlanPump;
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.LaborPlanMapper;
import com.tofly.water.entity.LaborPlan;
import com.tofly.water.entity.dto.LaborPlanDto;
import com.tofly.water.service.LaborPlanService;
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;

/**
 * 调度方案管理(LaborPlan)表服务实现类
 *
 * @author HaiQiu
 * @since 2022-11-22 11:41:09
 */
@Service("laborPlanService")
public class LaborPlanServiceImpl extends ServiceImpl<LaborPlanMapper, LaborPlan> implements LaborPlanService {


    @Autowired
    private PlanPumpService planPumpService;


    @Autowired
    private IndexAllocationService indexAllocationService;

    @Autowired
    private DeviceArchiveManageService deviceArchiveManageService;

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



    /**
     * 新增
     *
     * @param laborPlanDto 实体
     */
    @Override
    public void insert(LaborPlanDto laborPlanDto) {
        LaborPlan laborPlan = convert(laborPlanDto);
        if (!save(laborPlan)) {
            throw new RuntimeException("新增失败");
        }
        savePlanPump(laborPlan);
    }

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

    /**
     * 修改
     *
     * @param laborPlanDto 实体
     */
    @Override
    public void edit(LaborPlanDto laborPlanDto) {
        LaborPlan laborPlan = convert(laborPlanDto);
        if (!updateById(laborPlan)) {
            throw new RuntimeException("修改失败");
        }
        savePlanPump(laborPlan);
    }

    /**
     * 实体转换
     *
     * @param laborPlanDto dto实体
     */
    private LaborPlan convert(LaborPlanDto laborPlanDto) {
        LaborPlan laborPlan = new LaborPlan();
        BeanUtils.copyProperties(laborPlanDto, laborPlan);
        return laborPlan;
    }

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

    /**
     * 生成模板下载
     */
    @Override
    public void template() {
        LaborPlan laborPlan = new LaborPlan();
        ExcelUtils.createTemplate("调度方案管理", "调度方案管理",
                "调度方案管理", LaborPlan.class, Collections.singletonList(laborPlan));
    }

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

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

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

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

