using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace HySoft.BaseCallCenter.DAL
{
    public class LDWorkOrderCount
    {
        public HySoft.BaseCallCenter.Model.LDWorkOrderCount DataRowToModel(DataRow row)
        {
            HySoft.BaseCallCenter.Model.LDWorkOrderCount model = new HySoft.BaseCallCenter.Model.LDWorkOrderCount();
            if (row != null)
            {
                if (row["F_DEPTID"] != null && row["F_DEPTID"].ToString() != "")
                {
                    model.F_DEPTID = int.Parse(row["F_DEPTID"].ToString());
                }
                if (row["F_USERNAME"] != null && row["F_USERNAME"].ToString() != "")
                {
                    model.F_USERNAME = row["F_USERNAME"].ToString();
                }
                if (row["F_OWNERID"] != null && row["F_OWNERID"].ToString() != "")
                {
                    model.F_OWNERID = int.Parse(row["F_OWNERID"].ToString());
                    model.F_DJDCOUNT = getWorkOrderCount(model.F_OWNERID, 6, 0);
                    model.F_DCLCOUNT = getWorkOrderCount(model.F_OWNERID, 7, 0);
                }
                if (row["F_DPGCOUNT"] != null && row["F_DPGCOUNT"].ToString() != "")
                {
                    model.F_DPGCOUNT = int.Parse(row["F_DPGCOUNT"].ToString());
                }
            }
            return model;
        }

        private int getWorkOrderCount(int userid, int workorderstateid, int loseflag)
        {
            string sql = string.Format("select COUNT(*) from [XKP_CallCenter].[dbo].[T_Wo_WorkOrderTask] where F_INSTANCEID in(select distinct F_INSTANCEID from [XKP_CallCenter].[dbo].[T_Wo_WorkOrderTask] where F_OWNERID={0} and F_WORKORDERSTATEID=13) and F_WORKORDERSTATEID={1} and F_LOSEFLAG={2}", userid, workorderstateid, loseflag);
            DataTable dt = DBUtility.DbHelperSQL.Query(sql).Tables[0];
            int count = 0;
            int.TryParse(dt.Rows[0][0].ToString(), out count);
            return count;
        }
    }
}
