package cn.hmsoft.weixin.data.dao;

import org.springframework.stereotype.Component;

import cn.hmsoft.helper.StringHelper;
import cn.hmsoft.jdbc.core.PlatformDaoSupport;
import cn.hmsoft.jdbc.entity.Pager;
import cn.hmsoft.jdbc.entity.QueryOrder;
import cn.hmsoft.weixin.data.model.WxMaterial;

@Component
public class WxMaterialDao extends PlatformDaoSupport<WxMaterial> {
	public Pager pageMaterial(int wx_id, Integer start, Integer limit, String query, QueryOrder order) {
		String sql = "select u.*,f.* from (select * from wx_material where wx_id=?) u left join frame_file f on f.file_id=u.local_file_id";
		if (StringHelper.isEmpty(query))
			return this.pageBySql(order, start, limit, sql, wx_id);
		sql = sql + " where name like ?  or file_name like ?";
		return this.pageBySql(order, start, limit, sql, wx_id, "%" + query + "%", "%" + query + "%");
	}

	// private final static String
	public Pager pageFrameFile(int wx_id, String file_type, int start, int limit, String query, QueryOrder order) {
		if (StringHelper.isEmpty(file_type))
			file_type = "Image,Video,Voice";

		String LIST_FILE_SQL = "select * from frame_file where file_type in (?) and file_id not in (select local_file_id from wx_material where wx_id=? and local_file_id is not null)  ";
		if (StringHelper.isEmpty(query))
			return this.pageMapBySql( order, start, limit, LIST_FILE_SQL, file_type, wx_id);
		LIST_FILE_SQL = LIST_FILE_SQL + " and file_name like ? ";
		return this.pageMapBySql( order, start, limit, LIST_FILE_SQL, file_type, wx_id,
				"%" + query + "%");

	}

}