package net.wicp.tams.duckula.ops.pages.cas;

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

import org.apache.commons.lang3.StringUtils;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.apache.tapestry5.util.TextStreamResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import net.wicp.tams.app.duckula.controller.bean.models.SysGroup;
import net.wicp.tams.app.duckula.controller.bean.models.SysOrg;
import net.wicp.tams.app.duckula.controller.dao.SysGroupMapper;
import net.wicp.tams.app.duckula.controller.dao.SysOrgMapper;
import net.wicp.tams.app.duckula.controller.service.cas.impl.OrgService;
import net.wicp.tams.cas.bean.constant.ColsSysGroup;
import net.wicp.tams.cas.bean.constant.ColsSysOrg;
import net.wicp.tams.common.apiext.StringUtil;
import net.wicp.tams.common.apiext.json.EasyUiAssist;
import net.wicp.tams.common.callback.IConvertValue;
import net.wicp.tams.common.jdbc.convert.ConvertValueMysql;
import net.wicp.tams.component.tools.TapestryAssist;
import net.wicp.tams.duckula.ops.pages.ParentPageBean;

public class GroupManager extends ParentPageBean<SysGroup> {

	@Inject
	private SysGroupMapper sysGroupMapper;

	@Inject
	private SysOrgMapper sysOrgMapper;

	@Inject
	private OrgService orgService;
	@Property
	private String orgTree;

	@Property
	private String orgId;

	@SetupRender
	void init() {
		orgTree = orgService.createOrgTree(new IConvertValue<Object>() {
			@Override
			public String getStr(Object obj) {
				SysOrg org = (SysOrg) obj;
				String key = org.getOrgName();
				if (StringUtil.isNull(key) || !messages.contains(key)) {
					return key;
				}
				return messages.get(key);
			}
		});
		orgId = request.getParameter("orgId");
		System.out.println("orgId=" + orgId);
	}

	@Override
	public void doSave(SysGroup t, boolean isInsert) {
		if (isInsert) {
			this.sysGroupMapper.insert(t);
		} else {
			this.sysGroupMapper.updateByPrimaryKeySelective(t);
		}
	}

	@Override
	public void doDel(String id) {
		this.sysGroupMapper.deleteById(id);
	}

	@SuppressWarnings("rawtypes")
	public TextStreamResponse onQuery() {
		org.apache.tapestry5.json.JSONObject params = TapestryAssist.packParams(request, true);
		String groupName = params.getString("groupName");
		org.apache.tapestry5.json.JSONArray orgIds = TapestryAssist.getParams(params, "orgId");
		QueryWrapper<SysGroup> queryWrapper = new QueryWrapper<SysGroup>();
		if (StringUtils.isNotBlank(groupName)) {
			queryWrapper.likeLeft(ColsSysGroup.groupName.getOriColName(), groupName);
		}
		if (orgIds != null && orgIds.size() > 0) {
			List<Long> orglist = new ArrayList<Long>();
			for (int i = 0; i < orgIds.size(); i++) {
				orglist.add(Long.parseLong(orgIds.getString(i)));
			}
			queryWrapper.in(ColsSysGroup.orgId.getOriColName(), orglist);
		}
		List<SysGroup> groups = this.sysGroupMapper.selectList(queryWrapper);
		ConvertValueMysql orgconv = new ConvertValueMysql(ColsSysOrg.tb,ColsSysOrg.orgName.getOriColName());
		Map<String, IConvertValue> map = new HashMap<String, IConvertValue>();
		map.put("orgId1", orgconv);
		String str = EasyUiAssist.getJsonForGridAlias2(groups, new String[] { "orgId,orgId1" }, map, groups.size());
		return retStream(str);
	}

	@Override
	public BaseMapper<SysGroup> getBaseMapper() {
		return this.sysGroupMapper;
	}

	@Override
	public void packageQuery(SysGroup t, QueryWrapper<SysGroup> queryWrapper) {
		// JSONArray orgIds = TapestryAssist.getParams(params, "orgId");
		if (StringUtils.isNotBlank(t.getGroupName())) {
			queryWrapper.eq("group_name", t.getGroupName());
		}
	}
}
