package com.hzya.frame.sysnew.application.appAcount.service.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.hzya.frame.sysnew.application.appAcount.entity.SysApplicationAccountEntity;
import com.hzya.frame.sysnew.application.appAcount.dao.ISysApplicationAccountDao;
import com.hzya.frame.sysnew.application.appAcount.service.ISysApplicationAccountService;
import com.hzya.frame.web.entity.BaseResult;
import com.hzya.frame.web.entity.JsonResultEntity;
import org.apache.commons.collections.CollectionUtils;
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;

/**
 * 应用账户表(SysApplicationAccount)表服务实现类
 *
 * @author xiang2lin
 * @since 2025-05-10 15:52:26
 */
@Service(value = "sysApplicationAccountService")
public class SysApplicationAccountServiceImpl extends BaseService<SysApplicationAccountEntity, String> implements ISysApplicationAccountService {
     
    private ISysApplicationAccountDao sysApplicationAccountDao;
    
    @Autowired
	public void setSysApplicationAccountDao(ISysApplicationAccountDao dao) {
		this.sysApplicationAccountDao = dao;
		this.dao = dao;
	}

	/**
	 * 保存账户信息
	 *
	 * @param object
	 * @return
	 */
	@Override
	public JsonResultEntity saveAccount(JSONObject object) {
		SysApplicationAccountEntity entity =  getData("jsonStr", object, SysApplicationAccountEntity.class);
		try {
			checkParam(entity,"save");
		}catch (Exception e){
			return BaseResult.getFailureMessageEntity(e.getMessage());
		}
		//控制一下名字不能重复
		List<SysApplicationAccountEntity> queryList = queryByName(entity);
		if (CollectionUtils.isNotEmpty(queryList) && queryList.size() > 0){
			return BaseResult.getFailureMessageEntity(entity.getName()+"重复");
		}
		sysApplicationAccountDao.save(entity);
		return BaseResult.getSuccessMessageEntity("新增成功");
	}

	/**
	 * 更新账户信息
	 *
	 * @param object
	 * @return
	 */
	@Override
	public JsonResultEntity updateAccount(JSONObject object) {
		SysApplicationAccountEntity entity =  getData("jsonStr", object, SysApplicationAccountEntity.class);
		try {
			checkParam(entity,"update");
		}catch (Exception e){
			return BaseResult.getFailureMessageEntity(e.getMessage());
		}
		//检查一下名字不能重复
		List<SysApplicationAccountEntity> queryList = queryByName(entity);
		if (CollectionUtils.isNotEmpty(queryList) && queryList.size() > 0){
			for (SysApplicationAccountEntity acc : queryList) {
				if (!acc.getId().equals(entity.getId())){
					return BaseResult.getFailureMessageEntity(entity.getName()+"重复");
				}
			}
		}
		sysApplicationAccountDao.update(entity);
		return BaseResult.getSuccessMessageEntity("更新成功");
	}

	/**
	 * 删除账户信息
	 *
	 * @param object
	 * @return
	 */
	@Override
	public JsonResultEntity deleteAccount(JSONObject object) {
		SysApplicationAccountEntity entity =  getData("jsonStr", object, SysApplicationAccountEntity.class);
		try {
			checkParam(entity,"delete");
		}catch (Exception e){
			return BaseResult.getFailureMessageEntity(e.getMessage());
		}
		sysApplicationAccountDao.logicRemove(entity);
		return BaseResult.getSuccessMessageEntity("删除成功");
	}

	/**
	 * 查询账户详情
	 *
	 * @param object
	 * @return
	 */
	@Override
	public JsonResultEntity getAccount(JSONObject object) {
		SysApplicationAccountEntity entity =  getData("jsonStr", object, SysApplicationAccountEntity.class);
		try {
			checkParam(entity,"get");
		}catch (Exception e){
			return BaseResult.getFailureMessageEntity(e.getMessage());
		}
		SysApplicationAccountEntity sysApplicationAccountEntity = sysApplicationAccountDao.get(entity.getId());
		return BaseResult.getSuccessMessageEntity("查询账户详情成功",sysApplicationAccountEntity);
	}

	/**
	 * 查询账户列表数据
	 *
	 * @param object
	 * @return
	 */
	@Override
	public JsonResultEntity queryAccountList(JSONObject object) {
		SysApplicationAccountEntity entity =  getData("jsonStr", object, SysApplicationAccountEntity.class);
		try {
			checkParam(entity,"queryList");
		}catch (Exception e){
			return BaseResult.getFailureMessageEntity(e.getMessage());
		}
		List<SysApplicationAccountEntity> queryList = sysApplicationAccountDao.query(entity);
		return BaseResult.getSuccessMessageEntity("查询列表成功",queryList);
	}

	/**
	 * 查询账户分页数据
	 *
	 * @param object
	 * @return
	 */
	@Override
	public JsonResultEntity queryAccountPaged(JSONObject object) {
		SysApplicationAccountEntity entity =  getData("jsonStr", object, SysApplicationAccountEntity.class);
		try {
			checkParam(entity,"queryPaged");
		}catch (Exception e){
			return BaseResult.getFailureMessageEntity(e.getMessage());
		}
		return null;
	}


	//根据账户名称查询
	private List<SysApplicationAccountEntity> queryByName(SysApplicationAccountEntity entity){
		if (StrUtil.isNotEmpty(entity.getName())){
			SysApplicationAccountEntity account = new SysApplicationAccountEntity();
			account.setName(entity.getName());
			List<SysApplicationAccountEntity> queryList = sysApplicationAccountDao.query(account);
			return queryList;
		}
		return null;
	}
	//数据检查
	private void checkParam(SysApplicationAccountEntity entity,String type){
		Assert.notNull(entity,"参数不能为空");
		if ("save".equals(type)){
			Assert.notEmpty(entity.getAppId(),"appId不能为空");
			Assert.notEmpty(entity.getName(),"账户名称不能为空");
		}else if ("update".equals(type)){
			Assert.notEmpty(entity.getId(),"id不能为空");
		}else if ("delete".equals(type)){
			Assert.notEmpty(entity.getId(),"id不能为空");
		}else if ("get".equals(type)){
			Assert.notEmpty(entity.getId(),"id不能为空");
		}else if ("queryList".equals(type)){
			Assert.notEmpty(entity.getAppId(),"appId不能为空");
		}else if ("queryPaged".equals(type)){
			Assert.notNull(entity.getPageNum(),"pageNum不能为空");
			Assert.notNull(entity.getPageSize(),"pageSize不能为空");
		}
	}
}
