package com.yunyan.business.service.impl;

import java.util.List;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunyan.business.document.y22.pojo.finalExcelY22.FinalP4T1TermVo;
import com.yunyan.business.domain.ReportTerm;
import com.yunyan.business.domain.Term;
import com.yunyan.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yunyan.business.mapper.ReportFillMapper;
import com.yunyan.business.domain.ReportFill;
import com.yunyan.business.service.IReportFillService;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.NoArgsConstructor;
import lombok.AllArgsConstructor;
import lombok.Builder;
import com.yunyan.common.core.domain.AjaxResultVo;
import org.springframework.beans.BeanUtils;
import com.yunyan.common.core.domain.model.LoginUser;
import com.yunyan.common.utils.SecurityUtils;
import com.yunyan.common.enums.DataContentType;
import lombok.extern.slf4j.Slf4j;
import java.util.Date;
import java.util.Objects;
import java.util.stream.Collectors;

import com.yunyan.common.annotation.DataScope;

/**
 * 报告-填报数据Service业务层处理
 *
 * @author yunyan
 * @date 2023-12-11
 */
@Slf4j
@Service
public class ReportFillServiceImpl extends ServiceImpl<ReportFillMapper, ReportFill> implements IReportFillService
{
    @Autowired
    private ReportFillMapper reportFillMapper;

    /**
     * 查询报告-填报数据列表
     *
     * @param reportFill 报告-填报数据
     * @return 报告-填报数据
     */    @Override
    public List<ReportFill> selectReportFillList(ReportFill reportFill)
    {
        return reportFillMapper.selectReportFillList(reportFill);
    }

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

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

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

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

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

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

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

        return AjaxResultVo.success(entity);
    }

    @Override
    public boolean saveOrUpdateFille(Long reportId, String jsonConetns) {
        LambdaQueryWrapper<ReportFill> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReportFill::getReportId, reportId);
        queryWrapper.orderByDesc(ReportFill::getCreateTime);
        queryWrapper.select(ReportFill::getReportId, ReportFill::getId);
        List<ReportFill> reportFillList = this.list(queryWrapper);

        //保存 或者更新
        ReportFill reportFill = new ReportFill();
        reportFill.setReportId(reportId);
        reportFill.setJsonContent(jsonConetns);
        if(reportFillList.size() > 0) {
            reportFill.setId(reportFillList.get(0).getId());
        }

        boolean res = this.saveOrUpdate(reportFill);
        return res;
    }

    @Override
    public String getReportFillLast(Long reportId) {
        if(Objects.isNull(reportId)) {
            return "";
        }
        LambdaQueryWrapper<ReportFill> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReportFill::getReportId, reportId);
        queryWrapper.orderByDesc(ReportFill::getCreateTime);
        queryWrapper.select(ReportFill::getReportId, ReportFill::getJsonContent);
        List<ReportFill> reportFillList = this.list(queryWrapper);

        if(reportFillList.size() > 0) {
            ReportFill reportFill = reportFillList.get(0);
            return reportFill.getJsonContent();
        }

        return "";
    }
}
