package com.hzya.frame.report.reportClassify.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.report.reportClassify.entity.ReportClassifyEntity;
import com.hzya.frame.report.reportClassify.dao.IReportClassifyDao;
import com.hzya.frame.report.reportClassify.service.IReportClassifyService;
import com.hzya.frame.report.reportManage.dao.IReportManageDao;
import com.hzya.frame.report.reportManage.entity.ReportManageEntity;
import com.hzya.frame.sysnew.organ.entity.SysOrganEntity;
import com.hzya.frame.sysnew.person.entity.SysPersonEntity;
import com.hzya.frame.sysnew.userCompany.entity.SysUserCompanyEntity;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;

import com.hzya.frame.basedao.service.impl.BaseService;

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

/**
 * 报表分类表(ReportClassify)表服务实现类
 *
 * @author makejava
 * @since 2024-05-21 15:03:14
 */
@Service(value = "reportClassifyService")
public class ReportClassifyServiceImpl extends BaseService<ReportClassifyEntity, String> implements IReportClassifyService {

    private IReportClassifyDao reportClassifyDao;

    @Resource
	private IReportManageDao reportManageDao;
    @Autowired
    public void setReportClassifyDao(IReportClassifyDao dao) {
        this.reportClassifyDao = dao;
        this.dao = dao;
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 查询分类树
     * @Date 3:14 下午 2024/5/21
     **/
    @Override
    public JsonResultEntity queryEntityTree(JSONObject jsonObject) {
        ReportClassifyEntity entity = getData("jsonStr", jsonObject, ReportClassifyEntity.class);
        if (entity == null) {
            entity = new ReportClassifyEntity();
        }
        List<ReportClassifyEntity> returnEntity = new ArrayList<>();
        //查询符合条件的组织数据
        List<ReportClassifyEntity> reportClassifyEntities = reportClassifyDao.queryByLike(entity);
        if (reportClassifyEntities == null || reportClassifyEntities.size() == 0) {
            ReportClassifyEntity first = new ReportClassifyEntity();
            first.setCode("1");
            first.setName("系统");
            first.setParentId("0");
            first.setCreate();
            reportClassifyDao.save(first);
            reportClassifyEntities = new ArrayList<>();
            reportClassifyEntities.add(first);
        }
        if (reportClassifyEntities != null && reportClassifyEntities.size() > 0) {
            for (int i = 0; i < reportClassifyEntities.size(); i++) {
                if ("0".equals(reportClassifyEntities.get(i).getParentId())) {
                    recursiveSetTree(reportClassifyEntities.get(i), reportClassifyEntities);
                    reportClassifyEntities.get(i).setLabel("(" + reportClassifyEntities.get(i).getCode() + ")" + reportClassifyEntities.get(i).getName());
                    returnEntity.add(reportClassifyEntities.get(i));
                }
            }
        }
        return BaseResult.getSuccessMessageEntity("查询组织树成功", returnEntity);
    }

    /**
     * @param reportClassifyEntity
     * @param reportClassifyEntityList
     * @return void
     * @Author lvleigang
     * @Description 递归设置树
     * @Date 9:19 上午 2024/3/28
     **/
    private void recursiveSetTree(ReportClassifyEntity reportClassifyEntity, List<ReportClassifyEntity> reportClassifyEntityList) {
        List<ReportClassifyEntity> children = new ArrayList<>();
        for (int i = 0; i < reportClassifyEntityList.size(); i++) {
            if (reportClassifyEntityList.get(i).getParentId() != null && !"".equals(reportClassifyEntityList.get(i).getParentId())) {
                if (reportClassifyEntity.getId().equals(reportClassifyEntityList.get(i).getParentId())) {
                    reportClassifyEntityList.get(i).setLabel("(" + reportClassifyEntityList.get(i).getCode() + ")" + reportClassifyEntityList.get(i).getName());
                    children.add(reportClassifyEntityList.get(i));
                    recursiveSetTree(reportClassifyEntityList.get(i), reportClassifyEntityList);
                }
            }
        }
        reportClassifyEntity.setChildren(children);
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 保存分类
     * @Date 3:16 下午 2024/5/21
     **/
    @Override
    public JsonResultEntity saveEntity(JSONObject jsonObject) {
        ReportClassifyEntity entity = getData("jsonStr", jsonObject, ReportClassifyEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getCode() == null || "".equals(entity.getCode())) {
            return BaseResult.getFailureMessageEntity("请先输入编码");
        }
        if (entity.getName() == null || "".equals(entity.getName())) {
            return BaseResult.getFailureMessageEntity("请先输入名称");
        }
        if (entity.getParentId() == null || "".equals(entity.getParentId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity.setCreate();
        //保存
        reportClassifyDao.save(entity);
        return BaseResult.getSuccessMessageEntity("保存分类成功", entity);
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 获取分类
     * @Date 3:16 下午 2024/5/21
     **/
    @Override
    public JsonResultEntity getEntity(JSONObject jsonObject) {
        ReportClassifyEntity entity = getData("jsonStr", jsonObject, ReportClassifyEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity = reportClassifyDao.get(entity.getId());
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("获取组织失败");
        }
        if (entity.getParentId() != null && !"0".equals(entity.getParentId())) {
            ReportClassifyEntity parentEntity = reportClassifyDao.get(entity.getParentId());
            if (entity != null) {
                entity.setParentName(parentEntity.getName());
            }
        }
        return BaseResult.getSuccessMessageEntity("获取分类成功", entity);
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 修改分类
     * @Date 3:16 下午 2024/5/21
     **/
    @Override
    public JsonResultEntity updateEntity(JSONObject jsonObject) {
        ReportClassifyEntity entity = getData("jsonStr", jsonObject, ReportClassifyEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getName() == null || "".equals(entity.getName())) {
            return BaseResult.getFailureMessageEntity("请先输入名称");
        }
        if (entity.getCode() == null || "".equals(entity.getCode())) {
            return BaseResult.getFailureMessageEntity("请先输入编码");
        }
        if (entity.getParentId() == null || "".equals(entity.getParentId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity.setUpdate();
        //保存
        reportClassifyDao.update(entity);
        return BaseResult.getSuccessMessageEntity("修改分类成功", entity);
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 删除分类
     * @Date 3:16 下午 2024/5/21
     **/
    @Override
    public JsonResultEntity deleteEntity(JSONObject jsonObject) {
		ReportClassifyEntity entity = getData("jsonStr", jsonObject, ReportClassifyEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (!checkUse(entity)) {
            return BaseResult.getFailureMessageEntity("当前组织已经使用，不允许删除");
        }
        entity.setUpdate();
		reportClassifyDao.logicRemove(entity);
        return BaseResult.getSuccessMessageEntity("删除分类成功");
    }

	/**
	 * @Author lvleigang
	 * @Description  校验分类下面是否有配置报表
	 * @Date 3:27 下午 2024/3/27
	 * @param entity
	 * @return boolean
	 **/
	private boolean checkUse(ReportClassifyEntity entity) {
		ReportManageEntity reportManageEntity = new ReportManageEntity();
		reportManageEntity.setClassifyId(entity.getId());
		if(reportManageDao.getCount(reportManageEntity) > 0){
			return false;
		}
		return true;
	}
}
