package cn.hmsoft.weixin.control;

import java.util.ArrayList;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.hmsoft.application.web.Ajax;
import cn.hmsoft.frame.control.FrameControl;
import cn.hmsoft.frame.util.FrameAssertUtil;
import cn.hmsoft.helper.StringHelper;
import cn.hmsoft.jdbc.entity.Pager;
import cn.hmsoft.weixin.constants.WxResId;
import cn.hmsoft.weixin.data.dao.WxMaterialDao;
import cn.hmsoft.weixin.service.WxMaterialService;

@RestController
public class WxMaterialControl extends FrameControl {
	@Autowired
	private WxMaterialDao daoMaterial;
	@Autowired
	private WxMaterialService sMaterial;

	@RequestMapping("weixin/material/page")
	public Ajax page(Integer wxId, int start, int limit, String query, String order, String type) {
		// new ClearQuotaRequest().request();
		if (wxId == null)
			return new Ajax(new Pager(new ArrayList<String>()));
		this.authOptrRole(WxResId.Material_Page);
		order = StringHelper.isEmpty(order) ? "update_time" : order;
		type = StringHelper.isEmpty(type) ? "DESC" : type;
		return new Ajax(this.daoMaterial.pageMaterial(wxId, start, limit, query, getQueryOrder(order, type)));
	}

	@RequestMapping("weixin/material/file/page")
	public Ajax pageFrameFile(int wx_id,String file_type, int start, int limit, String query, String order, String type) {
		FrameAssertUtil.isNotEmpty(file_type, "请选择素材类型进行筛选！");
		this.authOptrRole(WxResId.Material_Page);
		order = StringHelper.isEmpty(order) ? "create_time" : order;
		type = StringHelper.isEmpty(type) ? "DESC" : type;
		return new Ajax(this.daoMaterial.pageFrameFile(wx_id,file_type, start, limit, query, getQueryOrder(order, type)));
	}

	@RequestMapping("weixin/material/file/upload")
	public Ajax upload(int wx_id, String type) {
		this.authOptrRole(WxResId.Material_Manage);
		this.sMaterial.uploadMaterial(wx_id,type, this.getFrameOptr());
		return new Ajax();
	}

	@RequestMapping("weixin/material/download")
	public Ajax download(int wx_id,String media_id) {
		this.authOptrRole(WxResId.Material_Manage);
		this.sMaterial.downloadMaterial(wx_id,media_id, this.getFrameOptr());
		return new Ajax();
	}

	@RequestMapping("weixin/material/download/temp")
	public Ajax downloadTemp(int wx_id,String media_id, String type) {
		this.authOptrRole(WxResId.Material_Manage);
		this.sMaterial.downloadMaterialTemp(wx_id,media_id, type, this.getFrameOptr());
		return new Ajax();
	}

	@RequestMapping("weixin/material/sync")
	public Ajax sync(int wx_id) {
		this.authOptrRole(WxResId.Material_Manage);
		this.sMaterial.syncMaterial(wx_id,this.getFrameOptr());
		return new Ajax();
	}

	@RequestMapping("weixin/material/delete")
	public Ajax delete(String app_id,String key) {
		this.authOptrRole(WxResId.Material_Manage);
		this.sMaterial.delMaterial(app_id,key, this.getFrameOptr());
		return new Ajax();
	}

}
