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.SysOrg;
import net.wicp.tams.app.duckula.controller.bean.models.SysPosition;
import net.wicp.tams.app.duckula.controller.dao.SysOrgMapper;
import net.wicp.tams.app.duckula.controller.dao.SysPositionMapper;
import net.wicp.tams.app.duckula.controller.service.cas.impl.OrgService;
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 PositionManager extends ParentPageBean<SysPosition> {

	@Inject
	private SysPositionMapper sysPositionMapper;

	@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(SysPosition t, boolean isInsert) {
		if (isInsert) {
			this.sysPositionMapper.insert(t);
		} else {
			this.sysPositionMapper.updateByPrimaryKeySelective(t);
		}
	}

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

	@SuppressWarnings("rawtypes")
	public TextStreamResponse onQuery() {
		org.apache.tapestry5.json.JSONObject params = TapestryAssist.packParams(request, true);
		String positionName = params.getString("positionName");
		org.apache.tapestry5.json.JSONArray orgIds = TapestryAssist.getParams(params, "orgId");
		QueryWrapper<SysPosition> queryWrapper = new QueryWrapper<SysPosition>();
		if (StringUtils.isNotBlank(positionName)) {
			queryWrapper.likeLeft("position_name", positionName);
		}
		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("org_id", orglist);
		}
		List<SysPosition> groups = this.sysPositionMapper.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<SysPosition> getBaseMapper() {
		return this.sysPositionMapper;
	}

	@Override
	public void packageQuery(SysPosition t, QueryWrapper<SysPosition> queryWrapper) {
	}
}
