package com.yunyan.business.service.impl;

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunyan.business.document.y22.pojo.finalExcelY22.FinalP1Vo;
import com.yunyan.business.domain.ReportDept;
import com.yunyan.business.mapper.ReportDeptMapper;
import com.yunyan.business.pojo.admin.dto.SysDeptReportEditDto;
import com.yunyan.business.service.IReportDeptService;
import com.yunyan.common.core.domain.AjaxResultVo;
import com.yunyan.common.core.domain.entity.SysDept;
import com.yunyan.common.utils.StringUtils;
import com.yunyan.system.service.ISysDeptService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * 报告-部门概况数据Service业务层处理
 *
 * @author yunyan
 * @date 2023-12-07
 */
@Slf4j
@Service
public class ReportDeptServiceImpl extends ServiceImpl<ReportDeptMapper, ReportDept> implements IReportDeptService
{
    @Autowired
    private ReportDeptMapper reportDeptMapper;

    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 查询报告-部门概况数据列表
     *
     * @param reportDept 报告-部门概况数据
     * @return 报告-部门概况数据
     */    @Override
    public List<ReportDept> selectReportDeptList(ReportDept reportDept)
    {
        return reportDeptMapper.selectReportDeptList(reportDept);
    }

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

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

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

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

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

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

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

        return AjaxResultVo.success(entity);
    }

    @Override
    public FinalP1Vo saveForUpdateNewDept(SysDept sysDept, String year, Integer type, boolean existRead) {

        FinalP1Vo finalP1Vo = new FinalP1Vo();

        Long deptId = sysDept.getDeptId();
        /*if(null == deptId ) {
            return finalP1Vo;
        }*/
        /*if(null == reportId ) {
            return finalP1Vo;
        }*/
        if(StringUtils.isBlank(year)) {
            return finalP1Vo;
        }
        if(null == type) {
            return finalP1Vo;
        }

        LambdaQueryWrapper<ReportDept> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReportDept::getDeptId, deptId);
        //queryWrapper.eq(ReportDept::getReportId, reportId);
        queryWrapper.eq(ReportDept::getYear, year);
        queryWrapper.eq(ReportDept::getType, type);
        queryWrapper.orderByDesc(ReportDept::getCreateTime);
        List<ReportDept> list = this.list(queryWrapper);
        Long id = null;
        if(list.size() > 0) {
            ReportDept reportDept = list.get(0);
            id = reportDept.getId();

            if(existRead) {
                //存在就只读, 不生成
                finalP1Vo = JSONUtil.toBean(reportDept.getJsonContent(), FinalP1Vo.class);
                return finalP1Vo;
            }
        }

        //SysDept sysDept = sysDeptService.getById(deptId);

        if(null != sysDept) {

            String settings = sysDept.getSettings();
            //内设机构无个：。
            if( StringUtils.isNotBlank(settings) && settings.contains("内设机构无个：。")) {
                settings = settings.replace("内设机构无个：。", "内设机构0个。");
            }
            if( StringUtils.isNotBlank(settings) && settings.contains("内设机构0个：。")) {
                settings = settings.replace("内设机构0个：。", "内设机构0个。");
            }
            if( StringUtils.isNotBlank(settings) && settings.contains("内设机构0个：无。")) {
                settings = settings.replace("内设机构0个：无。", "内设机构0个。");
            }
            finalP1Vo.setSettings(settings);
            finalP1Vo.setDuty(sysDept.getDuty());
            finalP1Vo.setBudgetInfo(sysDept.getBudgetInfo());


            String jsonContent = JSONUtil.toJsonStr(finalP1Vo);

            //保存 报告名词解释
            ReportDept reportDept = new ReportDept();
            reportDept.setType(type);
            reportDept.setYear(year);
            reportDept.setDeptId(deptId);
            //reportDept.setReportId(reportId);
            reportDept.setJsonContent(jsonContent);
            if(null != id) {
                reportDept.setId(id);
                reportDept.setUpdateTime(new Date());
            } else {
                reportDept.setCreateTime(new Date());
            }

            this.saveOrUpdate(reportDept);
        }

        return finalP1Vo;
    }

    @Override
    public FinalP1Vo getDeptInfo(Long deptId, String year, Integer type) {
        FinalP1Vo finalP1Vo = new FinalP1Vo();

        finalP1Vo.setSettings("");
        finalP1Vo.setDuty("");
        finalP1Vo.setBudgetInfo("");

        if(null == deptId ) {
            return finalP1Vo;
        }
        /*if(null == reportId ) {
            return finalP1Vo;
        }*/
        if(StringUtils.isBlank(year)) {
            return finalP1Vo;
        }
        if(null == type) {
            return finalP1Vo;
        }

        LambdaQueryWrapper<ReportDept> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReportDept::getDeptId, deptId);
        //queryWrapper.eq(ReportDept::getReportId, reportId);
        queryWrapper.eq(ReportDept::getYear, year);
        queryWrapper.eq(ReportDept::getType, type);
        queryWrapper.orderByDesc(ReportDept::getCreateTime);
        List<ReportDept> list = this.list(queryWrapper);
        if(list.size() > 0) {
            ReportDept reportDept = list.get(0);
            finalP1Vo = JSONUtil.toBean(reportDept.getJsonContent(), FinalP1Vo.class);
            return finalP1Vo;
        }

        return finalP1Vo;
    }

    @Override
    public AjaxResultVo changeDeptOverview(SysDeptReportEditDto dto) {
        FinalP1Vo finalP1Vo = new FinalP1Vo();

        Long deptId = dto.getDeptId();

        String year = dto.getYear();
        Integer type = dto.getType();

        LambdaQueryWrapper<ReportDept> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReportDept::getDeptId, deptId);
        queryWrapper.eq(ReportDept::getYear, year);
        queryWrapper.eq(ReportDept::getType, type);
        queryWrapper.orderByDesc(ReportDept::getCreateTime);
        List<ReportDept> list = this.list(queryWrapper);
        Long id = null;

        BeanUtils.copyProperties(dto, finalP1Vo);
        if(list.size() > 0) {
            ReportDept reportDept = list.get(0);
            id = reportDept.getId();
        }

        String jsonContent = JSONUtil.toJsonStr(finalP1Vo);

        //保存 报告名词解释
        ReportDept reportDept = new ReportDept();
        reportDept.setType(type);
        reportDept.setYear(year);
        reportDept.setDeptId(deptId);
        reportDept.setJsonContent(jsonContent);
        if(null != id) {
            reportDept.setId(id);
            reportDept.setUpdateTime(new Date());
        } else {
            reportDept.setCreateTime(new Date());
        }

        this.saveOrUpdate(reportDept);

        return AjaxResultVo.success();
    }

    @Override
    public AjaxResultVo changeDeptOverview(FinalP1Vo finalP1Vo, Long deptId, String year, Integer type) {
        //FinalP1Vo finalP1Vo = new FinalP1Vo();


        LambdaQueryWrapper<ReportDept> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ReportDept::getDeptId, deptId);
        queryWrapper.eq(ReportDept::getYear, year);
        queryWrapper.eq(ReportDept::getType, type);
        queryWrapper.orderByDesc(ReportDept::getCreateTime);
        List<ReportDept> list = this.list(queryWrapper);
        Long id = null;

        if(list.size() > 0) {
            ReportDept reportDept = list.get(0);
            id = reportDept.getId();
        }

        String jsonContent = JSONUtil.toJsonStr(finalP1Vo);

        //保存 报告名词解释
        ReportDept reportDept = new ReportDept();
        reportDept.setType(type);
        reportDept.setYear(year);
        reportDept.setDeptId(deptId);
        reportDept.setJsonContent(jsonContent);
        if(null != id) {
            reportDept.setId(id);
            reportDept.setUpdateTime(new Date());
        } else {
            reportDept.setCreateTime(new Date());
        }

        this.saveOrUpdate(reportDept);

        return AjaxResultVo.success();
    }
}
