389 lines
		
	
	
		
			15 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			389 lines
		
	
	
		
			15 KiB
		
	
	
	
		
			C#
		
	
	
	
using BLL;
 | 
						|
using Newtonsoft.Json.Linq;
 | 
						|
using System;
 | 
						|
using System.Collections.Generic;
 | 
						|
using System.Data;
 | 
						|
using System.Data.SqlClient;
 | 
						|
using System.IO;
 | 
						|
using System.Linq;
 | 
						|
 | 
						|
namespace FineUIPro.Web.Personal
 | 
						|
{
 | 
						|
    public partial class BusinessTrip : PageBase
 | 
						|
    {
 | 
						|
        /// <summary>
 | 
						|
        /// 加载页面
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="sender"></param>
 | 
						|
        /// <param name="e"></param>
 | 
						|
        protected void Page_Load(object sender, EventArgs e)
 | 
						|
        {
 | 
						|
            if (!IsPostBack)
 | 
						|
            {
 | 
						|
                Funs.DropDownPageSize(this.ddlPageSize);
 | 
						|
                if (this.CurrUser != null && this.CurrUser.PageSize.HasValue)
 | 
						|
                {
 | 
						|
                    Grid1.PageSize = this.CurrUser.PageSize.Value;
 | 
						|
                }
 | 
						|
                this.ddlPageSize.SelectedValue = Grid1.PageSize.ToString();
 | 
						|
                // 绑定表格
 | 
						|
                this.BindGrid();
 | 
						|
                ////权限按钮方法
 | 
						|
                this.GetButtonPower();
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 绑定数据
 | 
						|
        /// </summary>
 | 
						|
        private void BindGrid()
 | 
						|
        {
 | 
						|
            string strSql = @"SELECT trip.BusinessTripId,trip.ProjectId,trip.UserId,trip.ArriveDate,trip.LeaveDate,Users.UserName,Project.ProjectName,case trip.Type when '1' then '项目出差' when '2' then '其他出差' else '' end AS TypeStr,case when trip.ArriveDate is not null and trip.LeaveDate is not null then cast(DATEDIFF(day,trip.ArriveDate,trip.LeaveDate)+1 as nvarchar(10)) else '' end AS Days  "
 | 
						|
                          + @" From dbo.Person_BusinessTrip AS trip"
 | 
						|
                          + @" LEFT JOIN Sys_User AS Users ON Users.UserId=trip.UserId"
 | 
						|
                          + @" LEFT JOIN Base_Project AS Project ON Project.ProjectId=trip.ProjectId"
 | 
						|
                          + @" WHERE 1=1";
 | 
						|
            List<SqlParameter> listStr = new List<SqlParameter>();
 | 
						|
            if (this.rblType.SelectedValue != "0")
 | 
						|
            {
 | 
						|
                strSql += " AND trip.Type = @Type";
 | 
						|
                listStr.Add(new SqlParameter("@Type", this.rblType.SelectedValue));
 | 
						|
            }
 | 
						|
            if (this.CurrUser.UserId != BLL.Const.sysglyId && this.CurrUser.UserId != BLL.Const.hfnbdId)
 | 
						|
            {
 | 
						|
                strSql += " AND trip.UserId = @UserId";
 | 
						|
                listStr.Add(new SqlParameter("@UserId", this.CurrUser.UserId));
 | 
						|
            }
 | 
						|
            if (!string.IsNullOrEmpty(this.txtUserName.Text.Trim()))
 | 
						|
            {
 | 
						|
                strSql += " AND UserName LIKE @UserName";
 | 
						|
                listStr.Add(new SqlParameter("@UserName", "%" + this.txtUserName.Text.Trim() + "%"));
 | 
						|
            }
 | 
						|
            SqlParameter[] parameter = listStr.ToArray();
 | 
						|
            DataTable tb = SQLHelper.GetDataTableRunText(strSql, parameter);
 | 
						|
            Grid1.RecordCount = tb.Rows.Count;
 | 
						|
            var table = this.GetPagedDataTable(Grid1, tb);
 | 
						|
            Grid1.DataSource = table;
 | 
						|
            Grid1.DataBind();
 | 
						|
            if (this.rblType.SelectedValue == "0" || this.rblType.SelectedValue == "1")
 | 
						|
            {
 | 
						|
                this.Grid1.Columns[3].Hidden = false;
 | 
						|
            }
 | 
						|
            else
 | 
						|
            {
 | 
						|
                this.Grid1.Columns[3].Hidden = true;
 | 
						|
            }
 | 
						|
            int days = 0;
 | 
						|
            foreach (DataRow row in table.Rows)
 | 
						|
            {
 | 
						|
                days += Funs.GetNewIntOrZero(row["Days"].ToString());
 | 
						|
            }
 | 
						|
 | 
						|
            JObject summary = new JObject();
 | 
						|
            summary.Add("LeaveDate", "合计:");
 | 
						|
            summary.Add("Days", days);
 | 
						|
            Grid1.SummaryData = summary;
 | 
						|
        }
 | 
						|
 | 
						|
 | 
						|
        #region 分页
 | 
						|
        /// <summary>
 | 
						|
        /// 分页
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="sender"></param>
 | 
						|
        /// <param name="e"></param>
 | 
						|
        protected void Grid1_PageIndexChange(object sender, GridPageEventArgs e)
 | 
						|
        {
 | 
						|
            BindGrid();
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 分页显示条数下拉框
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="sender"></param>
 | 
						|
        /// <param name="e"></param>
 | 
						|
        protected void ddlPageSize_SelectedIndexChanged(object sender, EventArgs e)
 | 
						|
        {
 | 
						|
            Grid1.PageSize = Convert.ToInt32(ddlPageSize.SelectedValue);
 | 
						|
            BindGrid();
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 排序
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="sender"></param>
 | 
						|
        /// <param name="e"></param>
 | 
						|
        protected void Grid1_Sort(object sender, FineUIPro.GridSortEventArgs e)
 | 
						|
        {
 | 
						|
            BindGrid();
 | 
						|
        }
 | 
						|
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region  删除数据
 | 
						|
        /// <summary>
 | 
						|
        /// 批量删除数据
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="sender"></param>
 | 
						|
        /// <param name="e"></param>
 | 
						|
        protected void btnMenuDelete_Click(object sender, EventArgs e)
 | 
						|
        {
 | 
						|
            if (Grid1.SelectedRowIndexArray.Length > 0)
 | 
						|
            {
 | 
						|
                foreach (int rowIndex in Grid1.SelectedRowIndexArray)
 | 
						|
                {
 | 
						|
                    string rowID = Grid1.DataKeys[rowIndex][0].ToString();
 | 
						|
                    var trip = Person_BusinessTripService.GetPersonBusinessTripById(rowID);
 | 
						|
                    if (trip != null)
 | 
						|
                    {
 | 
						|
                        BLL.Person_BusinessTripService.DeletePersonBusinessTrip(rowID);
 | 
						|
                    }
 | 
						|
                }
 | 
						|
                BindGrid();
 | 
						|
                ShowNotify("删除数据成功!", MessageBoxIcon.Success);
 | 
						|
            }
 | 
						|
            else
 | 
						|
            {
 | 
						|
                ShowNotify("请至少选中一行!", MessageBoxIcon.Warning);
 | 
						|
            }
 | 
						|
        }
 | 
						|
        #endregion
 | 
						|
 | 
						|
        protected void btnNew_Click(object sender, EventArgs e)
 | 
						|
        {
 | 
						|
            var noEndTrip = (from x in Funs.DB.Person_BusinessTrip where x.UserId == this.CurrUser.UserId && (x.ArriveDate == null || x.LeaveDate == null) select x).FirstOrDefault();
 | 
						|
            if (noEndTrip != null)
 | 
						|
            {
 | 
						|
                ShowNotify("尚有一条记录到达/离开日期为空,请先完善信息后再新增出差记录!", MessageBoxIcon.Warning);
 | 
						|
                return;
 | 
						|
            }
 | 
						|
            PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("BusinessTripEdit.aspx", "编辑 - ")));
 | 
						|
        }
 | 
						|
 | 
						|
        #region 编辑
 | 
						|
        /// <summary>
 | 
						|
        /// 编辑
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="sender"></param>
 | 
						|
        /// <param name="e"></param>
 | 
						|
        protected void btnMenuEdit_Click(object sender, EventArgs e)
 | 
						|
        {
 | 
						|
            if (Grid1.SelectedRowIndexArray.Length == 0)
 | 
						|
            {
 | 
						|
                Alert.ShowInParent("请至少选择一条记录!", MessageBoxIcon.Warning);
 | 
						|
                return;
 | 
						|
            }
 | 
						|
            string Id = Grid1.SelectedRowID;
 | 
						|
            PageContext.RegisterStartupScript(Window1.GetShowReference(String.Format("BusinessTripEdit.aspx?BusinessTripId={0}", Id, "编辑 - ")));
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// Grid行双击事件
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="sender"></param>
 | 
						|
        /// <param name="e"></param>
 | 
						|
        protected void Grid1_RowDoubleClick(object sender, GridRowClickEventArgs e)
 | 
						|
        {
 | 
						|
            btnMenuEdit_Click(null, null);
 | 
						|
        }
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region 查询
 | 
						|
        /// <summary>
 | 
						|
        /// 查询
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="sender"></param>
 | 
						|
        /// <param name="e"></param>
 | 
						|
        protected void TextBox_TextChanged(object sender, EventArgs e)
 | 
						|
        {
 | 
						|
            this.BindGrid();
 | 
						|
        }
 | 
						|
        #endregion
 | 
						|
 | 
						|
        #region 获取按钮权限
 | 
						|
        /// <summary>
 | 
						|
        /// 获取按钮权限
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="button"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        private void GetButtonPower()
 | 
						|
        {
 | 
						|
            var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId, BLL.Const.BusinessTripMenuId);
 | 
						|
            if (buttonList.Count() > 0)
 | 
						|
            {
 | 
						|
                if (buttonList.Contains(BLL.Const.BtnAdd))
 | 
						|
                {
 | 
						|
                    this.btnNew.Hidden = false;
 | 
						|
                }
 | 
						|
                if (buttonList.Contains(BLL.Const.BtnModify))
 | 
						|
                {
 | 
						|
                    this.btnMenuEdit.Hidden = false;
 | 
						|
                }
 | 
						|
                if (buttonList.Contains(BLL.Const.BtnDelete))
 | 
						|
                {
 | 
						|
                    this.btnMenuDelete.Hidden = false;
 | 
						|
                }
 | 
						|
            }
 | 
						|
        }
 | 
						|
        #endregion
 | 
						|
 | 
						|
        protected void Window1_Close(object sender, EventArgs e)
 | 
						|
        {
 | 
						|
            BindGrid();
 | 
						|
        }
 | 
						|
 | 
						|
        protected void rblType_SelectedIndexChanged(object sender, EventArgs e)
 | 
						|
        {
 | 
						|
            BindGrid();
 | 
						|
        }
 | 
						|
 | 
						|
        #region 导出按钮
 | 
						|
        /// 导出按钮
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="sender"></param>
 | 
						|
        /// <param name="e"></param>
 | 
						|
        protected void btnOut_Click(object sender, EventArgs e)
 | 
						|
        {
 | 
						|
            string ids = string.Empty;
 | 
						|
            if (Grid1.SelectedRowIndexArray.Length > 0)
 | 
						|
            {
 | 
						|
                foreach (int rowIndex in Grid1.SelectedRowIndexArray)
 | 
						|
                {
 | 
						|
                    string rowID = Grid1.DataKeys[rowIndex][0].ToString();
 | 
						|
                    ids += rowID + ",";
 | 
						|
                }
 | 
						|
            }
 | 
						|
            else
 | 
						|
            {
 | 
						|
                ShowNotify("请至少选中一行!", MessageBoxIcon.Warning);
 | 
						|
                return;
 | 
						|
            }
 | 
						|
            string rootPath = Server.MapPath("~/");
 | 
						|
            string initTemplatePath = string.Empty;
 | 
						|
            string uploadfilepath = string.Empty;
 | 
						|
            string newUrl = string.Empty;
 | 
						|
            string filePath = string.Empty;
 | 
						|
            initTemplatePath = Const.BusinessTripTemplateUrl;
 | 
						|
            uploadfilepath = rootPath + initTemplatePath;
 | 
						|
            newUrl = uploadfilepath.Replace(".xlsx", "(" + string.Format("{0:yyyy-MM-dd}", DateTime.Now) + ").xlsx");
 | 
						|
            File.Copy(uploadfilepath, newUrl);
 | 
						|
            // 第一步:读取文件流
 | 
						|
            NPOI.SS.UserModel.IWorkbook workbook;
 | 
						|
            using (FileStream stream = new FileStream(newUrl, FileMode.Open, FileAccess.Read))
 | 
						|
            {
 | 
						|
                workbook = new NPOI.XSSF.UserModel.XSSFWorkbook(stream);
 | 
						|
            }
 | 
						|
            Model.SGGLDB db = Funs.DB;
 | 
						|
            string[] strs = ids.Split(',');
 | 
						|
            var projects = from x in db.Base_Project select x;
 | 
						|
            var lists = from x in db.Person_BusinessTrip where strs.Contains(x.BusinessTripId) orderby x.ArriveDate descending select x;
 | 
						|
            // 创建单元格样式
 | 
						|
            NPOI.SS.UserModel.ICellStyle cellStyle = workbook.CreateCellStyle();
 | 
						|
            cellStyle.BorderTop = NPOI.SS.UserModel.BorderStyle.Thin;
 | 
						|
            cellStyle.BorderRight = NPOI.SS.UserModel.BorderStyle.Thin;
 | 
						|
            cellStyle.BorderBottom = NPOI.SS.UserModel.BorderStyle.Thin;
 | 
						|
            cellStyle.BorderLeft = NPOI.SS.UserModel.BorderStyle.Thin;
 | 
						|
            cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center;
 | 
						|
            cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center;
 | 
						|
            cellStyle.WrapText = true;
 | 
						|
            var font = workbook.CreateFont();
 | 
						|
            font.FontHeightInPoints = 10;
 | 
						|
            //font.FontHeightInPoints = (short)8.5;字号为小数时要转为short
 | 
						|
            cellStyle.SetFont(font);
 | 
						|
            // 第二步:创建新数据行
 | 
						|
            NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(0);
 | 
						|
            int i = 1;
 | 
						|
            int sum = 0;
 | 
						|
            foreach (var item in lists)
 | 
						|
            {
 | 
						|
                // 第二步:创建新数据行
 | 
						|
                NPOI.SS.UserModel.IRow row = sheet.GetRow(i);
 | 
						|
                NPOI.SS.UserModel.ICell cell;
 | 
						|
                // 添加数据
 | 
						|
                cell = row.CreateCell(0);
 | 
						|
                cell.CellStyle = cellStyle;
 | 
						|
                cell.SetCellValue(i.ToString());
 | 
						|
                cell = row.CreateCell(1);
 | 
						|
                cell.CellStyle = cellStyle;
 | 
						|
                cell.SetCellValue(item.Type == "1" ? "项目出差" : "其他出差");
 | 
						|
                cell = row.CreateCell(2);
 | 
						|
                cell.CellStyle = cellStyle;
 | 
						|
                cell.SetCellValue(BLL.UserService.GetUserNameByUserId(item.UserId));
 | 
						|
                cell = row.CreateCell(3);
 | 
						|
                cell.CellStyle = cellStyle;
 | 
						|
                var project = projects.FirstOrDefault(x => x.ProjectId == item.ProjectId);
 | 
						|
                if (project != null)
 | 
						|
                {
 | 
						|
                    cell.SetCellValue(project.ProjectName);
 | 
						|
                }
 | 
						|
                cell = row.CreateCell(4);
 | 
						|
                cell.CellStyle = cellStyle;
 | 
						|
                string arriveDate = string.Empty;
 | 
						|
                if (item.ArriveDate != null)
 | 
						|
                {
 | 
						|
                    arriveDate = string.Format("{0:yyyy-MM-dd}", item.ArriveDate);
 | 
						|
                }
 | 
						|
                cell.SetCellValue(arriveDate);
 | 
						|
                string leaveDate = string.Empty;
 | 
						|
                if (item.LeaveDate != null)
 | 
						|
                {
 | 
						|
                    leaveDate = string.Format("{0:yyyy-MM-dd}", item.LeaveDate);
 | 
						|
                }
 | 
						|
                cell = row.CreateCell(5);
 | 
						|
                cell.CellStyle = cellStyle;
 | 
						|
                cell.SetCellValue(leaveDate);
 | 
						|
                string days = "";
 | 
						|
                if (item.ArriveDate != null && item.LeaveDate != null)
 | 
						|
                {
 | 
						|
                    days = ((item.LeaveDate.Value - item.ArriveDate.Value).Days + 1).ToString();
 | 
						|
                    sum += (item.LeaveDate.Value - item.ArriveDate.Value).Days + 1;
 | 
						|
                }
 | 
						|
                cell = row.CreateCell(6);
 | 
						|
                cell.CellStyle = cellStyle;
 | 
						|
                cell.SetCellValue(days);
 | 
						|
                i++;
 | 
						|
            }
 | 
						|
            NPOI.SS.UserModel.IRow rows = sheet.GetRow(i);
 | 
						|
            NPOI.SS.UserModel.ICell cells;
 | 
						|
            cells = rows.CreateCell(0);
 | 
						|
            cells.CellStyle = cellStyle;
 | 
						|
            cells.SetCellValue(string.Empty);
 | 
						|
            cells = rows.CreateCell(1);
 | 
						|
            cells.CellStyle = cellStyle;
 | 
						|
            cells.SetCellValue(string.Empty);
 | 
						|
            cells = rows.CreateCell(2);
 | 
						|
            cells.CellStyle = cellStyle;
 | 
						|
            cells.SetCellValue(string.Empty);
 | 
						|
            cells = rows.CreateCell(3);
 | 
						|
            cells.CellStyle = cellStyle;
 | 
						|
            cells.SetCellValue(string.Empty);
 | 
						|
            cells = rows.CreateCell(4);
 | 
						|
            cells.CellStyle = cellStyle;
 | 
						|
            cells.SetCellValue(string.Empty);
 | 
						|
            cells = rows.CreateCell(5);
 | 
						|
            cells.CellStyle = cellStyle;
 | 
						|
            cells.SetCellValue("合计:");
 | 
						|
            cells = rows.CreateCell(6);
 | 
						|
            cells.CellStyle = cellStyle;
 | 
						|
            cells.SetCellValue(sum.ToString());
 | 
						|
            // 第三步:写入文件流
 | 
						|
            using (FileStream stream = new FileStream(newUrl, FileMode.Create, FileAccess.Write))
 | 
						|
            {
 | 
						|
                workbook.Write(stream);
 | 
						|
                workbook.Close();
 | 
						|
            }
 | 
						|
            string fileName = Path.GetFileName(newUrl);
 | 
						|
            FileInfo info = new FileInfo(newUrl);
 | 
						|
            long fileSize = info.Length;
 | 
						|
            Response.Clear();
 | 
						|
            Response.ContentType = "application/x-zip-compressed";
 | 
						|
            Response.AddHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
 | 
						|
            Response.AddHeader("Content-Length", fileSize.ToString());
 | 
						|
            Response.TransmitFile(newUrl, 0, fileSize);
 | 
						|
            Response.Flush();
 | 
						|
            Response.Close();
 | 
						|
            File.Delete(newUrl);
 | 
						|
        }
 | 
						|
        #endregion
 | 
						|
    }
 | 
						|
} |