package cn.hmsoft.weixin.control;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

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.constants.FrameThreadCallbackType;
import cn.hmsoft.frame.control.FrameControl;
import cn.hmsoft.frame.data.model.FrameOptr;
import cn.hmsoft.frame.data.model.FrameThread;
import cn.hmsoft.frame.thread.FrameThreadHelper;
import cn.hmsoft.helper.StringHelper;
import cn.hmsoft.jdbc.entity.Pager;
import cn.hmsoft.jdbc.entity.QueryOrder;
import cn.hmsoft.jdbc.entity.QueryOrder.QueryOrderType;
import cn.hmsoft.weixin.constants.WxResId;
import cn.hmsoft.weixin.data.dao.WxUserDao;
import cn.hmsoft.weixin.data.model.WxDefine;
import cn.hmsoft.weixin.data.model.WxTag;
import cn.hmsoft.weixin.data.model.WxUserTag;
import cn.hmsoft.weixin.service.WxUserService;

@RestController
public class WxUserControl extends FrameControl {
	@Autowired
	private WxUserDao daoUser;
	@Autowired
	private WxUserService sUser;

	@RequestMapping("weixin/user/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>()));
		this.authOptrRole(WxResId.User_Page);
		order = StringHelper.isEmpty(order) ? "active_time" : order;
		type = StringHelper.isEmpty(type) ? "DESC" : type;
		return new Ajax(this.daoUser.pageUser(wx_id, start, limit, query, getQueryOrder(order, type)));
	}

	@RequestMapping("weixin/user/tags/query")
	public Ajax queryTags(int wx_id, String open_id) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("TagArray", this.daoUser.all(WxTag.class, new QueryOrder("create_time", QueryOrderType.DESC)));
		map.put("UserTagsArray", this.daoUser.list(WxUserTag.class, "open_id", open_id));
		return new Ajax(map);
	}

	@RequestMapping("weixin/user/tags/save")
	public Ajax saveTag(int wuser_id,Integer[] tag_ids) {
		if (tag_ids == null)
			tag_ids = new Integer[0];
		sUser.modifyUserTags(wuser_id, tag_ids, this.getFrameOptr());
		return new Ajax();
	}

	@RequestMapping("weixin/user/remark/save")
	public Ajax saveRemark(int wx_id, String open_id, String remark) {
		sUser.modifyUserRemark(wx_id, open_id, remark, this.getFrameOptr());
		return new Ajax();
	}

	@RequestMapping("weixin/user/sync")
	public Ajax syncUser(int wx_id, String open_id) {
		sUser.syncUser(wx_id, open_id, this.getFrameOptr());
		return new Ajax();
	}

	@RequestMapping("weixin/user/sync/all")
	public Ajax syncUserAll(int wx_id) {
		FrameOptr optr = this.getFrameOptr();
		WxDefine wx = this.daoUser.find(WxDefine.class, "wx_id", wx_id);
		FrameThread thread = FrameThreadHelper.createThread("微信插件-同步用户", "同步【" + wx.getWx_name() + "】微信公众号关注用户",
				FrameThreadCallbackType.Swal, "用户同步完成...", this.getFrameOptr());
		// new Thread().start();

		new Thread(() -> {
			sUser.syncUserAll(thread, wx_id, optr);
		}).start();
		
		Ajax ajax = new Ajax();
		ajax.setEntity(thread);
		ajax.setThreadId(thread.getThread_id());
		return ajax;
	}

}
