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.helper.StringHelper;
import cn.hmsoft.jdbc.entity.Pager;
import cn.hmsoft.weixin.constants.WxResId;
import cn.hmsoft.weixin.data.dao.WxTagDao;
import cn.hmsoft.weixin.data.model.WxTag;
import cn.hmsoft.weixin.service.WxTagService;

@RestController
public class WxTagControl extends FrameControl {
	@Autowired
	private WxTagDao daoTag;
	@Autowired
	private WxTagService sTag;

	@RequestMapping("weixin/tag/page")
	public Ajax page(Integer wx_id, int start, int limit, String query, String order, String type) {
		if (wx_id == null)
			return new Ajax(new Pager(new ArrayList<String>()));
		order = StringHelper.isEmpty(order) ? "create_time" : order;
		type = StringHelper.isEmpty(type) ? "DESC" : type;
		return new Ajax(this.daoTag.pageTag(wx_id, start, limit, query, getQueryOrder(order, type)));
	}

	@RequestMapping("weixin/tag/all")
	public Ajax all(int wx_id) {
		return new Ajax(this.daoTag.listTag(wx_id));
	}

	@RequestMapping("weixin/tag/add")
	public Ajax add( WxTag Tag) {
		this.authOptrRole(WxResId.Tag_Manage);
		this.sTag.addTag(Tag, this.getFrameOptr());
		return new Ajax();
	}

	@RequestMapping("weixin/tag/edit")
	public Ajax edit(WxTag Tag) {
		this.authOptrRole(WxResId.Tag_Manage);
		this.sTag.editTag( Tag, this.getFrameOptr());
		return new Ajax();
	}

	@RequestMapping("weixin/tag/delete")
	public Ajax delete(int wx_id, int tag_id) {
		this.authOptrRole(WxResId.Tag_Manage);
		this.sTag.delTag(wx_id, tag_id, this.getFrameOptr());
		return new Ajax();
	}

	@RequestMapping("weixin/tag/sync")
	public Ajax sync(int wx_id) {
		this.authOptrRole(WxResId.Tag_Manage);
		this.sTag.syncTag(wx_id,this.getFrameOptr());
		return new Ajax();
	}

	@RequestMapping("weixin/tag/sync/user")
	public Ajax syncUser(int wx_id, int tag_id) {
		this.authOptrRole(WxResId.Tag_Manage);
		this.sTag.syncTagUser(wx_id, tag_id, this.getFrameOptr());
		return new Ajax();
	}

	@RequestMapping("weixin/tag/sync/all")
	public Ajax syncAll(int wx_id) {
		this.authOptrRole(WxResId.Tag_Manage);
		this.sTag.syncTagAll(wx_id, this.getFrameOptr());
		return new Ajax();
	}
}
