package com.hzya.frame.sys.dataSource.service.impl;

import cn.dev33.satoken.stp.StpUtil;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hzya.frame.serviceUtil.DsDataSourceUtil;
import com.hzya.frame.sys.dataSource.entity.SysDataSourceEntity;
import com.hzya.frame.sys.dataSource.dao.ISysDataSourceDao;
import com.hzya.frame.sys.dataSource.entity.SysDataSourceVo;
import com.hzya.frame.sys.dataSource.service.ISysDataSourceService;
import com.hzya.frame.sysnew.application.entity.SysApplicationDatasourceDto;
import com.hzya.frame.util.AESUtil;
import com.hzya.frame.uuid.UUIDUtils;
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.Date;
import java.util.List;

/**
 * 数据源表(SysDataSource)表服务实现类
 *
 * @author makejava
 * @since 2023-09-05 16:07:13
 */
@Service(value = "sysDataSourceService")
public class SysDataSourceServiceImpl extends BaseService<SysDataSourceEntity, String> implements ISysDataSourceService {
     
    private ISysDataSourceDao sysDataSourceDao;
    @Resource
    private DsDataSourceUtil dsDataSourceUtil;

    @Autowired
	public void setSysDataSourceDao(ISysDataSourceDao dao) {
		this.sysDataSourceDao = dao;
		this.dao = dao;
	}
	/**
	 * @param object
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 * @Author lvleigang
	 * @Description 查询列表
	 * @Date 2:18 下午 2023/7/17
	 **/
	@Override
	public JsonResultEntity queryList(JSONObject object) {
		SysDataSourceEntity entity = getData("jsonStr", object, SysDataSourceEntity.class);
		List<SysDataSourceVo> sysDataSourceEntities = sysDataSourceDao.queryVoList(entity);
		return BaseResult.getSuccessMessageEntity("查询数据成功", sysDataSourceEntities);
	}

	/**
	 * @param object
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 * @Author lvleigang
	 * @Description 查询列表
	 * @Date 2:18 下午 2023/7/17
	 **/
	@Override
	public JsonResultEntity queryPage(JSONObject object) {
		SysDataSourceEntity entity = getData("jsonStr", object, SysDataSourceEntity.class);
		//判断分页
		if(entity == null || entity.getPageNum() == null || entity.getPageSize() == null){
			return BaseResult.getFailureMessageEntity("分页查询参数不存在");
		}
		PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
		List<SysDataSourceVo> sysDataSourceEntities = sysDataSourceDao.queryVoList(entity);
		if(sysDataSourceEntities != null && sysDataSourceEntities.size() > 0){
			for (int i = 0; i < sysDataSourceEntities.size(); i++) {
				sysDataSourceEntities.get(i).setPassword(AESUtil.decrypt(sysDataSourceEntities.get(i).getPassword()));
			}
		}
		PageInfo pageInfo = new PageInfo(sysDataSourceEntities);
		return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
	}

	/**
	 * @param object
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 * @Author lvleigang
	 * @Description 获取数据
	 * @Date 2:18 下午 2023/7/17
	 **/
	@Override
	public JsonResultEntity queryEntity(JSONObject object) {
		SysDataSourceEntity entity = getData("jsonStr", object, SysDataSourceEntity.class);
		//判断分页
		if(entity == null || entity.getId() == null || "".equals(entity.getId())){
			return BaseResult.getFailureMessageEntity("请先传递请求主键");
		}
		SysDataSourceEntity sysDataSourceEntity = sysDataSourceDao.queryOne(entity);
		sysDataSourceEntity.setPassword(AESUtil.decrypt(sysDataSourceEntity.getPassword()));
		return BaseResult.getSuccessMessageEntity("查询数据成功", sysDataSourceEntity);
	}

	/**
	 * @param object
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 * @Author lvleigang
	 * @Description 新增数据
	 * @Date 2:18 下午 2023/7/17
	 **/
	@Override
	public JsonResultEntity addEntity(JSONObject object) {
		SysDataSourceEntity entity = getData("jsonStr", object, SysDataSourceEntity.class);
		if(entity == null){
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if(entity.getSourceName() == null || "".equals(entity.getSourceName())){
			return BaseResult.getFailureMessageEntity("请先输入数据源名称");
		}
		if(entity.getSourceCode() == null || "".equals(entity.getSourceCode())){
			return BaseResult.getFailureMessageEntity("请先输入数据源编码");
		}
		SysDataSourceEntity checkEntity = new SysDataSourceEntity();
		checkEntity.setSts("Y");
		checkEntity.setSourceCode(entity.getSourceCode());
		int checkCount = sysDataSourceDao.getCount(checkEntity);
		if(checkCount > 0){
			return BaseResult.getFailureMessageEntity("数据源编码已存在");
		}
		setCreate(entity);
		if(entity.getUseState() == 1){
			try {
				dsDataSourceUtil.addDataSource(entity);
				entity.setDataState("1");
			} catch (Exception e) {
				entity.setDataState("0");
				//return BaseResult.getFailureMessageEntity("初始化数据源失败,请检查数据源配置是否正确");
			}
		}
		entity.setPassword(AESUtil.encrypt(entity.getPassword()));
		sysDataSourceDao.save(entity);
		return BaseResult.getSuccessMessageEntity("保存数据源成功",entity.getId());
	}
	private void setCreate(SysDataSourceEntity entity) {
		entity.setId(UUIDUtils.getUUID());
		entity.setSts("Y");
		entity.setCreate_user_id(StpUtil.getLoginIdAsString());
		entity.setModify_user_id(StpUtil.getLoginIdAsString());
		entity.setCreate_time(new Date());
		entity.setModify_time(new Date());
	}
	/**
	 * @param object
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 * @Author lvleigang
	 * @Description 修改数据
	 * @Date 2:18 下午 2023/7/17
	 **/
	@Override
	public JsonResultEntity editEntity(JSONObject object) {
		SysDataSourceEntity entity = getData("jsonStr", object, SysDataSourceEntity.class);
		if(entity == null){
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if(entity.getId() == null || "".equals(entity.getId())){
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		if(entity.getSourceName() == null || "".equals(entity.getSourceName())){
			return BaseResult.getFailureMessageEntity("请先输入数据源名称");
		}
		if(entity.getSourceCode() == null || "".equals(entity.getSourceCode())){
			return BaseResult.getFailureMessageEntity("请先输入数据源编码");
		}
		SysDataSourceEntity checkEntity = new SysDataSourceEntity();
		checkEntity.setSts("Y");
		checkEntity.setId(entity.getId());
		checkEntity.setSourceCode(entity.getSourceCode());
		int checkCount = sysDataSourceDao.entity_count_not_id(checkEntity);
		if(checkCount > 0){
			return BaseResult.getFailureMessageEntity("接口编码已存在");
		}
		entity.setModify_user_id(StpUtil.getLoginIdAsString());
		entity.setModify_time(new Date());
		entity.setSts("Y");
		if(entity.getUseState() == 1){
			try {
				dsDataSourceUtil.editDataSource(entity);
			} catch (Exception e) {
				//return BaseResult.getSuccessMessageEntity("修改数据源失败，请检查数据源配置是否正确");
			}
		}else {
			try {
				dsDataSourceUtil.removeDataSource(entity.getSourceCode());
			} catch (Exception e) {
				return BaseResult.getSuccessMessageEntity("修改数据源失败");
			}
		}
		if(entity.getPassword() != null ){
			entity.setPassword(AESUtil.encrypt(entity.getPassword()));
		}
		sysDataSourceDao.update(entity);
		return BaseResult.getSuccessMessageEntity("修改数据源成功",entity.getId());

	}
	/**
	 * @param object
	 * @return
	 * @Author lvleigang
	 * @Description 数据源测试接口 测试当前页面数据源是否配置正确
	 * @Date 2:08 下午 2023/9/23
	 **/
	@Override
	public JsonResultEntity testDatabase(JSONObject object) {
		SysDataSourceEntity entity = getData("jsonStr", object, SysDataSourceEntity.class);
		if (entity == null) {
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		SysApplicationDatasourceDto sysApplicationDatasourceDto = new SysApplicationDatasourceDto();
		sysApplicationDatasourceDto.setSourceType(entity.getSourceType());
		sysApplicationDatasourceDto.setSourceUrl(entity.getSourceUrl());
		sysApplicationDatasourceDto.setLoginName(entity.getLoginName());
		sysApplicationDatasourceDto.setPassword(entity.getPassword());
		if (dsDataSourceUtil.testConnection(sysApplicationDatasourceDto)) {
			return BaseResult.getSuccessMessageEntity("测试连接数据源成功",true);
		} else {
			return BaseResult.getSuccessMessageEntity("测试连接数据源失败",false);
		}
	}
	/**
	 * @param object
	 * @return com.hzya.frame.web.entity.JsonResultEntity
	 * @Author lvleigang
	 * @Description 删除数据
	 * @Date 2:18 下午 2023/7/17
	 **/
	@Override
	public JsonResultEntity deleteEntity(JSONObject object) {
		SysDataSourceEntity entity = getData("jsonStr", object, SysDataSourceEntity.class);
		//判断编码是否重复
		if (entity == null) {
			return BaseResult.getFailureMessageEntity("请先填写内容");
		}
		if (entity.getId() == null || "".equals(entity.getId())) {
			return BaseResult.getFailureMessageEntity("请先选择数据");
		}
		SysDataSourceEntity deleteEntity = sysDataSourceDao.queryOne(entity);
		//删除主表和明细表
		entity.setModify_user_id(StpUtil.getLoginIdAsString());
		entity.setModify_time(new Date());
		entity.setSts("Y");
		try {
			dsDataSourceUtil.removeDataSource(deleteEntity.getSourceCode());
		} catch (Exception e) {
			return BaseResult.getSuccessMessageEntity("删除数据源失败");
		}
		sysDataSourceDao.logicRemoveMultiCondition(entity);
		return BaseResult.getSuccessMessageEntity("删除成功");
	}
}
