package com.hzya.frame.sysnew.plugArg.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.basedao.service.impl.BaseService;
import com.hzya.frame.sysnew.plugArgDeatil.dao.ISysPlugArgDetailDao;
import com.hzya.frame.sysnew.plugArgDeatil.entity.SysPlugArgDetailEntity;
import com.hzya.frame.sysnew.plugArg.dao.ISysPlugArgDao;
import com.hzya.frame.sysnew.plugArg.entity.SysPlugArgEntity;
import com.hzya.frame.sysnew.plugArg.service.ISysPlugArgService;
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.Date;
import java.util.List;

/**
 * 参数设置主表(SysPlugArg)表服务实现类
 *
 * @author makejava
 * @since 2024-03-18 14:32:30
 */
@Service(value = "sysPlugArgService")
public class SysPlugArgServiceImpl extends BaseService<SysPlugArgEntity, String> implements ISysPlugArgService {

    private ISysPlugArgDao sysPlugArgDao;
    @Autowired
    private ISysPlugArgDetailDao sysPlugArgDetailDao;

    @Autowired
    public void setSysPlugArgDao(ISysPlugArgDao dao) {
        this.sysPlugArgDao = dao;
        this.dao = dao;
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 查询配置的插件参数列表
     * @Date 2:45 下午 2024/3/18
     **/
    @Override
    public JsonResultEntity queryPlugArg(JSONObject object) {
		SysPlugArgEntity entity = getData("jsonStr", object, SysPlugArgEntity.class);
		//判断分页
		if (entity == null || entity.getPageNum() == null || entity.getPageSize() == null) {
			return BaseResult.getFailureMessageEntity("分页查询参数不存在");
		}
		PageHelper.startPage(entity.getPageNum(), entity.getPageSize());
		List<SysPlugArgEntity> sysPlugArgEntityList = sysPlugArgDao.queryByLike(entity);
		PageInfo pageInfo = new PageInfo(sysPlugArgEntityList);
		return BaseResult.getSuccessMessageEntity("查询数据成功", pageInfo);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 保存配置的插件参数列表
     * @Date 2:45 下午 2024/3/18
     **/
    @Override
    public JsonResultEntity savePlugArg(JSONObject object) {
		SysPlugArgEntity entity = getData("jsonStr", object, SysPlugArgEntity.class);
		if (entity == null) {
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if (entity.getPlugId() == null || "".equals(entity.getPlugId())) {
			return BaseResult.getFailureMessageEntity("请先选择插件");
		}
		if (entity.getSysPlugArgDetailEntityList() == null || entity.getSysPlugArgDetailEntityList().size() == 0 ) {
			return BaseResult.getFailureMessageEntity("请先输入插件对应的参数");
		}
		SysPlugArgEntity checkEntity = new SysPlugArgEntity();
		checkEntity.setSts("Y");
		checkEntity.setPlugId(entity.getPlugId());
		int checkCount = sysPlugArgDao.getCount(checkEntity);
		if (checkCount > 0) {
			return BaseResult.getFailureMessageEntity("插件已存在");
		}
		entity.setCreate();
		//保存主表
		sysPlugArgDao.save(entity);
		//保存明细表
		for (int i = 0; i < entity.getSysPlugArgDetailEntityList().size(); i++) {
			SysPlugArgDetailEntity sysPlugArgDetailEntity = entity.getSysPlugArgDetailEntityList().get(i);
			sysPlugArgDetailEntity.setFormmainId(entity.getId());
			sysPlugArgDetailEntity.setCreate();
			sysPlugArgDetailDao.save(sysPlugArgDetailEntity);
		}
		return BaseResult.getSuccessMessageEntity("保存插件参数成功", entity.getId());
    }


    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 保存配置的插件参数列表
     * @Date 2:45 下午 2024/3/18
     **/
    @Override
    public JsonResultEntity updatePlugArg(JSONObject object) {
		SysPlugArgEntity entity = getData("jsonStr", object, SysPlugArgEntity.class);
		if (entity == null) {
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if (entity.getId() == null || "".equals(entity.getId())) {
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		if (entity.getPlugId() == null || "".equals(entity.getPlugId())) {
			return BaseResult.getFailureMessageEntity("请先选择插件");
		}
		if (entity.getSysPlugArgDetailEntityList() == null || entity.getSysPlugArgDetailEntityList().size() == 0 ) {
			return BaseResult.getFailureMessageEntity("请先输入插件对应的参数");
		}
		entity.setUpdate();
		//保存主表
		sysPlugArgDao.update(entity);
		//保存明细表
		//先删除明细
		SysPlugArgDetailEntity deleteEntity = new SysPlugArgDetailEntity();
		deleteEntity.setFormmainId(entity.getId());
		deleteEntity.setModify_time(new Date());
		deleteEntity.setModify_user_id(StpUtil.getLoginIdAsString());
		sysPlugArgDetailDao.logicRemoveMultiCondition(deleteEntity);
		for (int i = 0; i < entity.getSysPlugArgDetailEntityList().size(); i++) {
			SysPlugArgDetailEntity sysPlugArgDetailEntity = entity.getSysPlugArgDetailEntityList().get(i);
			sysPlugArgDetailEntity.setFormmainId(entity.getId());
			sysPlugArgDetailEntity.setCreate();
			sysPlugArgDetailDao.save(sysPlugArgDetailEntity);
		}
		return BaseResult.getSuccessMessageEntity("修改插件参数成功", entity.getId());
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 删除配置的插件参数列表
     * @Date 2:45 下午 2024/3/18
     **/
    @Override
    public JsonResultEntity deletePlugArg(JSONObject object) {
		SysPlugArgEntity entity = getData("jsonStr", object, SysPlugArgEntity.class);
		if (entity == null) {
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if (entity.getId() == null || "".equals(entity.getId())) {
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		entity.setUpdate();
		//保存主表
		sysPlugArgDao.logicRemoveMultiCondition(entity);
		//先删除明细
		SysPlugArgDetailEntity deleteEntity = new SysPlugArgDetailEntity();
		deleteEntity.setFormmainId(entity.getId());
		deleteEntity.setModify_time(new Date());
		deleteEntity.setModify_user_id(StpUtil.getLoginIdAsString());
		sysPlugArgDetailDao.logicRemoveMultiCondition(deleteEntity);

		return BaseResult.getSuccessMessageEntity("删除插件参数成功", entity.getId());
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 获取配置的插件参数列表
     * @Date 2:45 下午 2024/3/18
     **/
    @Override
    public JsonResultEntity getPlugArg(JSONObject object) {
		SysPlugArgEntity entity = getData("jsonStr", object, SysPlugArgEntity.class);
		if (entity == null) {
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if (entity.getId() == null || "".equals(entity.getId())) {
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		//保存主表
		entity = sysPlugArgDao.get(entity.getId());
		if(entity == null ){
			return BaseResult.getFailureMessageEntity("获取插件参数失败");
		}
		SysPlugArgDetailEntity getEntity = new SysPlugArgDetailEntity();
		getEntity.setFormmainId(entity.getId());
		List<SysPlugArgDetailEntity> list = sysPlugArgDetailDao.queryBase(getEntity);
		entity.setSysPlugArgDetailEntityList(list);
		return BaseResult.getSuccessMessageEntity("获取插件参数成功", entity);
    }

    /**
     * @param object
     * @return com.hzya.frame.web.entity.JsonResultEntity
     * @Author lvleigang
     * @Description 获取配置的插件参数列表根据插件id
     * @Date 2:45 下午 2024/3/18
     **/
    @Override
    public JsonResultEntity getPlugArgByPlugId(JSONObject object) {
		SysPlugArgEntity entity = getData("jsonStr", object, SysPlugArgEntity.class);
		if (entity == null) {
			return BaseResult.getFailureMessageEntity("参数不允许为空");
		}
		if (entity.getPlugId() == null || "".equals(entity.getPlugId())) {
			return BaseResult.getFailureMessageEntity("系统错误");
		}
		//保存主表
		entity = sysPlugArgDao.queryOne(entity);
		if(entity == null ){
			return BaseResult.getFailureMessageEntity("获取插件参数失败");
		}
		SysPlugArgDetailEntity getEntity = new SysPlugArgDetailEntity();
		getEntity.setFormmainId(entity.getId());
		List<SysPlugArgDetailEntity> list = sysPlugArgDetailDao.queryBase(getEntity);
		return BaseResult.getSuccessMessageEntity("获取插件参数成功", list);
    }
}
