using BLL;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
namespace FineUIPro.Web.HSSE.OccupationHealth
{
public partial class PhysicalExaminationEdit : PageBase
{
#region 定义项
///
/// 主键
///
public string PersonId
{
get { return (string)ViewState["PersonId"]; }
set { ViewState["PersonId"] = value; }
}
///
/// 二维码路径id
///
public string QRCodeAttachUrl
{
get { return (string)ViewState["QRCodeAttachUrl"]; }
set { ViewState["QRCodeAttachUrl"] = value; }
}
///
/// 项目ID
///
public string ProjectId
{
get { return (string)ViewState["ProjectId"]; }
set { ViewState["ProjectId"] = value; }
}
public string UnitId
{
get { return (string)ViewState["UnitId"]; }
set { ViewState["UnitId"] = value; }
}
///
/// 公司级编辑
///
public string CompanyEdit
{
get { return (string)ViewState["CompanyEdit"]; }
set { ViewState["CompanyEdit"] = value; }
}
public string Type
{
get { return (string)ViewState["Type"]; }
set { ViewState["Type"] = value; }
}
#endregion
#region 加载页面
///
/// 加载页面
///
///
///
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
btnClose.OnClientClick = ActiveWindow.GetHideReference();
////权限按钮方法
if (Request.Params["type"] != "-1")
{
this.GetButtonPower();
}
if (!string.IsNullOrEmpty(Request.Params["Company"]))
{
this.btnSave.Hidden = true;
}
this.ProjectId = Request.Params["ProjectId"];
this.UnitId = Request.Params["UnitId"];
this.PersonId = Request.Params["PersonId"];
this.Type = Request.Params["Type"];
CompanyEdit = Request.Params["CompanyEdit"];
this.InitDropDownList();
this.drpIdcardType.SelectedValue = "SHENFEN_ZHENGJIAN";
if (!string.IsNullOrEmpty(this.PersonId))
{
var person = BLL.PersonService.GetPersonById(this.PersonId);
if (person != null)
{
this.ProjectId = person.ProjectId;
if (!string.IsNullOrEmpty(person.Sex))
{
this.rblSex.SelectedValue = person.Sex;
}
if (!string.IsNullOrEmpty(person.UnitId))
{
var unit = BLL.UnitService.GetUnitByUnitId(person.UnitId);
if (unit != null)
{
this.drpUnitId.SelectedValue = unit.UnitId;
drpUnitId_OnSelectedIndexChanged(null, null);
this.UnitId = person.UnitId;
}
}
if (!string.IsNullOrEmpty(person.WorkAreaId))
{
txtWorkArea.Values = person.WorkAreaId.Split(',');
}
if (!string.IsNullOrEmpty(person.WorkPostId))
{
this.drpPost.SelectedValue = person.WorkPostId;
}
if (!string.IsNullOrEmpty(person.PostTitleId))
{
this.drpTitle.SelectedValue = person.PostTitleId;
}
if (!string.IsNullOrEmpty(person.TeamGroupId))
{
this.drpTeamGroup.SelectedValue = person.TeamGroupId;
}
if (!string.IsNullOrEmpty(person.EduLevel))
{
this.drpEduLevel.SelectedValue = person.EduLevel;
}
this.rblIsUsed.SelectedValue = person.IsUsed ? "True" : "False";
this.txtCardNo.Text = person.CardNo;
this.txtPersonName.Text = person.PersonName;
this.txtIdentityCard.Text = person.IdentityCard;
this.txtTelephone.Text = person.Telephone;
this.txtMajor.Text = person.Major;
if (person.InTime.HasValue)
{
this.txtInTime.Text = string.Format("{0:yyyy-MM-dd}", person.InTime);
}
if (person.OutTime.HasValue)
{
this.txtOutTime.Text = string.Format("{0:yyyy-MM-dd}", person.OutTime);
}
if (person.Birthday.HasValue)
{
this.txtBirthday.Text = string.Format("{0:yyyy-MM-dd}", person.Birthday);
}
if (!string.IsNullOrEmpty(person.PhotoUrl))
{
imgPhoto.ImageUrl = ("~/" + person.PhotoUrl);
}
if (!string.IsNullOrEmpty(person.IdcardType))
{
this.drpIdcardType.SelectedValue = person.IdcardType;
}
}
}
else
{
var unit = BLL.UnitService.GetUnitByUnitId(this.UnitId);
if (unit != null)
{
this.drpUnitId.SelectedValue = unit.UnitId;
this.txtCardNo.Text = string.Empty;
}
this.txtInTime.Text = string.Format("{0:yyyy-MM-dd}", System.DateTime.Now);
this.rblIsUsed.SelectedValue = "True";
}
}
}
#endregion
///
/// 初始化下拉框
///
private void InitDropDownList()
{
if (!string.IsNullOrEmpty(CompanyEdit))
{
txtWorkArea.ShowRedStar = false;
txtWorkArea.Required = false;
gvWorkArea.DataSource = BLL.UnitWorkService.GetUnitWorkLists(this.ProjectId);
gvWorkArea.DataBind(); //单位工程
UnitService.InitBranchUnitDropDownList(this.drpUnitId, false);
}
else
{
gvWorkArea.DataSource = BLL.UnitWorkService.GetUnitWorkLists(this.ProjectId);
gvWorkArea.DataBind(); //单位工程
UnitService.InitProjectUnitDropDownList(this.drpUnitId, this.ProjectId, false);
}
if (Type == "0") //专职安全人员
{
drpPost.DataValueField = "WorkPostId";
drpPost.DataTextField = "WorkPostName";
drpPost.DataSource = BLL.WorkPostService.GetWorkPostList().Where(x => x.IsHsse == true);
drpPost.DataBind();
}
else if (Type == "1") //特种作业管理人员
{
drpPost.DataValueField = "WorkPostId";
drpPost.DataTextField = "WorkPostName";
drpPost.DataSource = BLL.WorkPostService.GetWorkPostList()
.Where(x => x.PostType == "2" || x.PostType == "4");
drpPost.DataBind();
}
else if (Type == "3") //其他人员
{
drpPost.DataValueField = "WorkPostId";
drpPost.DataTextField = "WorkPostName";
drpPost.DataSource = BLL.WorkPostService.GetWorkPostList()
.Where(x => x.IsHsse != true && x.PostType != "2" || x.PostType != "4");
drpPost.DataBind();
}
PostTitleService.InitPostTitleDropDownList(this.drpTitle, false);
BasicDataService.InitBasicDataProjectUnitDropDownList(this.drpEduLevel, "EDU_LEVEL", false);
BasicDataService.InitBasicDataProjectUnitDropDownList(this.drpIdcardType, "ZHENGJIAN_TYPE", true);
}
#region 保存
///
/// 保存按钮
///
///
///
protected void btnSave_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.txtPersonName.Text))
{
ShowNotify("人员姓名不能为空!", MessageBoxIcon.Warning);
return;
}
if (this.drpIdcardType.SelectedValue == BLL.Const._Null)
{
ShowNotify("请选择证件类型!", MessageBoxIcon.Warning);
return;
}
if (string.IsNullOrEmpty(this.txtIdentityCard.Text))
{
ShowNotify("证件号码不能为空!", MessageBoxIcon.Warning);
return;
}
if (this.drpUnitId.SelectedValue == BLL.Const._Null)
{
ShowNotify("请选择所属单位!", MessageBoxIcon.Warning);
return;
}
if (this.drpPost.SelectedValue == BLL.Const._Null)
{
ShowNotify("请选择所属岗位!", MessageBoxIcon.Warning);
return;
}
if (string.IsNullOrEmpty(this.txtInTime.Text))
{
ShowNotify("入场时间不能为空!", MessageBoxIcon.Warning);
return;
}
SaveData();
PageContext.RegisterStartupScript(ActiveWindow.GetHidePostBackReference());
}
protected void SaveData()
{
Model.SitePerson_Person person = new Model.SitePerson_Person
{
Sex = this.rblSex.SelectedValue,
ProjectId = this.ProjectId
};
//如果是公司级编辑
if (!string.IsNullOrEmpty(CompanyEdit))
{
person.ProjectId = null;
}
if (!string.IsNullOrEmpty(this.drpUnitId.SelectedValue))
{
person.UnitId = this.drpUnitId.SelectedValue;
}
if (this.drpIdcardType.SelectedValue != BLL.Const._Null)
{
person.IdcardType = this.drpIdcardType.SelectedValue;
}
if (this.drpTeamGroup.SelectedValue != BLL.Const._Null)
{
person.TeamGroupId = this.drpTeamGroup.SelectedValue;
}
if (!string.IsNullOrWhiteSpace(String.Join(",", this.txtWorkArea.Values)))
{
person.WorkAreaId = string.Join(",", txtWorkArea.Values);
}
if (this.drpPost.SelectedValue != BLL.Const._Null)
{
person.WorkPostId = this.drpPost.SelectedValue;
}
if (this.drpTitle.SelectedValue != BLL.Const._Null)
{
person.PostTitleId = this.drpTitle.SelectedValue;
}
if (!string.IsNullOrEmpty(this.rblIsUsed.SelectedValue))
{
person.IsUsed = Convert.ToBoolean(this.rblIsUsed.SelectedValue);
}
if (!string.IsNullOrEmpty(this.txtCardNo.Text.Trim()))
{
person.CardNo = this.txtCardNo.Text.Trim();
}
person.PersonName = this.txtPersonName.Text.Trim();
if (!string.IsNullOrEmpty(this.txtIdentityCard.Text))
{
person.IdentityCard = this.txtIdentityCard.Text.Trim();
}
if (this.drpEduLevel.SelectedValue != Const._Null)
{
person.EduLevel = this.drpEduLevel.SelectedValue;
}
person.Telephone = this.txtTelephone.Text.Trim();
person.Major = this.txtMajor.Text.Trim();
if (!string.IsNullOrEmpty(this.txtInTime.Text.Trim()))
{
person.InTime = Convert.ToDateTime(this.txtInTime.Text.Trim());
}
if (!string.IsNullOrEmpty(this.txtOutTime.Text.Trim()))
{
person.OutTime = Convert.ToDateTime(this.txtOutTime.Text.Trim());
}
if (!string.IsNullOrEmpty(this.txtBirthday.Text.Trim()))
{
person.Birthday = Convert.ToDateTime(this.txtBirthday.Text.Trim());
}
if (!string.IsNullOrEmpty(imgPhoto.ImageUrl) && imgPhoto.ImageUrl != "~/res/images/blank_150.png")
{
person.PhotoUrl = imgPhoto.ImageUrl.Replace("~/", "");
person.HeadImage = AttachFileService.SetImageToByteArray(Funs.RootPath + person.PhotoUrl);
}
else
{
person.PhotoUrl = null;
person.HeadImage = null;
}
if (string.IsNullOrEmpty(PersonId))
{
if (!string.IsNullOrEmpty(this.txtCardNo.Text.Trim()))
{
int cardNoCount =
BLL.PersonService.GetPersonCountByCardNo(this.ProjectId, this.txtCardNo.Text.Trim());
if (cardNoCount > 0)
{
ShowNotify("此卡号已存在,不能重复!", MessageBoxIcon.Warning);
return;
}
}
if (!string.IsNullOrEmpty(this.txtIdentityCard.Text))
{
person.IdentityCard = this.txtIdentityCard.Text.Trim();
var identityCardCount = PersonService.GetPersonCountByIdentityCard(this.txtIdentityCard.Text.Trim(),
this.CurrUser.LoginProjectId);
if (identityCardCount != null)
{
ShowNotify("此身份证号已存在,不能重复!", MessageBoxIcon.Warning);
return;
}
}
this.PersonId = SQLHelper.GetNewID(typeof(Model.SitePerson_Person));
person.PersonId = this.PersonId;
BLL.PersonService.AddPerson(person);
BLL.LogService.AddSys_Log(this.CurrUser, person.PersonName, person.PersonId, BLL.Const.PersonListMenuId,
BLL.Const.BtnAdd);
}
else
{
var getPerson = BLL.PersonService.GetPersonById(PersonId);
if (getPerson != null)
{
person.FromPersonId = getPerson.FromPersonId;
}
person.PersonId = PersonId;
BLL.PersonService.UpdatePerson(person);
//判断并更新项目用户的主副专业信息
var projectUser =
BLL.ProjectUserService.GetProjectUserByProjectIdAndIdentityCard(this.ProjectId,
person.IdentityCard);
if (projectUser != null)
{
projectUser.MainCNProfessionalId = person.MainCNProfessionalId;
projectUser.ViceCNProfessionalId = person.ViceCNProfessionalId;
BLL.ProjectUserService.UpdateProjectUser(projectUser);
}
BLL.LogService.AddSys_Log(this.CurrUser, person.PersonName, person.PersonId, BLL.Const.PersonListMenuId,
BLL.Const.BtnModify);
}
///上传人脸库
var getProject = ProjectService.GetProjectByProjectId(person.ProjectId);
if (!string.IsNullOrEmpty(person.PhotoUrl) && getProject != null && getProject.IsFace == true)
{
Alert.ShowInParent(APIPersonService.PersonFace(person), MessageBoxIcon.Warning);
}
}
#endregion
#region 上传照片
///
/// 上传照片
///
///
///
protected void filePhoto_FileSelected(object sender, EventArgs e)
{
if (filePhoto.HasFile)
{
string fileName = filePhoto.ShortFileName;
if (!ValidateFileType(fileName))
{
ShowNotify("无效的文件类型!");
return;
}
fileName = fileName.Replace(":", "_").Replace(" ", "_").Replace("\\", "_").Replace("/", "_");
fileName = DateTime.Now.Ticks.ToString() + "_" + fileName;
string url = "~/FileUpload/PersonBaseInfo/" + DateTime.Now.Year + "-" + DateTime.Now.Month + "/";
filePhoto.SaveAs(Server.MapPath(url + fileName));
imgPhoto.ImageUrl = url + fileName;
// 清空文件上传组件
filePhoto.Reset();
}
}
#endregion
#region 验证身份证 卡号是否存在
///
/// 验证身份证 卡号是否存在
///
///
///
protected void TextBox_TextChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(this.txtCardNo.Text))
{
var q = Funs.DB.SitePerson_Person.FirstOrDefault(x =>
x.ProjectId == this.CurrUser.LoginProjectId && x.CardNo == this.txtCardNo.Text.Trim() &&
(x.PersonId != this.PersonId || (this.PersonId == null && x.PersonId != null)));
if (q != null)
{
ShowNotify("输入的卡号已存在!", MessageBoxIcon.Warning);
}
}
if (!string.IsNullOrEmpty(this.txtIdentityCard.Text))
{
var q2 = Funs.DB.SitePerson_Person.FirstOrDefault(x =>
x.ProjectId == this.CurrUser.LoginProjectId && x.IdentityCard == this.txtIdentityCard.Text.Trim() &&
(x.PersonId != this.PersonId || (this.PersonId == null && x.PersonId != null)));
if (q2 != null)
{
ShowNotify("输入的身份证号码已存在!", MessageBoxIcon.Warning);
}
}
}
#endregion
#region 判断按钮权限
///
/// 判断按钮权限
///
private void GetButtonPower()
{
if (Request.Params["value"] == "0")
{
return;
}
var buttonList = BLL.CommonService.GetAllButtonList(this.CurrUser.LoginProjectId, this.CurrUser.UserId,
BLL.Const.PersonListMenuId);
if (buttonList.Count() > 0)
{
if (buttonList.Contains(BLL.Const.BtnSave))
{
this.btnSave.Hidden = false;
this.filePhoto.Hidden = false;
}
}
}
#endregion
///
///
///
///
///
protected void btnQR_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.txtIdentityCard.Text.Trim()))
{
Alert.ShowInTop("身份证号码不能为空!", MessageBoxIcon.Warning);
return;
}
if (string.IsNullOrEmpty(this.txtPersonName.Text))
{
ShowNotify("人员姓名不能为空!", MessageBoxIcon.Warning);
return;
}
if (string.IsNullOrEmpty(this.txtInTime.Text))
{
ShowNotify("入场时间不能为空!", MessageBoxIcon.Warning);
return;
}
if (string.IsNullOrEmpty(this.PersonId))
{
this.SaveData();
}
string strCode = "person$" + this.txtIdentityCard.Text.Trim();
PageContext.RegisterStartupScript(Window1.GetShowReference(
String.Format("~/Controls/SeeQRImage.aspx?PersonId={0}&strCode={1}", this.PersonId, strCode), "二维码查看",
400, 400));
}
#region 附件上传
///
/// 上传附件
///
///
///
protected void btnAttachUrl3_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.PersonId))
{
SaveData();
}
PageContext.RegisterStartupScript(WindowAtt.GetShowReference(String.Format(
"~/AttachFile/webuploader.aspx?toKeyId={0}&path=FileUpload/PersonBaseInfo&menuId={1}&strParam=3&type=0",
this.PersonId, BLL.Const.PersonListMenuId)));
}
#endregion
protected void txtIdentityCard_TextChanged(object sender, EventArgs e)
{
bool isok = true;
if (!string.IsNullOrEmpty(this.txtIdentityCard.Text))
{
string idCard = this.txtIdentityCard.Text.Trim();
var q2 = Funs.DB.SitePerson_Person.FirstOrDefault(x =>
x.ProjectId == this.CurrUser.LoginProjectId && x.IdentityCard == idCard &&
(x.PersonId != this.PersonId || (this.PersonId == null && x.PersonId != null)));
if (q2 != null)
{
isok = false;
ShowNotify("输入的身份证号码已存在!", MessageBoxIcon.Warning);
}
if (this.drpIdcardType.SelectedValue == "SHENFEN_ZHENGJIAN")
{
if (!IDCardValid.CheckIDCard(idCard))
{
isok = false;
ShowNotify("输入的身份证号码有误!", MessageBoxIcon.Warning);
}
}
if (isok)
{
string birthDateStr = "";
if (idCard.Length == 18) //如果是新的二代身份证,直接取中间8位日期
{
birthDateStr = idCard.Substring(6, 8);
}
else if (idCard.Length == 15) //如果是老的一代身份证,取中间6位日期
{
birthDateStr = "19" + idCard.Substring(6, 6);
}
var Birthday = DateTime.ParseExact(birthDateStr, "yyyyMMdd", null);
this.txtBirthday.SelectedDate = Birthday;
}
}
}
protected void drpUnitId_OnSelectedIndexChanged(object sender, EventArgs e)
{
TeamGroupService.InitTeamGroupProjectUnitDropDownList(this.drpTeamGroup, this.ProjectId,
this.drpUnitId.SelectedValue, true);
}
protected void txtPersonName_TextChanged(object sender, EventArgs e)
{
var model = (from x in Funs.DB.Sys_User where x.UserName == txtPersonName.Text.Trim() select x)
.FirstOrDefault();
if (model != null)
{
txtPersonName.Text = model.UserName;
txtTelephone.Text = model.Telephone;
txtIdentityCard.Text = model.IdentityCard;
txtIdentityCard_TextChanged(null, null);
rblSex.SelectedValue = model.Sex == "1" ? "1" : "2";
txtRemark.Text = model.Remark;
}
}
}
}