using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZXDT.CallCenter.MVCWeb.Models;
using ZXDT.CallCenter.Model;
using System.Data.SqlClient;

namespace ZXDT.CallCenter.MVCWeb.Controllers
{
    public class HrEmployeeBaseController : BaseController
    {


        #region   初使化页面
        public ActionResult BlackCustomer()
        {
            Model.T_Cus_CustomerBase cusmodel = new Model.T_Cus_CustomerBase();
            return View(cusmodel);

        }
        public ActionResult EmployeeBaseList()
        {
            Model.T_Hr_EmployeeBase viewModel = new Model.T_Hr_EmployeeBase();

            ////获取部门列表
            //viewModel.DepartmentList = deptBLL.GetModelList("F_ParentId=0");
            ////获取角色列表
            //viewModel.RoleInfoList = rollBll.GetModelList("");
            return View(viewModel);
        }
        public ActionResult EmployeeSearchList()
        {
            Model.T_Hr_EmployeeBase viewModel = new Model.T_Hr_EmployeeBase();
            return View(viewModel);

        }
        public ActionResult EmployyFromExcel()
        {

            return View();
        }

        #endregion
        #region 人员管理
        BLL.T_Sys_Department deptBLL = new BLL.T_Sys_Department();
        BLL.T_Sys_RoleInfo rollBll = new BLL.T_Sys_RoleInfo();
        BLL.T_Hr_EmployeeBase empBll = new BLL.T_Hr_EmployeeBase();
        ///通过部门父级节点获取数据，父级点0
        /// </summary>
        /// <param name="fid"></param>
        /// <returns></returns>
        public string GetDepartmentJsonModel(int parentId)
        {
            return Newtonsoft.Json.JsonConvert.SerializeObject(deptBLL.GetDepartmentJsonModel(parentId));
        }

        /// <summary>
        /// 获取用户数据
        /// </summary>
        /// <param name="page">当前页码</param>
        /// <param name="limit">每页数据量</param>
        /// <returns></returns>
        [ActionName("EmployDate")]
        [HttpGet]
        public string EmployDate(DateTime? NowDateTime, int page, int limit, int? deptId, string realname, string phone)
        {

            //数据结果集
            ResponseData dataModel = new ResponseData();
            string sql = "";

            if (deptId != null && deptId > 0)
            {
                sql += " and F_DepartmentId=" + deptId;
            }
            if (!string.IsNullOrEmpty(realname))
            {

                sql += " and F_RealName like '%" + realname.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(phone))
            {
                sql += " and F_Phone like '%" + phone.Trim() + "%'";
            }

            DataTable dt = new DataTable();
            int recordCount = 0;
            Model.PageData<Model.T_Wo_WorkOrderBase> pageModel = new Model.PageData<Model.T_Wo_WorkOrderBase>();
            dt = BLL.PagerBLL.GetListPager(
                "T_Hr_EmployeeBase",
                "F_EmployeeId",
                "*"
                + ",(select  top 1 t.F_DeptName from T_Sys_Department t where t.F_DeptId=T_Hr_EmployeeBase.F_DepartmentId) as F_DeptName  "
                 + ",(select  top 1 t.F_RoleName from T_Sys_RoleInfo t where t.F_RoleId=T_Hr_EmployeeBase.F_WorkDeptId) as F_RoleName  "
                   + ",(CASE F_Sex WHEN 1 THEN '男' WHEN 0 THEN '女' ELSE NULL END ) as F_SexName"
                   ,
                sql,
                "ORDER BY F_EmployeeId desc ",
                limit,
                page,
                true,
                out recordCount);


            dataModel.code = 0;
            dataModel.count = recordCount;
            dataModel.data = dt;

            return JsonConvert.SerializeObject(dataModel);
        }
        /// <summary>
        /// 用户编辑
        /// </summary>
        /// <param name="editType">类型1、新增 2、修改</param>
        /// /// <returns></returns>
        public ActionResult EmployeeBaseEdit(int? employeeId, int? deptId, int editType, string F_UserCode1, string telephone, string employename, string city)
        {

            Model.T_Hr_EmployeeBase viewModel = new Model.T_Hr_EmployeeBase();

            //当前对象实体
            if (editType == 1)
            {
                viewModel.F_DepartmentId = int.Parse(deptId.ToString());
                viewModel.F_RealName = employename;
                viewModel.F_Phone = telephone;
                viewModel.F_Address = city;
            }
            else
            {
                Model.T_Hr_EmployeeBase userModel = empBll.GetModel(int.Parse(employeeId.ToString()));
                viewModel = userModel;
            }
            ////获取部门列表
            //viewModel.DepartmentList = deptBLL.GetModelList("F_ParentId=0");
            ////获取角色列表
            //viewModel.RoleInfoList = rollBll.GetModelList("");
            return View(viewModel);
        }
        /// <summary>
        /// 保存编辑
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Post)]
        public bool SaveEmployeeData(T_Hr_EmployeeBase employeeModel)
        {
            if (employeeModel.F_EmployeeId > 0)
            {
                T_Hr_EmployeeBase oldemployeeModel = empBll.GetModel(employeeModel.F_EmployeeId);
                oldemployeeModel.F_DepartmentId = employeeModel.F_DepartmentId;
                oldemployeeModel.F_Position = employeeModel.F_Position;
                oldemployeeModel.F_RealName = employeeModel.F_RealName;
                oldemployeeModel.F_WorkingCode = employeeModel.F_WorkingCode;
                oldemployeeModel.F_Sex = employeeModel.F_Sex;
                oldemployeeModel.F_Phone = employeeModel.F_Phone;
                oldemployeeModel.F_Resume = employeeModel.F_Resume;
                oldemployeeModel.F_Address = employeeModel.F_Address;
                oldemployeeModel.F_OfficePhone = employeeModel.F_OfficePhone;
                return empBll.Update(oldemployeeModel);
            }
            else
            {
                return empBll.Add(employeeModel) > 0;
            }
        }
        /// <summary>
        /// 单个用户删除
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get)]
        public bool DeletehremployData(int employeeId)
        {
            return empBll.Delete(employeeId);

        }
        /// <summary>
        /// 批量删除 
        /// </summary>
        /// <param name="workOrderBaseModel"></param>
        /// <returns></returns>
        [AcceptVerbs(HttpVerbs.Get)]
        public bool DeleteEmployeeData(string employeeId)
        {
            return empBll.DeleteList(employeeId);

        }

        #endregion
        #region 导入excel

        //将Excel导入数据库


        public string SqlBulkCopyByDatatable(string connectionString, string TableName, DataTable dt)
        {
            using (SqlConnection conn = new SqlConnection(connectionString))
            {

                using (SqlBulkCopy sqlbulkcopy =
    new SqlBulkCopy(connectionString, SqlBulkCopyOptions.UseInternalTransaction))
                {
                    try
                    {
                        sqlbulkcopy.DestinationTableName = TableName;
                        for (int i = 0; i < dt.Columns.Count; i++)
                        {
                            sqlbulkcopy.ColumnMappings.Add(dt.Columns[i].ColumnName, dt.Columns[i].ColumnName);
                        }
                        sqlbulkcopy.WriteToServer(dt);
                        return "200";
                    }
                    catch (System.Exception ex)
                    {
                        throw (ex);
                    }
                }
            }
        }


        #endregion

    }
}
