package com.uas.account.dao;

import java.util.List;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;

import com.uas.account.entity.UserSpace;
import com.uas.account.entity.UserSpaceDetail;

@Repository
public class PlatfromManageDaoImpl extends BaseDao implements PlatfromManageDao {

	@Override
	public ModelMap updateEnterpriseInfoInManage(UserSpaceDetail detail, String businessCode) {
		ModelMap map = new ModelMap();
		String countSql = "select count(1) from enterprise where en_bussinesscode = '" + businessCode + "'";
		Object[] args = new Object[] {};
		Integer count = count(countSql, args);
		if (count > 0) {
			String updateSql = "update enterprise set en_name = '" + detail.getName() + "', en_address = '"
					+ detail.getAddress() + "', en_bussinesscode ='"
					+ detail.getBusinessCode() + "' where en_bussinesscode = '"
					+ businessCode + "'";
			jdbcTemplate.execute(updateSql);
			countSql = "select count(1) from enterprise where en_name = '" + detail.getName() + "' and en_bussinesscode = '"
                    + detail.getBusinessCode() + "'";
			count = count(countSql, args);
			if (count == 0) {
				map.put("error", "管理平台企业信息更新失败");
			}
		} else {
			map.put("error", "管理平台未找到相关企业信息，更新失败");
		}
		return map;
	}

	@Override
	public ModelMap updateMasterInfoInManage(UserSpaceDetail detail, String businessCode) {
		ModelMap map = new ModelMap();
		String countSql = "select count(1) from masters where business_code = '" + businessCode + "'";
		Object[] args = new Object[] {};
		Integer count = count(countSql, args);
		if (count > 0) {
			String updateSql = "update masters set name = '" + detail.getName() + "',business_code = '"
					+ detail.getBusinessCode() + "' where  business_code = '" + businessCode + "'";
			jdbcTemplate.execute(updateSql);
			countSql = "select count(1) from masters where name = '" + detail.getName() + "' and business_code = '"
					+ detail.getBusinessCode() + "'";
			count = count(countSql, args);
			if (count == 0) {
				map.put("error", "管理平台控制中心企业信息更新失败");
			}
		} else {
			// 如果没找到，可以进行插入数据处理
			Long id = generateId("masters_seq");
			// 得到uu号等信息（目前只更新商务平台的信息）
			UserSpace userSpace = getUserSpaceByUidAndAppId(detail.getBusinessCode(), "b2b");
			jdbcTemplate.update(
					"insert into masters(id,checked,name,platform,uu,business_code,env) values (?,?,?,?,?,?,?)", id, 1,
					detail.getName(), "B2B", userSpace.getDialectUID(), detail.getBusinessCode(), "prod");
			countSql = "select count(1) from masters where name = '" + detail.getName() + "' and business_code = '"
					+ detail.getBusinessCode() + "'";
			count = count(countSql, args);
			if (count == 0) {
				map.put("error", "管理平台控制中心企业信息更新失败");
			}
		}
		return map;
	}

	/**
	 * 通过营业执照号和应用查询注册后的信息
	 * 
	 * @param uid
	 * @param appId
	 * @return
	 */
	private UserSpace getUserSpaceByUidAndAppId(String uid, String appId) {
		try {
			List<UserSpace> spaces = jdbcTemplate.query(
					"select appId,id,uid_ as \"UID\",dialectUID,name,userCount from ac$userspaces where appId=? and uid_=?",
					new BeanPropertyRowMapper<UserSpace>(UserSpace.class), appId, uid);
			return CollectionUtils.isEmpty(spaces) ? null : spaces.get(0);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}

}
