package com.dayou.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.dayou.bo.HeadlineTreeBO;
import com.dayou.common.BaseEntity;
import com.dayou.common.CascaderDTO;
import com.dayou.entity.ReportHeadline;
import com.dayou.mapper.ReportHeadlineMapper;
import com.dayou.service.IReportHeadlineService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dayou.utils.ConvertUtil;
import com.dayou.utils.TreeUtil;
import com.dayou.vo.IdNameVO;
import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.dayou.utils.ExcelUtil;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;
import java.util.List;
import java.util.ArrayList;
import org.springframework.transaction.annotation.Transactional;
import com.dayou.enums.BatchTaskTypeEnum;

/**
 * <p>
 * 报告标题 服务实现类
 * </p>
 *
 * @author wucl
 * @since 2023-08-09
 */
@Service
public class ReportHeadlineServiceImpl extends ServiceImpl<ReportHeadlineMapper, ReportHeadline> implements IReportHeadlineService {

    @Autowired
    private ReportHeadlineMapper reportHeadlineMapper;

    @Override
    @SuppressWarnings("unchecked")
    public Page<ReportHeadline> selectPage(Page page,ReportHeadline reportHeadline){
        return this.page(page, new QueryWrapper<ReportHeadline>(reportHeadline));
    }


    @Override
    public ReportHeadline detail(Long id){
        return this.getById(id);
    }

    @Override
    public Boolean add(ReportHeadline reportHeadline){
        return  this.save(reportHeadline);
    }

    @Override
    public Boolean update(ReportHeadline reportHeadline){
        return  this.updateById(reportHeadline);
    }

    @Override
    public Boolean delete(Long id){
        //逻辑删除
        return this.removeById(id);
    }

    @Override
    public List<ReportHeadline> getHeadline(Long rootId) {
        List<ReportHeadline> list = reportHeadlineMapper.getHeadline(rootId);
        return list;
    }

    @Override
    public List<HeadlineTreeBO> getHeadlineTree(Long rootId) {
        List<ReportHeadline> list = reportHeadlineMapper.getHeadline(rootId);
        List<HeadlineTreeBO> headlineTreeBOS = ConvertUtil.copyList(list, HeadlineTreeBO.class);
        List<HeadlineTreeBO> headlineTree = (List<HeadlineTreeBO>) TreeUtil.buildTree(headlineTreeBOS,rootId);
        return headlineTree;
    }

    @Override
    public List<ReportHeadline> getHeadlineRoot() {
        List<ReportHeadline> list = this.list(new LambdaQueryWrapper<ReportHeadline>().isNull(ReportHeadline::getParentId).eq(BaseEntity::getDeleted, Boolean.FALSE));
        return list;
    }

    @Override
    public List<CascaderDTO> getAllHeadlineTree() {
        List<CascaderDTO> list = reportHeadlineMapper.getAllHeadlineTree();
        List<CascaderDTO> headlineTree = (List<CascaderDTO>) TreeUtil.buildTree(list);
        return headlineTree;
    }

    @Override
    public List<ReportHeadline> getHeadlinesByType(String reportType) {
        List<ReportHeadline> list = reportHeadlineMapper.getHeadlinesByType(reportType);
        return list;
    }
}
