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

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.sysnew.buttonConfig.entity.SysButtonConfigEntity;
import com.hzya.frame.sysnew.buttonConfig.dao.ISysButtonConfigDao;
import com.hzya.frame.sysnew.buttonConfig.service.ISysButtonConfigService;
import com.hzya.frame.sysnew.menuConfig.entity.SysMenuConfigEntity;
import com.hzya.frame.sysnew.popedomOperate.dao.ISysPopedomOperateDao;
import com.hzya.frame.sysnew.popedomOperate.entity.SysPopedomOperateEntity;
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 com.hzya.frame.basedao.service.impl.BaseService;

import javax.annotation.Resource;
import java.util.List;

/**
 * 按钮配置表(SysButtonConfig)表服务实现类
 *
 * @author makejava
 * @since 2024-03-26 15:23:01
 */
@Service(value = "sysButtonConfigService")
public class SysButtonConfigServiceImpl extends BaseService<SysButtonConfigEntity, String> implements ISysButtonConfigService {
     
    private ISysButtonConfigDao sysButtonConfigDao;

    @Resource
	private ISysPopedomOperateDao sysPopedomOperateDao;

	@Autowired
	public void setSysButtonConfigDao(ISysButtonConfigDao dao) {
		this.sysButtonConfigDao = dao;
		this.dao = dao;
	}

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


	/**
	 * @Author lvleigang
	 * @Description  查询按钮列表
	 * @Date 11:52 上午 2024/3/27
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 **/
	@Override
	public JsonResultEntity queryEntity(JSONObject jsonObject) {
		SysButtonConfigEntity entity = getData("jsonStr", jsonObject, SysButtonConfigEntity.class);
		if(entity == null ){
			entity = new SysButtonConfigEntity();
		}
		List<SysButtonConfigEntity> sysApplicationVos = sysButtonConfigDao.queryByLike(entity);
		return BaseResult.getSuccessMessageEntity("查询数据成功", sysApplicationVos);
	}
	/**
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 * @Author lvleigang
	 * @Description 保存按钮
	 * @Date 2:36 下午 2024/3/27
	 **/
	@Override
	public JsonResultEntity saveEntity(JSONObject jsonObject) {
		SysButtonConfigEntity entity = getData("jsonStr", jsonObject, SysButtonConfigEntity.class);
		if (entity == null) {
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if (entity.getNameCh() == null || "".equals(entity.getNameCh())) {
			return BaseResult.getFailureMessageEntity("请先输入按钮名称");
		}
		if (entity.getMenuId() == null || "".equals(entity.getMenuId())) {
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		entity.setCreate();
		//保存
		sysButtonConfigDao.save(entity);
		return BaseResult.getSuccessMessageEntity("保存按钮成功", entity);
	}
	/**
	 * @param jsonObject
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 * @Author lvleigang
	 * @Description 获取按钮
	 * @Date 2:36 下午 2024/3/27
	 **/
	@Override
	public JsonResultEntity getEntity(JSONObject jsonObject) {
		SysButtonConfigEntity entity = getData("jsonStr", jsonObject, SysButtonConfigEntity.class);
		if (entity == null) {
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if (entity.getId() == null || "".equals(entity.getId())) {
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		entity = sysButtonConfigDao.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) {
		SysButtonConfigEntity entity = getData("jsonStr", jsonObject, SysButtonConfigEntity.class);
		if (entity == null) {
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if (entity.getId() == null || "".equals(entity.getId())) {
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		if (entity.getNameCh() == null || "".equals(entity.getNameCh())) {
			return BaseResult.getFailureMessageEntity("请先输入按钮名称");
		}
		if (entity.getMenuId() == null || "".equals(entity.getMenuId())) {
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		entity.setUpdate();
		//保存
		sysButtonConfigDao.update(entity);
		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) {
		SysButtonConfigEntity entity = getData("jsonStr", jsonObject, SysButtonConfigEntity.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();
		sysButtonConfigDao.logicRemove(entity);
		return BaseResult.getSuccessMessageEntity("删除按钮成功");
	}

	/**
	 * @Author lvleigang
	 * @Description  校验按钮是否使用 目前只校验（用户按钮、按钮权限配置下的）
	 * @Date 3:27 下午 2024/3/27
	 * @param entity
	 * @return boolean
	 **/
	private boolean checkUse(SysButtonConfigEntity entity) {
		SysPopedomOperateEntity sysPopedomOperateEntity = new SysPopedomOperateEntity();
		sysPopedomOperateEntity.setOperate(entity.getId());
		if(sysPopedomOperateDao.getCount(sysPopedomOperateEntity) > 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 getUserButton(JSONObject jsonObject) {
		SysButtonConfigEntity entity = getData("jsonStr", jsonObject, SysButtonConfigEntity.class);
		if (entity == null) {
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		if (entity.getUserId() == null || "".equals(entity.getUserId())) {
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		if (entity.getMenuId() == null || "".equals(entity.getMenuId())) {
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		List<SysButtonConfigEntity> sysButtonConfigEntities = sysButtonConfigDao.getUserButton(entity);
		return BaseResult.getSuccessMessageEntity("查询按钮成功",sysButtonConfigEntities);
	}
}
