public ActionResult Edit(CasContractApprovalStepEntity entity, string id, string type)
        {
            string typeValue = "";

            if (type == "check")
            {
                ViewBag.Type = "3";
                typeValue    = "3";
            }
            //entity.Status = Convert.ToInt32(entity.submitType);
            string strError = "";

            if (!IsPost)
            {
                entity = BusinessDataService.ContractApprovalSetService.CreateContractApprovalSetEntity("MDM");
                if (!string.IsNullOrEmpty(id))
                {
                    entity = BusinessDataService.ContractApprovalSetService.GetById <CasContractApprovalStepEntity>(id);
                    //编辑
                    var exist = BusinessDataService.ContractApprovalSetService.CheckInProcessContract(entity.ContractApprovalStepId);
                    if (!exist)
                    {
                        ViewBag.Exist = "1";//存在审批中的合同
                    }
                    ViewBag.EditType = (!string.IsNullOrWhiteSpace(typeValue))? typeValue:"0";
                }
                else
                {
                    //新增
                    ViewBag.EditType = "1";
                }
                return(View(entity));
            }
            else
            {
                if (BusinessDataService.ContractApprovalSetService.SaveContractApprovalSetEntity(entity, ref strError))
                {
                    return(Json(AjaxResult.Success()));
                }
                else
                {
                    strError = "Update failed";
                    return(Json(AjaxResult.Error(strError)));
                }
                //ViewBag.strError = strError;
                //return View(new CasContractApprovalStepEntity());
            }
        }
        /// <summary>
        /// 更新合同审批
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool UpdateApprovalEntites(CasContractApprovalStepEntity entity)
        {
            if (string.IsNullOrWhiteSpace(entity.ContractApprovalStepId))
            {
                return(false);
            }
            var strsql = new StringBuilder();

            strsql.AppendFormat("DELETE FROM CAS_CONTRACT_APPROVAL_STEP_APPROVAL_DEPT WHERE CONTRACT_APPROVAL_STEP_ID= {0}",
                                Utils.ToSQLStr(entity.ContractApprovalStepId).Trim());
            var val             = DataAccess.SelectScalar(strsql.ToString());
            var approvalEntites = new List <CasContractApprovalStepApprovalDeptEntity>();

            string[] approvalD = new string[] { };
            if (!string.IsNullOrWhiteSpace(entity.ExaminationValue))
            {
                approvalD = entity.ExaminationValue.ToString().Split(',');
            }
            for (int j = 0; j < approvalD.Length; j++)
            {
                var approvalEntity = new CasContractApprovalStepApprovalDeptEntity();
                approvalEntity.ContractApprovalStepApprovalDeptId = Guid.NewGuid().ToString();
                approvalEntity.ContractApprovalStepId             = entity.ContractApprovalStepId;
                approvalEntity.DeptId           = approvalD[j];
                approvalEntity.CreatedBy        = WebCaching.UserId;
                approvalEntity.CreateTime       = DateTime.Now;
                approvalEntity.LastModifiedBy   = WebCaching.UserId;
                approvalEntity.LastModifiedTime = DateTime.Now;
                approvalEntites.Add(approvalEntity);
            }
            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    DataAccess.Insert <CasContractApprovalStepApprovalDeptEntity>(approvalEntites, broker);
                    DataAccess.Update(entity, broker);
                    broker.Commit();
                    return(true);
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    return(false);
                }
            }
        }
 public virtual bool SaveContractApprovalSetEntity(CasContractApprovalStepEntity contractApprovalSetEntity, ref string strError)
 {
     contractApprovalSetEntity.LastModifiedBy   = WebCaching.UserAccount;
     contractApprovalSetEntity.LastModifiedTime = DateTime.Now;
     try
     {
         //var isExist = IsExist(contractApprovalSetEntity, ref strError);
         if (!string.IsNullOrEmpty(contractApprovalSetEntity.ContractApprovalStepId))
         {
             //更新
             if (UpdateApprovalEntites(contractApprovalSetEntity))
             {
                 return(true);
             }
         }
         else
         {
             //var stepExist = IsExistStep(contractApprovalSetEntity, ref strError);
             var applierExist = IsExistApplier(contractApprovalSetEntity, ref strError);
             if (!applierExist)
             {
                 //新增
                 contractApprovalSetEntity.ContractApprovalStepId = Guid.NewGuid().ToString();
                 contractApprovalSetEntity.CreatedBy  = WebCaching.UserAccount;
                 contractApprovalSetEntity.CreateTime = DateTime.Now;
                 contractApprovalSetEntity.IsDeleted  = false;
                 //if (Insert(contractApprovalSetEntity))
                 if (InsertApprovalEntites(contractApprovalSetEntity))
                 {
                     return(true);
                 }
             }
         }
     }
     catch (Exception exception)
     {
         strError = exception.Message;
     }
     return(false);
 }
        public ActionResult Delet(CasContractApprovalStepEntity entity, string id)
        {
            var a = BusinessDataService.ContractApprovalSetService.DeleteApprovalStep(id);

            return(Json(AjaxResult.Success()));
        }
        /// <summary>
        /// 保存新增数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool InsertApprovalEntites(CasContractApprovalStepEntity entity)
        {
            var applyEntites    = new List <CasContractApprovalStepApplyDeptEntity>();
            var approvalEntites = new List <CasContractApprovalStepApprovalDeptEntity>();

            string[] applyID;
            if (entity.ApprovalRole == ApplyTypeEnum.AllDepartment.GetHashCode())
            {
                applyID             = GetAllDepartmentID();
                entity.ApprovalRole = ApplyTypeEnum.Department.GetHashCode();
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(entity.ApprovalDepValue) && entity.ApprovalRole == ApplyTypeEnum.Department.GetHashCode())
                {
                    applyID = entity.ApprovalDepValue.ToString().Split(',');
                }
                else
                {
                    applyID = entity.ApprovalUserValue.ToString().Split(',');
                }
            }
            string[] approvalD = new string[] { };
            if (!string.IsNullOrWhiteSpace(entity.ExaminationValue))
            {
                approvalD = entity.ExaminationValue.ToString().Split(',');
            }
            for (int i = 0; i < applyID.Length; i++)
            {
                var applyEntity = new CasContractApprovalStepApplyDeptEntity();
                applyEntity.ContractApprovalStepApplyDeptId = Guid.NewGuid().ToString();
                applyEntity.ContractApprovalStepId          = entity.ContractApprovalStepId;
                applyEntity.ApplyType        = (int)entity.ApprovalRole;
                applyEntity.DeptId           = applyID[i];
                applyEntity.CreatedBy        = WebCaching.UserId;
                applyEntity.CreateTime       = DateTime.Now;
                applyEntity.LastModifiedBy   = WebCaching.UserId;
                applyEntity.LastModifiedTime = DateTime.Now;
                applyEntites.Add(applyEntity);
            }
            for (int j = 0; j < approvalD.Length; j++)
            {
                var approvalEntity = new CasContractApprovalStepApprovalDeptEntity();
                approvalEntity.ContractApprovalStepApprovalDeptId = Guid.NewGuid().ToString();
                approvalEntity.ContractApprovalStepId             = entity.ContractApprovalStepId;
                approvalEntity.DeptId           = approvalD[j];
                approvalEntity.CreatedBy        = WebCaching.UserId;
                approvalEntity.CreateTime       = DateTime.Now;
                approvalEntity.LastModifiedBy   = WebCaching.UserId;
                approvalEntity.LastModifiedTime = DateTime.Now;
                approvalEntites.Add(approvalEntity);
            }

            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                try
                {
                    broker.BeginTransaction();
                    DataAccess.Insert <CasContractApprovalStepApplyDeptEntity>(applyEntites, broker);
                    DataAccess.Insert <CasContractApprovalStepApprovalDeptEntity>(approvalEntites, broker);
                    DataAccess.Insert(entity, broker);
                    broker.Commit();
                    return(true);
                }
                catch (Exception ex)
                {
                    broker.RollBack();
                    return(false);
                }
            }
        }
        ///// <summary>
        ///// 判断申请步骤是否重复
        ///// </summary>
        ///// <param name="contractApprovalSetEntity"></param>
        ///// <param name="strError"></param>
        ///// <returns></returns>
        //public bool IsExistStep(CasContractApprovalStepEntity contractApprovalSetEntity, ref string strError)
        //{
        //    var strsql = new StringBuilder();
        //    strsql.AppendFormat("SELECT 1 FROM CAS_CONTRACT_APPROVAL_STEP WHERE CONTRACT_TYPE_ID= {0} AND STEP='{1}'",
        //        Utils.ToSQLStr(contractApprovalSetEntity.ContractTypeId).Trim(), contractApprovalSetEntity.Step);
        //    var val = DataAccess.SelectScalar(strsql.ToString());
        //    if (string.IsNullOrEmpty(val) || val != "1") return false;
        //    strError = "该合同类型的该审批步骤已经存在";
        //    return true;
        //}


        /// <summary>
        /// 判断申请人是否重复
        /// </summary>
        /// <param name="contractApprovalSetEntity"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        public bool IsExistApplier(CasContractApprovalStepEntity contractApprovalSetEntity, ref string strError)
        {
            string strSql = "";

            if (contractApprovalSetEntity.ApprovalRole == ApplyTypeEnum.Department.GetHashCode())
            {
                string deptIds = "'" + contractApprovalSetEntity.ApprovalDepValue.Replace(",", "','") + "'";
                strSql = $@"SELECT * FROM 
                                (
                                    SELECT CAS_CONTRACT_APPROVAL_STEP_APPLY_DEPT.DEPT_ID USER_ID
                                      FROM CAS_CONTRACT_APPROVAL_STEP_APPLY_DEPT
                                INNER JOIN CAS_CONTRACT_APPROVAL_STEP
	                                    ON CAS_CONTRACT_APPROVAL_STEP_APPLY_DEPT.CONTRACT_APPROVAL_STEP_ID 
                                           = CAS_CONTRACT_APPROVAL_STEP.CONTRACT_APPROVAL_STEP_ID
                                     WHERE CAS_CONTRACT_APPROVAL_STEP.CONTRACT_TYPE_ID = '{contractApprovalSetEntity.ContractTypeId}'
                                       AND CAS_CONTRACT_APPROVAL_STEP.STEP = {contractApprovalSetEntity.Step}
                                       AND APPLY_TYPE = {ApplyTypeEnum.User.GetHashCode()}
                                UNION
                                    SELECT CAS_DEPT_USER.USER_ID
                                      FROM CAS_CONTRACT_APPROVAL_STEP_APPLY_DEPT
                                INNER JOIN CAS_DEPT_USER ON CAS_CONTRACT_APPROVAL_STEP_APPLY_DEPT.DEPT_ID =CAS_DEPT_USER.DEPT_ID
                                INNER JOIN CAS_CONTRACT_APPROVAL_STEP
	                                    ON CAS_CONTRACT_APPROVAL_STEP_APPLY_DEPT.CONTRACT_APPROVAL_STEP_ID 
                                           = CAS_CONTRACT_APPROVAL_STEP.CONTRACT_APPROVAL_STEP_ID
                                     WHERE CAS_CONTRACT_APPROVAL_STEP.CONTRACT_TYPE_ID = '{contractApprovalSetEntity.ContractTypeId}'
                                       AND CAS_CONTRACT_APPROVAL_STEP.STEP = {contractApprovalSetEntity.Step}
                                       AND APPLY_TYPE = {ApplyTypeEnum.Department.GetHashCode()}
                                ) TEMP
                                WHERE USER_ID IN 
                                (SELECT USER_ID FROM CAS_DEPT_USER WHERE DEPT_ID IN({deptIds}))";
            }
            else if (contractApprovalSetEntity.ApprovalRole == ApplyTypeEnum.User.GetHashCode())
            {
                string userIds = "'" + contractApprovalSetEntity.ApprovalUserValue.Replace(",", "','") + "'";
                strSql = $@"SELECT * FROM 
                                (
                                    SELECT CAS_CONTRACT_APPROVAL_STEP_APPLY_DEPT.DEPT_ID USER_ID
                                      FROM CAS_CONTRACT_APPROVAL_STEP_APPLY_DEPT
                                INNER JOIN CAS_CONTRACT_APPROVAL_STEP
	                                    ON CAS_CONTRACT_APPROVAL_STEP_APPLY_DEPT.CONTRACT_APPROVAL_STEP_ID 
                                           = CAS_CONTRACT_APPROVAL_STEP.CONTRACT_APPROVAL_STEP_ID
                                     WHERE CAS_CONTRACT_APPROVAL_STEP.CONTRACT_TYPE_ID = '{contractApprovalSetEntity.ContractTypeId}'
                                       AND CAS_CONTRACT_APPROVAL_STEP.STEP = {contractApprovalSetEntity.Step}
                                       AND APPLY_TYPE = {ApplyTypeEnum.User.GetHashCode()}
                                UNION
                                    SELECT CAS_DEPT_USER.USER_ID
                                      FROM CAS_CONTRACT_APPROVAL_STEP_APPLY_DEPT
                                INNER JOIN CAS_DEPT_USER ON CAS_CONTRACT_APPROVAL_STEP_APPLY_DEPT.DEPT_ID =CAS_DEPT_USER.DEPT_ID
                                INNER JOIN CAS_CONTRACT_APPROVAL_STEP
	                                    ON CAS_CONTRACT_APPROVAL_STEP_APPLY_DEPT.CONTRACT_APPROVAL_STEP_ID 
                                           = CAS_CONTRACT_APPROVAL_STEP.CONTRACT_APPROVAL_STEP_ID
                                     WHERE CAS_CONTRACT_APPROVAL_STEP.CONTRACT_TYPE_ID = '{contractApprovalSetEntity.ContractTypeId}'
                                       AND CAS_CONTRACT_APPROVAL_STEP.STEP = {contractApprovalSetEntity.Step}
                                       AND APPLY_TYPE = {ApplyTypeEnum.Department.GetHashCode()}
                                ) TEMP
                                WHERE USER_ID IN ({userIds})";
            }
            else
            {
                strSql = $@"SELECT * FROM CAS_CONTRACT_APPROVAL_STEP_APPLY_DEPT
                               INNER JOIN CAS_CONTRACT_APPROVAL_STEP
	                                   ON CAS_CONTRACT_APPROVAL_STEP_APPLY_DEPT.CONTRACT_APPROVAL_STEP_ID 
                                          = CAS_CONTRACT_APPROVAL_STEP.CONTRACT_APPROVAL_STEP_ID
                                    WHERE CAS_CONTRACT_APPROVAL_STEP.CONTRACT_TYPE_ID = '{contractApprovalSetEntity.ContractTypeId}'
                                      AND CAS_CONTRACT_APPROVAL_STEP.STEP = {contractApprovalSetEntity.Step}";
            }
            var val = DataAccess.SelectScalar(strSql.ToString());

            if (!string.IsNullOrEmpty(val))
            {
                strError = "该合同类型的申请部门和已有申请部门重复";
                return(true);
            }
            return(false);
        }