package com.tofly.scada.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tofly.common.core.util.StringUtil;
import com.tofly.common.oauth.util.SecurityUtils;
import com.tofly.scada.entity.TfScadaReportDeal;
import com.tofly.scada.entity.TfScadaReportList;
import com.tofly.scada.mapper.TfScadaReportListMapper;
import com.tofly.scada.service.TfScadaReportDealService;
import com.tofly.scada.service.TfScadaReportListService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import java.io.Serializable;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 报警数据存储(TfScadaReportList)表服务实现类
 *
 * @author HaiQiu
 * @since 2022-09-21 16:21:21
 */
@Service("tfScadaReportListService")
@Slf4j
public class TfScadaReportListServiceImpl extends ServiceImpl<TfScadaReportListMapper, TfScadaReportList> implements TfScadaReportListService {

    @Autowired
   private TfScadaReportDealService tfScadaReportDealService;

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

    /**
     * 新增
     *
     * @param tfScadaReportList 实体
     */
    @Override
    public void insert(TfScadaReportList tfScadaReportList) {
        if (!save(tfScadaReportList)) {
            throw new RuntimeException("新增失败");
        }
    }

    /**
     * 修改
     *
     * @param tfScadaReportList 实体
     */
    @Override
    public void edit(TfScadaReportList tfScadaReportList) {
        if (!updateById(tfScadaReportList)) {
            throw new RuntimeException("修改失败");
        }
    }

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

    /**
     * 生成模板下载
     */
    @Override
    public void template() {
//        TfScadaReportList tfScadaReportList = new TfScadaReportList();
//        HqExcelUtils.createTemplate("TfScadaReportList报警数据存储", "TfScadaReportList报警数据存储",
//                TfScadaReportList.class, Collections.singletonList(tfScadaReportList));
    }

    /**
     * 数据导入
     *
     * @param file 文件
     */
    @Override
    public void importData(MultipartFile file) {
//        List<TfScadaReportList> datas = HqExcelUtils.getExcelToList("TfScadaReportList报警数据存储", file, TfScadaReportList.class);
//        if (!CollectionUtils.isEmpty(datas)) {
//            saveBatch(datas);
//        }
    }

    /**
     * 分页查询
     *
     * @param page              分页
     * @param tfScadaReportList 查询wrapper
     * @return 数据集合
     */
    @Override
    public Page<TfScadaReportList> pageList(Page<TfScadaReportList> page, TfScadaReportList tfScadaReportList) {
        /*List<String> codes = new ArrayList<>();
        if (tfScadaReportList != null && StringUtils.hasText(tfScadaReportList.getCodes())) {
            codes = Arrays.asList(tfScadaReportList.getCodes().split(","));
        }
        if(!StringUtil.isEmpty(tfScadaReportList.getDealStatus())){
            tfScadaReportList.setStatus(tfScadaReportList.getDealStatus());
            tfScadaReportList.setDealStatus(null);
        }

        List<Long> listIds=new ArrayList<>();

         //只查询自己相关的数据
        if(tfScadaReportList.getSearchOwn().equals(1)){
            listIds=tfScadaReportDealService.list(Wrappers.<TfScadaReportDeal>lambdaQuery()
                            .eq(TfScadaReportDeal::getDealUserId,SecurityUtils.getUserId()))
                    .stream().map(TfScadaReportDeal::getReportId).collect(Collectors.toList());
            if(listIds==null || listIds.size()==0){
                listIds=new ArrayList<>();
                listIds.add(0L);
            }

        }*/
        return baseMapper.pageList(page,tfScadaReportList,SecurityUtils.getUserId());
      /*  return page(page, new QueryWrapper<>(tfScadaReportList).lambda()
                .between(tfScadaReportList != null && tfScadaReportList.getStart() != null && tfScadaReportList.getEnd() != null,
                        TfScadaReportList::getScadaTime, tfScadaReportList.getStart(), tfScadaReportList.getEnd())
                .in(!CollectionUtils.isEmpty(codes), TfScadaReportList::getCode, codes)
                .in(listIds.size()>0,TfScadaReportList::getId,listIds));*/
    }

    /**
     * 数据导出
     *
     * @param page              分页
     * @param tfScadaReportList 查询模型
     * @param isAll             是否导出全部数据，true是，false否
     */
    @Override
    public void export(Page<TfScadaReportList> page, TfScadaReportList tfScadaReportList, Boolean isAll) {
        Page<TfScadaReportList> pageData = new Page<>();
        if (isAll == null || !isAll) {
            pageData = page(page, new QueryWrapper<>(tfScadaReportList));
        } else {
            pageData.setRecords(list(new QueryWrapper<>(tfScadaReportList)));
        }
        //导出数据
        //HqExcelUtils.getListToExcel(TfScadaReportList.class, pageData.getRecords(), "TfScadaReportList报警数据存储");
    }

    /**
     * 查询所有数据
     *
     * @param tfScadaReportList 查询条件
     * @return 查询所有数据
     */
    @Override
    public List<TfScadaReportList> allList(TfScadaReportList tfScadaReportList) {
        List<String> codes = new ArrayList<>();
        if (tfScadaReportList != null && StringUtils.hasText(tfScadaReportList.getCodes())) {
            codes = Arrays.asList(tfScadaReportList.getCodes().split(","));
        }
        return list(new QueryWrapper<>(tfScadaReportList).lambda()
                .between(tfScadaReportList != null && tfScadaReportList.getStart() != null && tfScadaReportList.getEnd() != null,
                        TfScadaReportList::getScadaTime, tfScadaReportList.getStart(), tfScadaReportList.getEnd())
                .in(!CollectionUtils.isEmpty(codes), TfScadaReportList::getCode, codes).orderByDesc(TfScadaReportList::getScadaTime));
    }

    @Override
    public void change(List<Long> idList, String status,String dealRemark) {
        List<TfScadaReportList> scadaReportLists = new ArrayList<>();
        for (Long aLong : idList) {
            TfScadaReportList tfScadaReportList = new TfScadaReportList();
            tfScadaReportList.setId(aLong);
            tfScadaReportList.setStatus(status);
            tfScadaReportList.setDealDate(new Date());
            tfScadaReportList.setDealRemark(dealRemark);
            tfScadaReportList.setDealUserId(SecurityUtils.getUserId());
            tfScadaReportList.setDealUserName(SecurityUtils.getUserName());
            scadaReportLists.add(tfScadaReportList);
        }
        updateBatchById(scadaReportLists);
    }

    @Override
    public List<Map<String, String>> getReportLevel(TfScadaReportList tfScadaReportList) {

        return this.baseMapper.getReportLevel(tfScadaReportList);
    }



}

