package com.uas.account.dao;

import java.util.Arrays;
import java.util.List;

import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;

import com.alibaba.druid.util.StringUtils;
import com.uas.account.core.Page;
import com.uas.account.entity.Asset;

@Repository
public class AssetDaoImpl extends BaseDao implements AssetDao {

	@Override
	public Asset save(Asset asset) {
		if (asset.getId() == null) {
			asset.setId(generateId("ac$asset_seq"));
			jdbcTemplate
					.update("insert into ac$assets(id,appId,idx,url,background,imgLink,title,subTitle,color,reserveColor,btns) values (?,?,?,?,?,?,?,?,?,?,?)",
							asset.getId(), asset.getAppId(), asset.getIdx(), asset.getUrl(), asset.getBackground(), asset.getImgLink(),
							asset.getTitle(), asset.getSubTitle(), asset.getColor(), asset.getReserveColor(), asset.getBtns());
		} else {
			jdbcTemplate
					.update("update ac$assets set appId=?,idx=?,url=?,background=?,imgLink=?,title=?,subTitle=?,color=?,reserveColor=?,btns=? where id=?",
							asset.getAppId(), asset.getIdx(), asset.getUrl(), asset.getBackground(), asset.getImgLink(), asset.getTitle(),
							asset.getSubTitle(), asset.getColor(), asset.getReserveColor(), asset.getBtns(), asset.getId());
		}
		return asset;
	}

	@Override
	public List<Asset> findByApp(String appId) {
		try {
			return jdbcTemplate.query("select * from ac$assets where appId=? order by idx", new BeanPropertyRowMapper<Asset>(Asset.class),
					appId);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}

	@Override
	public void delete(Asset asset) {
		jdbcTemplate.update("delete from ac$assets where id=?", asset.getId());
		jdbcTemplate.update("update ac$assets set idx=idx-1 where idx>? and appId=?", asset.getIdx(), asset.getAppId());
	}

	@Override
	public Page<Asset> findAll(String appId, int pageNumber, int pageSize) {
		StringBuffer countSql = new StringBuffer("select count(1) from ac$assets where 1=1");
		StringBuffer querySql = new StringBuffer("select * from (select t.*,rownum r from (select * from ac$assets where 1=1");
		Object[] args = new Object[] {};
		if (!StringUtils.isEmpty(appId)) {
			countSql.append(" and appId=?");
			querySql.append(" and appId=?");
			args = Arrays.copyOf(args, args.length + 1);
			args[args.length - 1] = appId;
		}
		int total = count(countSql.toString(), args);

		querySql.append(" order by appId,idx) t where rownum <= ?) where r >= ?");
		args = Arrays.copyOf(args, args.length + 2);
		args[args.length - 2] = pageNumber * pageSize;
		args[args.length - 1] = (pageNumber - 1) * pageSize + 1;
		List<Asset> content = null;
		try {
			content = jdbcTemplate.query(querySql.toString(), new BeanPropertyRowMapper<Asset>(Asset.class), args);
		} catch (EmptyResultDataAccessException e) {

		}
		return new Page<Asset>(pageNumber, pageSize, content, total);
	}

	@Override
	public int countByApp(String appId) {
		return count("select count(1) from ac$assets where appId=?", appId);
	}

	@Override
	public Asset findOne(Long id) {
		try {
			return jdbcTemplate.queryForObject("select * from ac$assets where id=?", new BeanPropertyRowMapper<Asset>(Asset.class), id);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}

}
