package com.engine.kq.cmd.upgrade;

import com.engine.common.biz.AbstractCommonCommand;
import com.engine.common.entity.BizLogContext;
import com.engine.core.interceptor.CommandContext;
import com.engine.kq.biz.KQOneStopUp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import weaver.conn.RecordSet;
import weaver.hrm.User;
import weaver.hrm.company.DepartmentComInfo;
import weaver.hrm.company.SubCompanyComInfo;
import weaver.hrm.resource.ResourceComInfo;
import weaver.systeminfo.SystemEnv;

/**
 * 带薪假数据升级
 * @author pzy
 *
 */
public class DoUpgrade6Cmd extends AbstractCommonCommand<Map<String, Object>>{

	public DoUpgrade6Cmd(Map<String, Object> params, User user) {
		this.user = user;
		this.params = params;
	}

	@Override
	public BizLogContext getLogContext() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Map<String, Object> execute(CommandContext commandContext) {
		Map<String,Object> retmap = new HashMap<String,Object>();
    List<Object> items = new ArrayList<>();
    RecordSet rs = new RecordSet();
    RecordSet rs1 = new RecordSet();
		try {
      ResourceComInfo rci = new ResourceComInfo();
      SubCompanyComInfo sci = new SubCompanyComInfo();
      DepartmentComInfo dci = new DepartmentComInfo();
		  KQOneStopUp kqOneStopUp = new KQOneStopUp();
		  String sql = "select distinct leavetype from hrmpslmanagement where status = 1";
            if (rs.getDBType().equalsIgnoreCase("sqlserver")
                    || rs.getDBType().equalsIgnoreCase("mysql")) {
                sql += " and (leavetype is not null and leavetype<>'')";
            } else {
                sql += " and (leavetype is not null)";
            }
      kqOneStopUp.clearBalance(sql);
		  kqOneStopUp.upPaid();

      Map<String,Object> paidmap = new HashMap<String,Object>();
      List<Object> paidList = new ArrayList<>();

      String getPaidSql = "select* from kq_BalanceOfLeave where leaverulesid in(select distinct leavetype from hrmpslmanagement where status = 1) order by leaverulesid ";
      rs.executeQuery(getPaidSql);
      while (rs.next()){
        String resourceid = rs.getString("resourceid");
        String lastname = rci.getLastname(resourceid);
        String subname = sci.getSubCompanyname(rci.getSubCompanyID(resourceid));
        String deptname = dci.getDepartmentname(rci.getDepartmentID(resourceid));
        String extraamount = rs.getString("extraamount");
        String desc = lastname+" "+subname+" "+deptname+" "+extraamount;
        paidList.add(desc);
      }
      paidmap.put("label", SystemEnv.getHtmlLabelName(10000843,weaver.general.Util.getIntValue(user.getLanguage())));
      paidmap.put("status", "1");
      paidmap.put("results", paidList);
      items.add(paidmap);

			retmap.put("status", "1");
      retmap.put("items", items);
		} catch (Exception e) {
			writeLog(e);
      retmap.put("status", "-1");
      retmap.put("message",  SystemEnv.getHtmlLabelName(382661,user.getLanguage()));
		}
		return retmap;
	}

}
