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

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.basedao.service.impl.BaseService;
import com.hzya.frame.report.reportManage.dao.IReportManageDao;
import com.hzya.frame.report.reportManage.entity.JimuReportEntity;
import com.hzya.frame.report.reportManage.entity.ReportManageEntity;
import com.hzya.frame.report.reportManage.service.IReportManageService;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 报表管理表(ReportManage)表服务实现类
 *
 * @author makejava
 * @since 2024-05-21 15:04:12
 */
@Service(value = "reportManageService")
public class ReportManageServiceImpl extends BaseService<ReportManageEntity, String> implements IReportManageService {

    private IReportManageDao reportManageDao;

    @Autowired
    public void setReportManageDao(IReportManageDao dao) {
        this.reportManageDao = dao;
        this.dao = dao;
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 查询报表列表分页
     * @Date 11:52 上午 2024/3/27
     **/
    @Override
    public JsonResultEntity queryEntityPage(JSONObject jsonObject) {
        ReportManageEntity entity = getData("jsonStr", jsonObject, ReportManageEntity.class);
        //判断分页
        if (entity == null || entity.getPageNum() == null || entity.getPageSize() == null) {
            return BaseResult.getFailureMessageEntity("分页查询参数不存在");
        }
        PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
        List<ReportManageEntity> list = reportManageDao.queryByLikeClassify(entity);
        PageInfo pageInfo = new PageInfo(list);
        return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 查询报表列表
     * @Date 11:52 上午 2024/3/27
     **/
    @Override
    public JsonResultEntity queryEntity(JSONObject jsonObject) {
        ReportManageEntity entity = getData("jsonStr", jsonObject, ReportManageEntity.class);
        if (entity == null) {
            entity = new ReportManageEntity();
        }
        List<ReportManageEntity> list = reportManageDao.queryByLike(entity);
        return BaseResult.getSuccessMessageEntity("查询数据成功", list);
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 保存报表
     * @Date 2:36 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity saveEntity(JSONObject jsonObject) {
        ReportManageEntity entity = getData("jsonStr", jsonObject, ReportManageEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getReportId() == null || "".equals(entity.getReportId())) {
            return BaseResult.getFailureMessageEntity("请先选择报表");
        }
        if (entity.getReportName() == null || "".equals(entity.getReportName())) {
            return BaseResult.getFailureMessageEntity("请先输入报表名称");
        }
        if (entity.getClassifyId() == null || "".equals(entity.getClassifyId())) {
            return BaseResult.getFailureMessageEntity("请先选择分类");
        }
        if (entity.getPreviewAddress() == null || "".equals(entity.getPreviewAddress())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity.setState("0");
        if (!checkIsUse(entity)) {
            return BaseResult.getFailureMessageEntity("当前报表已经配置，请勿重复配置");
        }
        entity.setCreate();
        //保存
        reportManageDao.save(entity);
        return BaseResult.getSuccessMessageEntity("保存报表成功", entity);
    }


    private boolean checkIsUse(ReportManageEntity entity) {
        ReportManageEntity checkUse = new ReportManageEntity();
        checkUse.setReportId(entity.getReportId());
        return reportManageDao.getCount(checkUse) <= 0;
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 获取报表
     * @Date 2:36 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity getEntity(JSONObject jsonObject) {
        ReportManageEntity entity = getData("jsonStr", jsonObject, ReportManageEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity = reportManageDao.get(entity.getId());
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("获取报表失败");
        }
        return BaseResult.getSuccessMessageEntity("获取报表成功", entity);
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 修改报表
     * @Date 2:37 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity updateEntity(JSONObject jsonObject) {
        ReportManageEntity entity = getData("jsonStr", jsonObject, ReportManageEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getReportId() == null || "".equals(entity.getReportId())) {
            return BaseResult.getFailureMessageEntity("请先选择报表");
        }
        if (entity.getReportName() == null || "".equals(entity.getReportName())) {
            return BaseResult.getFailureMessageEntity("请先输入报表名称");
        }
        if (entity.getClassifyId() == null || "".equals(entity.getClassifyId())) {
            return BaseResult.getFailureMessageEntity("请先选择分类");
        }
        if (entity.getPreviewAddress() == null || "".equals(entity.getPreviewAddress())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity.setUpdate();
        //保存
        reportManageDao.update(entity);
        return BaseResult.getSuccessMessageEntity("修改报表成功");
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 删除报表
     * @Date 2:37 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity deleteEntity(JSONObject jsonObject) {
        ReportManageEntity entity = getData("jsonStr", jsonObject, ReportManageEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity.setUpdate();
        reportManageDao.logicRemove(entity);
        return BaseResult.getSuccessMessageEntity("删除报表成功");
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 启用停用报表
     * @Date 2:37 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity enableDisableEntity(JSONObject jsonObject) {
        ReportManageEntity entity = getData("jsonStr", jsonObject, ReportManageEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        if (entity.getState() == null || "".equals(entity.getState())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity.setUpdate();
        reportManageDao.update(entity);
        //0启用 1 停用
        if ("0".equals(entity.getState())) {
            return BaseResult.getSuccessMessageEntity("启用用户成功");
        } else {
            return BaseResult.getSuccessMessageEntity("停用用户成功");
        }
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 查询报表
     * @Date 2:36 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity queryReport(JSONObject jsonObject) {
        JimuReportEntity entity = getData("jsonStr", jsonObject, JimuReportEntity.class);
        //判断分页
        if (entity == null || entity.getPageNum() == null || entity.getPageSize() == null) {
            return BaseResult.getFailureMessageEntity("分页查询参数不存在");
        }
        PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
        List<JimuReportEntity> list = reportManageDao.queryReportNotUse(entity);
        PageInfo pageInfo = new PageInfo(list);
        return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 查询报表
     * @Date 2:36 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity queryReportEntity(JSONObject jsonObject) {
        JimuReportEntity entity = getData("jsonStr", jsonObject, JimuReportEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity = reportManageDao.queryReportEntity(entity);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("获取报表失败");
        }
        return BaseResult.getSuccessMessageEntity("获取报表成功", entity);

    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 权限查询
     * @Date 2:37 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity jurisdiction(JSONObject jsonObject) {


        return null;
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 保存权限查询
     * @Date 2:37 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity saveJurisdiction(JSONObject jsonObject) {

        return null;
    }
}
