//public JsonResult GetCommentResults(JqGrid grid, string id)
        //{
        //    //领导批注结果
        //    DataTable dt = BusinessDataService.ContractCommentService.GetCommentResultDt(id);
        //    grid.DataBind(dt, 10);
        //    return Json(AjaxResult.Success(BusinessDataService.ContractCommentService.ForGrid(grid)));
        //}

        /// <summary>
        /// 领导批注
        /// </summary>
        /// <returns></returns>
        public JsonResult OptionComment(string option, string contractId)
        {
            CasContractApproverEntity casContractApproverEntity = new CasContractApproverEntity
            {
                ContractId   = contractId,
                ApproverId   = CurrentUser.CasUserEntity.UserId,
                ApproverType = 1, //审批用户类型:领导
                Status       = 2  //审批结果:待审批
            };

            casContractApproverEntity = BusinessDataService.CommonHelperService.GetApprover(casContractApproverEntity);
            //找不到审批人数据
            if (casContractApproverEntity == null)
            {
                return(Json(AjaxResult.Error("Approval data exception!")));
            }
            CasContractApprovalResultEntity casContractApprovalResultEntity = new CasContractApprovalResultEntity
            {
                ContractApprovalResultId = Guid.NewGuid().ToString(),
                ApprovalResult           = 1, //审批结果:已批注
                ApproverType             = 1, //审批用户类型:领导
                ApprovalOpinions         = option,
                ApproverId             = CurrentUser.CasUserEntity.UserId,
                ContractApprovalStepId = casContractApproverEntity.ContractApprovalStepId,
                ContractId             = casContractApproverEntity.ContractId,
                CreatedBy        = CurrentUser.CasUserEntity.UserId,
                CreateTime       = DateTime.Now,
                ApprovalTime     = DateTime.Now,
                LastModifiedBy   = CurrentUser.CasUserEntity.UserId,
                LastModifiedTime = DateTime.Now
            };
            bool flag = BusinessDataService.ContractCommentService.OptionCommnet(casContractApprovalResultEntity);

            return(Json(flag ? AjaxResult.Success() : AjaxResult.Error()));
        }
        /// <summary>
        /// 领导批注
        /// </summary>
        /// <param name="casContractApprovalResultEntity"></param>
        /// <returns></returns>
        public bool OptionCommnet(CasContractApprovalResultEntity casContractApprovalResultEntity)
        {
            bool flag = false;

            using (DataAccessBroker broker = DataAccessFactory.Instance())
            {
                broker.BeginTransaction();
                try
                {
                    string sql = $@"UPDATE dbo.CAS_CONTRACT_APPROVER SET STATUS = 3 
                                    WHERE APPROVER_TYPE = 1 
                                    AND CONTRACT_ID = '{casContractApprovalResultEntity.ContractId}'
                                    AND APPROVER_ID = '{casContractApprovalResultEntity.ApproverId}'
                                    AND STATUS = 2";

                    //审批人表更改领导批注状态为已批注
                    broker.ExecuteSQL(sql);

                    //插入审批结果
                    flag = DataAccess.Insert(casContractApprovalResultEntity);
                    if (flag)
                    {
                        broker.Commit();
                    }
                    else
                    {
                        broker.RollBack();
                    }
                }
                catch (Exception e)
                {
                    flag = false;
                    broker.RollBack();
                    SystemService.LogErrorService.InsertLog(e);
                }
            }

            return(flag);
        }
        /// <summary>
        /// 审批人审批
        /// </summary>
        /// <param name="comment"></param>
        /// <param name="contractApproverId"></param>
        /// <returns></returns>
        public JsonResult Submit(int approvalType, string contractApproverId, string comment)
        {
            bool   flag = false;
            string msg  = "";
            //获取审批人信息
            CasContractApproverEntity casContractApproverEntity = new CasContractApproverEntity {
                ContractApproverId = contractApproverId
            };

            casContractApproverEntity = BusinessDataService.CommonHelperService.GetApprover(casContractApproverEntity);
            if (casContractApproverEntity != null)
            {
                CasContractEntity casContractEntity = BusinessDataService.CommonHelperService.GetById <CasContractEntity>(casContractApproverEntity.ContractId);
                CasUserEntity     userEntity        = BusinessDataService.CommonHelperService.GetById <CasUserEntity>(casContractEntity.CreatedBy);
                if (casContractEntity.Status != ContractStatusEnum.WaitApproval.GetHashCode() && casContractEntity.Status != ContractStatusEnum.Resubmit.GetHashCode())
                {
                    flag = false;
                    msg  = "Approve failed: the contract is not in review process.";
                    return(Json(AjaxResult.Error(msg)));
                }

                if (casContractApproverEntity.Status == ContractApproverStatusEnum.WaitApproval.GetHashCode() || casContractApproverEntity.Status == ContractApproverStatusEnum.OverTime.GetHashCode())
                {
                    CasContractApprovalResultEntity casContractApprovalResultEntity = new CasContractApprovalResultEntity
                    {
                        ContractApprovalResultId = Guid.NewGuid().ToString(),
                        ApprovalResult           = approvalType,                           //审批结果
                        ApproverType             = casContractApproverEntity.ApproverType, //审批用户类型
                        ApprovalOpinions         = comment,
                        ApproverId             = CurrentUser.CasUserEntity.UserId,
                        ContractApprovalStepId = casContractApproverEntity.ContractApprovalStepId,
                        ContractId             = casContractApproverEntity.ContractId,
                        CreatedBy        = CurrentUser.CasUserEntity.UserId,
                        CreateTime       = DateTime.Now,
                        ApprovalTime     = DateTime.Now,
                        LastModifiedBy   = CurrentUser.CasUserEntity.UserId,
                        LastModifiedTime = DateTime.Now
                    };
                    flag = BusinessDataService.ContractApprovalService.Approval(casContractApprovalResultEntity, casContractApproverEntity);
                    if (flag && approvalType == 4)
                    {
                        var title    = $@"e-Approval – Contract Reject";
                        var content  = $@"Dear:{userEntity.EnglishName},</br>尊敬的:{userEntity.ChineseName}</br></br>The following contract is rejected:</br>以下合同被拒绝:</br></br>Contract Name 合同名称:{casContractEntity.ContractName}  {casContractEntity.TemplateName}</br>Ferrero Entity  费列罗方 :{casContractEntity.FerreroEntity}</br>Counter Party  相对方:{casContractEntity.CounterpartyEn}  {casContractEntity.CounterpartyCn}</br>Rejected by 拒绝审批人:{ CurrentUser.CasUserEntity.EnglishName}{ CurrentUser.CasUserEntity.ChineseName}</br>Reject Reason 拒绝理由:{comment}</br></br>Please review your contract and resubmit or close the request.</br>请检查合同相关内容,重新提交或关闭合同。</br></br>Ferrero China Contract Approval System</br>费列罗中国合同审批系统";
                        var cc       = "*****@*****.**";
                        var reciever = userEntity.Email;
                        SendEmail.Send(reciever, cc, title, content);
                    }
                }
                else
                {
                    flag = false;
                    msg  = "Approve failed: the contract is already approved.";
                }
            }
            else
            {
                flag = false;
                msg  = "Operation failed: cannot find the approver's info.";
            }
            return(Json(flag ? AjaxResult.Success() : AjaxResult.Error(msg)));
        }