package com.hzya.frame.sysnew.sysInterface.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.sysnew.person.entity.SysPersonEntity;
import com.hzya.frame.sysnew.popedomInterface.dao.ISysPopedomInterfaceDao;
import com.hzya.frame.sysnew.popedomInterface.entity.SysPopedomInterfaceEntity;
import com.hzya.frame.sysnew.popedomInterface.service.impl.InterfaceCache;
import com.hzya.frame.sysnew.sysInterface.entity.SysInterfaceEntity;
import com.hzya.frame.sysnew.sysInterface.dao.ISysInterfaceDao;
import com.hzya.frame.sysnew.sysInterface.service.ISysInterfaceService;
import com.hzya.frame.sysnew.user.entity.SysUserEntity;
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.List;

/**
 * 系统接口表(SysInterface)表服务实现类
 *
 * @author makejava
 * @since 2024-04-09 08:53:28
 */
@Service(value = "sysInterfaceService")
public class SysInterfaceServiceImpl extends BaseService<SysInterfaceEntity, String> implements ISysInterfaceService {

    private ISysInterfaceDao sysInterfaceDao;
    @Resource
    private InterfaceCache interfaceCache;
    @Resource
    private ISysPopedomInterfaceDao sysPopedomInterfaceDao;
    @Autowired
    public void setSysInterfaceDao(ISysInterfaceDao dao) {
        this.sysInterfaceDao = 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) {
        SysInterfaceEntity entity = getData("jsonStr", jsonObject, SysInterfaceEntity.class);
        //判断分页
        if (entity == null || entity.getPageNum() == null || entity.getPageSize() == null) {
            return BaseResult.getFailureMessageEntity("分页查询参数不存在");
        }
        PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
        List<SysInterfaceEntity> list = sysInterfaceDao.queryByLike(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) {
        SysInterfaceEntity entity = getData("jsonStr", jsonObject, SysInterfaceEntity.class);
        if(entity == null ){
            entity = new SysInterfaceEntity();
        }
        List<SysInterfaceEntity> list = sysInterfaceDao.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) {
        SysInterfaceEntity entity = getData("jsonStr", jsonObject, SysInterfaceEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getName() == null || "".equals(entity.getName())) {
            return BaseResult.getFailureMessageEntity("请先输入接口名称");
        }
        if (entity.getBeanName() == null || "".equals(entity.getBeanName())) {
            return BaseResult.getFailureMessageEntity("请先输入服务名");
        }
        if (entity.getInterfaceName() == null || "".equals(entity.getInterfaceName())) {
            return BaseResult.getFailureMessageEntity("请先输入方法名");
        }
        if(!checksave(entity)){
            return BaseResult.getFailureMessageEntity("当前接口已存在，请勿重复配置");
        }
        entity.setState("0");
        entity.setCreate();
        //保存
        sysInterfaceDao.save(entity);
        interfaceCache.reloadData("6");
        return BaseResult.getSuccessMessageEntity("保存接口成功", entity);
    }
    /**
     * @Author lvleigang
     * @Description  判断当前接口是否已经被使用
     * @Date 10:04 上午 2024/3/28
     * @param entity
     * @return boolean
     **/
    private boolean checksave(SysInterfaceEntity entity) {
        SysInterfaceEntity checkEntity = new SysInterfaceEntity();
        checkEntity.setBeanName(entity.getBeanName());
        checkEntity.setInterfaceName(entity.getInterfaceName());
        if(sysInterfaceDao.getCount(checkEntity) > 0){
            return false;
        }
        return true;
    }
    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 获取接口
     * @Date 2:36 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity getEntity(JSONObject jsonObject) {
        SysInterfaceEntity entity = getData("jsonStr", jsonObject, SysInterfaceEntity.class);
        if (entity == null) {
            return BaseResult.getFailureMessageEntity("参数不允许为空");
        }
        if (entity.getId() == null || "".equals(entity.getId())) {
            return BaseResult.getFailureMessageEntity("系统错误");
        }
        entity = sysInterfaceDao.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) {
        SysInterfaceEntity entity = getData("jsonStr", jsonObject, SysInterfaceEntity.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.getBeanName() == null || "".equals(entity.getBeanName())) {
            return BaseResult.getFailureMessageEntity("请先输入服务名");
        }
        if (entity.getInterfaceName() == null || "".equals(entity.getInterfaceName())) {
            return BaseResult.getFailureMessageEntity("请先输入方法名");
        }
        entity.setUpdate();
        //保存
        sysInterfaceDao.update(entity);
        interfaceCache.reloadData("6");

        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 deleteEntity(JSONObject jsonObject) {
        SysInterfaceEntity entity = getData("jsonStr", jsonObject, SysInterfaceEntity.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();
        sysInterfaceDao.logicRemove(entity);
        interfaceCache.reloadData("6");

        return BaseResult.getSuccessMessageEntity("删除接口成功");
    }

    /**
     * @Author lvleigang
     * @Description  判断当前接口是否已经被使用
     * @Date 10:04 上午 2024/3/28
     * @param entity
     * @return boolean
     **/
    private boolean checkUse(SysInterfaceEntity entity) {
        SysPopedomInterfaceEntity checkEntity = new SysPopedomInterfaceEntity();
        checkEntity.setInterfaceId(entity.getId());
        if(sysPopedomInterfaceDao.getCount(checkEntity) > 0){
            return false;
        }
        return true;
    }

    /**
     * @param jsonObject
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 启用停用接口
     * @Date 2:37 下午 2024/3/27
     **/
    @Override
    public JsonResultEntity enableDisableEntity(JSONObject jsonObject) {
        SysInterfaceEntity entity = getData("jsonStr", jsonObject, SysInterfaceEntity.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("系统错误");
        }
        //0启用 1 停用
        if("0".equals(entity.getState())){
            entity.setUpdate();
            sysInterfaceDao.update(entity);
            interfaceCache.reloadData("6");

            return BaseResult.getSuccessMessageEntity("启用接口成功");
        }else {
            entity.setUpdate();
            sysInterfaceDao.update(entity);
            interfaceCache.reloadData("6");

            return BaseResult.getSuccessMessageEntity("停用接口成功");
        }
    }
}
