package com.yunyan.business.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunyan.business.document.y22.FinalY22Utils;
import com.yunyan.business.document.y22.pojo.finalExcelY22.FinalP4T1TermVo;
import com.yunyan.business.domain.ReportDept;
import com.yunyan.business.domain.Term;
import com.yunyan.business.service.ITermService;
import com.yunyan.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yunyan.business.mapper.ReportTermMapper;
import com.yunyan.business.domain.ReportTerm;
import com.yunyan.business.service.IReportTermService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunyan.common.core.domain.AjaxResultVo;
import org.springframework.beans.BeanUtils;
import lombok.extern.slf4j.Slf4j;

import java.util.stream.Collectors;

/**
 * 报告-名词解释数据Service业务层处理
 *
 * @author yunyan
 * @date 2023-12-07
 */
@Slf4j
@Service
public class ReportTermServiceImpl extends ServiceImpl<ReportTermMapper, ReportTerm> implements IReportTermService
{
    @Autowired
    private ReportTermMapper reportTermMapper;

    @Autowired
    private ITermService termService;

    /**
     * 查询报告-名词解释数据列表
     *
     * @param reportTerm 报告-名词解释数据
     * @return 报告-名词解释数据
     */    @Override
    public List<ReportTerm> selectReportTermList(ReportTerm reportTerm)
    {
        return reportTermMapper.selectReportTermList(reportTerm);
    }

    /**
    * 新增
    * @param
    * @return
    */
    @Override
    public AjaxResultVo add(ReportTerm dto) {
        ReportTerm entity = dto;

        this.save(entity);
        return AjaxResultVo.success();
    }

    /**
     * 编辑
     * @param
     * @return
     */
    @Override
    public AjaxResultVo edit(ReportTerm dto) {

        ReportTerm entity = this.getById(dto.getId());
        if(null == entity) {
            return AjaxResultVo.error("非法操作");
        }

        entity = new ReportTerm();
        BeanUtils.copyProperties(dto, entity);

        this.updateById(entity);
        return AjaxResultVo.success();
    }

    /**
     * 详情
     * @param id
     * @return
     */
    @Override
    public AjaxResultVo info(Long id) {
        ReportTerm entity = this.getById(id);
        if(null == entity) {
            return AjaxResultVo.error("数据查询异常~");
        }

        return AjaxResultVo.success(entity);
    }

    @Override
    public List<FinalP4T1TermVo>  saveForNewTerm(String area, String year, Integer type) {
        List<FinalP4T1TermVo> resList = new ArrayList<>();

        if(StringUtils.isBlank(area) ) {
            return resList;
        }
        if(StringUtils.isBlank(year)) {
            return resList;
        }
        if(null == type) {
            return resList;
        }

        LambdaQueryWrapper<ReportTerm> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReportTerm::getArea, area);
        queryWrapper.eq(ReportTerm::getYear, year);
        queryWrapper.eq(ReportTerm::getType, type);
        queryWrapper.orderByDesc(ReportTerm::getCreateTime);
        List<ReportTerm> reportTermList = this.list(queryWrapper);
        if(reportTermList.size() > 0) {
            ReportTerm reportTerm = reportTermList.get(0);
            resList = JSONUtil.toList(reportTerm.getJsonContent(), FinalP4T1TermVo.class);

            //删除旧的, 下面保存新的
            List<Long> idList = reportTermList.stream().map(ReportTerm::getId).collect(Collectors.toList());
            termService.removeByIds(idList);
            //return resList;
        }

        List<Term> termList = termService.lambdaQuery().eq(Term::getStatus, 1).eq(Term::getType, type).eq(Term::getArea, area).orderByAsc(Term::getOrderNum).list();
        if(!termList.isEmpty()) {
            final int[] index = {1};
            resList = termList.stream().map(i-> {
                FinalP4T1TermVo p4T1TermVo = new FinalP4T1TermVo();
                p4T1TermVo.setName(i.getTitle());
                p4T1TermVo.setInfo(i.getDetail());
                //p4T1TermVo.setRowNo(String.valueOf(index[0]++));
                return p4T1TermVo;
            }).collect(Collectors.toList());

            //设置序列
            FinalY22Utils.setRowNoChineseFormatter(resList, "rowNo");

            String jsonContent = JSONUtil.toJsonStr(resList);

            //保存 报告名词解释
            ReportTerm reportTerm = new ReportTerm();
            reportTerm.setType(type);
            reportTerm.setYear(year);
            reportTerm.setArea(area);
            reportTerm.setJsonContent(jsonContent);
            reportTerm.setCreateTime(new Date());

            this.save(reportTerm);
        }

        return resList;
    }

    @Override
    public List<FinalP4T1TermVo> getTermInfo(String area, String year, Integer type) {
        List<FinalP4T1TermVo> resList = new ArrayList<>();

        if(StringUtils.isBlank(area) ) {
            return resList;
        }
        if(StringUtils.isBlank(year)) {
            return resList;
        }
        if(null == type) {
            return resList;
        }

        LambdaQueryWrapper<ReportTerm> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReportTerm::getArea, area);
        queryWrapper.eq(ReportTerm::getYear, year);
        queryWrapper.eq(ReportTerm::getType, type);
        queryWrapper.orderByDesc(ReportTerm::getCreateTime);
        List<ReportTerm> reportTermList = this.list(queryWrapper);
        if(reportTermList.size() > 0) {
            ReportTerm reportTerm = reportTermList.get(0);
            resList = JSONUtil.toList(reportTerm.getJsonContent(), FinalP4T1TermVo.class);

            return resList;
        }


        return resList;
    }
}
