Ejemplo n.º 1
0
        public ActionResult Su_Repayment()
        {
            Utils.SetSYSDateTimeFormat();

            chuangtouEntities ef  = new chuangtouEntities();
            string            str = "";
            ReRepayment       Re  = new ReRepayment();

            Re.CmdId        = DNTRequest.GetString("CmdId");
            Re.RespCode     = DNTRequest.GetString("RespCode");
            Re.MerCustId    = DNTRequest.GetString("MerCustId");
            Re.ProId        = DNTRequest.GetString("ProId");
            Re.OrdId        = DNTRequest.GetString("OrdId");
            Re.OrdDate      = DNTRequest.GetString("OrdDate");
            Re.OutCustId    = DNTRequest.GetString("OutCustId");
            Re.SubOrdId     = DNTRequest.GetString("SubOrdId");
            Re.SubOrdDate   = DNTRequest.GetString("SubOrdDate");
            Re.OutAcctId    = DNTRequest.GetString("OutAcctId");
            Re.TransAmt     = DNTRequest.GetString("TransAmt");
            Re.PrincipalAmt = DNTRequest.GetString("PrincipalAmt");
            Re.InterestAmt  = DNTRequest.GetString("InterestAmt");
            Re.Fee          = DNTRequest.GetString("Fee");
            Re.InCustId     = DNTRequest.GetString("InCustId");
            Re.InAcctId     = DNTRequest.GetString("InAcctId");
            Re.FeeObjFlag   = DNTRequest.GetString("FeeObjFlag");
            Re.DzObject     = DNTRequest.GetString("DzObject");
            Re.BgRetUrl     = DNTRequest.GetString("BgRetUrl");
            Re.MerPriv      = DNTRequest.GetString("MerPriv");
            Re.RespExt      = DNTRequest.GetString("RespExt");
            LogInfo.WriteLog("后台还款返回报文:" + FastJSON.toJOSN(Re));

            string version = "20";

            if (string.IsNullOrEmpty(Re.TransAmt) || Re.TransAmt == "0.00")
            {
                version = "30";
            }
            StringBuilder builder = new StringBuilder();

            builder.Append(Re.CmdId);
            builder.Append(Re.RespCode);
            builder.Append(Re.MerCustId);
            if (version == "30")
            {
                builder.Append(Re.ProId);
            }
            builder.Append(Re.OrdId);
            builder.Append(Re.OrdDate);
            builder.Append(Re.OutCustId);
            builder.Append(Re.SubOrdId);
            builder.Append(Re.SubOrdDate);
            builder.Append(Re.OutAcctId);
            if (version == "30")
            {
                builder.Append(Re.PrincipalAmt);
                builder.Append(Re.InterestAmt);
            }
            else
            {
                builder.Append(Re.TransAmt);
            }
            builder.Append(Re.Fee);
            builder.Append(Re.InCustId);
            builder.Append(Re.InAcctId);
            builder.Append(Re.FeeObjFlag);
            if (version == "30")
            {
                builder.Append(Re.DzObject);
            }
            builder.Append(HttpUtility.UrlDecode(Re.BgRetUrl));
            builder.Append(Re.MerPriv);
            builder.Append(HttpUtility.UrlDecode(Re.RespExt));
            // builder.Append(Re.ChkValue);
            var msg = builder.ToString();

            if (version == "30")
            {
                msg = Utils.MD5(msg);
            }
            string pgPubkFile = AppDomain.CurrentDomain.BaseDirectory + Utils.GetPgPubk();
            int    ret        = DllInterop.VeriSignMsg(pgPubkFile, msg, msg.Length, Re.ChkValue);

            LogInfo.WriteLog("后台验签 ret= " + ret.ToString());
            if (ret == 0)
            {
                if (Re.RespCode == "000" || Re.TransAmt == "0.00")
                {
                    string cachename = "Repayment" + Re.OrdId;
                    if (Utils.GeTThirdCache(cachename) == 0)
                    {
                        Utils.SetThirdCache(cachename);

                        B_member_table          o   = new B_member_table();
                        M_member_table          p   = new M_member_table();
                        M_member_table          p2  = new M_member_table();
                        M_Capital_account_water baw = new M_Capital_account_water();
                        V_borrowing_Bid_records_income_statement item = new V_borrowing_Bid_records_income_statement();
                        int income_statement_id = int.Parse(Re.MerPriv);
                        item = ef.V_borrowing_Bid_records_income_statement.AsNoTracking().Where(ps => ps.income_statement_id == income_statement_id).FirstOrDefault();
                        p    = o.GetModel(int.Parse(item.borrower_registerid.ToString())); //借款人用户对象

                        baw.membertable_registerid = p.registerid;
                        baw.income             = 0.00M;
                        baw.expenditure        = decimal.Parse(Re.TransAmt);
                        baw.time_of_occurrence = DateTime.Now;
                        baw.account_balance    = p.available_balance - baw.expenditure; //面要得么帐户余额
                        baw.types_Finance      = (int)Enum.Parse(typeof(EnumTypesFinance), EnumTypesFinance.还款.ToString());
                        baw.createtime         = DateTime.Now;
                        baw.keyid   = 0;
                        baw.remarks = Re.OrdId + "," + Re.OrdDate;

                        M_Capital_account_water iaw = new M_Capital_account_water(); //投资人流水对象


                        p2 = o.GetModel(int.Parse(item.investor_registerid.ToString())); //投资人用户对象

                        iaw.membertable_registerid = p2.registerid;
                        iaw.income             = decimal.Parse(Re.TransAmt);
                        iaw.expenditure        = 0.00M;
                        iaw.time_of_occurrence = DateTime.Now;
                        iaw.account_balance    = p2.available_balance + iaw.income; //面要得么帐户余额
                        iaw.types_Finance      = (int)Enum.Parse(typeof(EnumTypesFinance), EnumTypesFinance.借款人还款.ToString());
                        iaw.createtime         = DateTime.Now;
                        iaw.keyid   = 0;
                        iaw.remarks = Re.OrdId + "," + Re.OrdDate;


                        int targetid = int.Parse(item.targetid.ToString());
                        //判断是否为最后一期还款,并进行相应状态处理
                        hx_repayment_plan plan = (from a in ef.hx_repayment_plan where a.targetid == targetid select a).OrderByDescending(a => a.current_period).Take(1).FirstOrDefault();
                        var lastcur            = plan.current_period;
                        //判断是否是最后一期,如果是对本金处理。得直接用最后一期还款金额减去本金,这样就解决多出本金问题
                        bool lastrepamt = false;

                        if (item.current_investment_period.ToString() == lastcur.ToString())
                        {
                            lastrepamt = true;
                        }



                        //需要更新投资记录表已还款金额

                        B_usercenter BUC = new B_usercenter();

                        decimal PrincipalInterest = decimal.Parse(item.Principal.ToString()) + decimal.Parse(item.interestpayment.ToString()); //本金加利息

                        decimal PInterest = decimal.Parse(item.interestpayment.ToString());                                                    //利息

                        Re.MerPriv = DateTime.Parse(item.interest_payment_date.ToString()).ToString("yyyy-MM-dd");                             //传入还款日期
                        int bucd = BUC.Repayment_Successfully(Re, baw, iaw, lastrepamt, PrincipalInterest, PInterest);

                        if (bucd > 0)
                        {
                            // 尊敬#UserName#,您投资的第#PID#号标第#ORDER#期还款已到帐,本次已成功还款#MONEY#元.欢迎继续投资!【创利投】



                            #region 短信通知
                            //短信通知

                            string contxt = Utils.GetMSMEmailContext(11, 1); // 获取注册成功邮件内容

                            StringBuilder sbsms = new StringBuilder(contxt);

                            sbsms = sbsms.Replace("#USERANEM#", item.username.ToString());

                            sbsms = sbsms.Replace("#PID#", item.targetid.ToString());

                            sbsms = sbsms.Replace("#ORDER#", item.current_investment_period.ToString());

                            sbsms = sbsms.Replace("#MONEY#", item.repayment_amount.ToString());


                            string mobile = item.mobile.ToString();

                            M_td_SMS_record psms    = new M_td_SMS_record();
                            B_td_SMS_record osms    = new B_td_SMS_record();
                            int             smstype = (int)Enum.Parse(typeof(EnumSMSType), EnumSMSType.投资回款.ToString());
                            psms.phone_number = mobile;
                            psms.sendtime     = DateTime.Now;
                            psms.senduserid   = int.Parse(item.investor_registerid.ToString());
                            psms.smstype      = smstype;
                            psms.smscontext   = sbsms.ToString();
                            psms.orderid      = SendSMS.Send_SMS(mobile, sbsms.ToString());
                            psms.vcode        = "";

                            osms.Add(psms);
                            #endregion


                            #region 系统消息
                            //系统消息


                            DateTime dti = DateTime.Now;

                            M_td_System_message pm = new M_td_System_message();
                            pm.MReg   = int.Parse(item.investor_registerid.ToString());
                            pm.Mstate = 0;
                            pm.MTitle = "投资回款";
                            //  pm.MContext = "尊敬的用户" + dt.Rows[0]["username"].ToString() + ":您好!恭喜您成功投资了项目【" + dt.Rows[0]["borrowing_title"].ToString() + "】,投资金额是:" + dt.Rows[0]["investment_amount"].ToString() + "。如有问题可咨询创利投的客服!谢谢!";
                            pm.MContext = sbsms.ToString();
                            pm.PubTime  = dti;
                            pm.Mtype    = 2;
                            B_usercenter.AddMessage(pm);


                            #endregion

                            #region 更新标的状态为还还清
                            ///更新标的状态为还还清

                            if (item.current_investment_period.ToString() == lastcur.ToString())
                            {
                                //更新标的状态为还还清

                                string sql = "update hx_borrowing_target set tender_state=5 where targetid=" + item.targetid.ToString() + "";
                                DbHelperSQL.ExecuteSql(sql);

                                sql = "update  hx_Bid_records  set  payment_status =1 where bid_records_id=" + item.bid_records_id.ToString();
                                DbHelperSQL.ExecuteSql(sql);
                            }
                            #endregion


                            //Response.Write("还款验签成功!");
                        }
                        else
                        {
                            //Response.Write("还款更新失败!" + bucd.ToString());
                        }
                    }

                    str = "RECV_ORD_ID_" + Re.OrdId;
                    LogInfo.WriteLog(str + "后台还款成功");
                }
                else
                {
                    string cc = Utils.GetReturnCode(Int32.Parse(Re.RespCode));


                    if (cc.Contains("还款金额超过还款总额") || cc.Contains("重复的还款请求"))
                    {
                        B_member_table          o   = new B_member_table();
                        M_member_table          p   = new M_member_table();
                        M_member_table          p2  = new M_member_table();
                        M_Capital_account_water baw = new M_Capital_account_water();
                        V_borrowing_Bid_records_income_statement item = new V_borrowing_Bid_records_income_statement();
                        int income_statement_id = int.Parse(Re.MerPriv);
                        item = ef.V_borrowing_Bid_records_income_statement.AsNoTracking().Where(ps => ps.income_statement_id == income_statement_id).FirstOrDefault();
                        p    = o.GetModel(int.Parse(item.borrower_registerid.ToString())); //借款人用户对象

                        baw.membertable_registerid = p.registerid;
                        baw.income             = 0.00M;
                        baw.expenditure        = decimal.Parse(Re.TransAmt);
                        baw.time_of_occurrence = DateTime.Now;
                        baw.account_balance    = p.available_balance - baw.expenditure; //面要得么帐户余额
                        baw.types_Finance      = (int)Enum.Parse(typeof(EnumTypesFinance), EnumTypesFinance.还款.ToString());
                        baw.createtime         = DateTime.Now;
                        baw.keyid   = 0;
                        baw.remarks = Re.OrdId + "," + Re.OrdDate;

                        M_Capital_account_water iaw = new M_Capital_account_water(); //投资人流水对象


                        p2 = o.GetModel(int.Parse(item.investor_registerid.ToString())); //投资人用户对象

                        iaw.membertable_registerid = p2.registerid;
                        iaw.income             = decimal.Parse(Re.TransAmt);
                        iaw.expenditure        = 0.00M;
                        iaw.time_of_occurrence = DateTime.Now;
                        iaw.account_balance    = p2.available_balance + iaw.income; //面要得么帐户余额
                        iaw.types_Finance      = (int)Enum.Parse(typeof(EnumTypesFinance), EnumTypesFinance.借款人还款.ToString());
                        iaw.createtime         = DateTime.Now;
                        iaw.keyid   = 0;
                        iaw.remarks = Re.OrdId + "," + Re.OrdDate;


                        int targetid = int.Parse(item.targetid.ToString());
                        //判断是否为最后一期还款,并进行相应状态处理
                        hx_repayment_plan plan = (from a in ef.hx_repayment_plan where a.targetid == targetid select a).OrderByDescending(a => a.current_period).Take(1).FirstOrDefault();
                        var lastcur            = plan.current_period;
                        //判断是否是最后一期,如果是对本金处理。得直接用最后一期还款金额减去本金,这样就解决多出本金问题
                        bool lastrepamt = false;

                        if (item.current_investment_period.ToString() == lastcur.ToString())
                        {
                            lastrepamt = true;
                        }



                        //需要更新投资记录表已还款金额

                        B_usercenter BUC = new B_usercenter();

                        decimal PrincipalInterest = decimal.Parse(item.Principal.ToString()) + decimal.Parse(item.interestpayment.ToString()); //本金加利息

                        decimal PInterest = decimal.Parse(item.interestpayment.ToString());                                                    //利息

                        Re.MerPriv = DateTime.Parse(item.interest_payment_date.ToString()).ToString("yyyy-MM-dd");                             //传入还款日期
                        int bucd = BUC.Repayment_Successfully(Re, baw, iaw, lastrepamt, PrincipalInterest, PInterest);

                        if (bucd > 0)
                        {
                            // 尊敬#UserName#,您投资的第#PID#号标第#ORDER#期还款已到帐,本次已成功还款#MONEY#元.欢迎继续投资!【创利投】



                            #region 短信通知
                            //短信通知

                            string contxt = Utils.GetMSMEmailContext(11, 1); // 获取注册成功邮件内容

                            StringBuilder sbsms = new StringBuilder(contxt);

                            sbsms = sbsms.Replace("#USERANEM#", item.username.ToString());

                            sbsms = sbsms.Replace("#PID#", item.targetid.ToString());

                            sbsms = sbsms.Replace("#ORDER#", item.current_investment_period.ToString());

                            sbsms = sbsms.Replace("#MONEY#", item.repayment_amount.ToString());


                            string mobile = item.mobile.ToString();

                            M_td_SMS_record psms    = new M_td_SMS_record();
                            B_td_SMS_record osms    = new B_td_SMS_record();
                            int             smstype = (int)Enum.Parse(typeof(EnumSMSType), EnumSMSType.投资回款.ToString());
                            psms.phone_number = mobile;
                            psms.sendtime     = DateTime.Now;
                            psms.senduserid   = int.Parse(item.investor_registerid.ToString());
                            psms.smstype      = smstype;
                            psms.smscontext   = sbsms.ToString();
                            psms.orderid      = SendSMS.Send_SMS(mobile, sbsms.ToString());
                            psms.vcode        = "";

                            osms.Add(psms);
                            #endregion


                            #region 系统消息
                            //系统消息


                            DateTime dti = DateTime.Now;

                            M_td_System_message pm = new M_td_System_message();
                            pm.MReg   = int.Parse(item.investor_registerid.ToString());
                            pm.Mstate = 0;
                            pm.MTitle = "投资回款";
                            //  pm.MContext = "尊敬的用户" + dt.Rows[0]["username"].ToString() + ":您好!恭喜您成功投资了项目【" + dt.Rows[0]["borrowing_title"].ToString() + "】,投资金额是:" + dt.Rows[0]["investment_amount"].ToString() + "。如有问题可咨询创利投的客服!谢谢!";
                            pm.MContext = sbsms.ToString();
                            pm.PubTime  = dti;
                            pm.Mtype    = 2;
                            B_usercenter.AddMessage(pm);


                            #endregion

                            #region 更新标的状态为还还清
                            ///更新标的状态为还还清

                            if (item.current_investment_period.ToString() == lastcur.ToString())
                            {
                                //更新标的状态为还还清

                                string sql = "update hx_borrowing_target set tender_state=5 where targetid=" + item.targetid.ToString() + "";
                                DbHelperSQL.ExecuteSql(sql);

                                sql = "update  hx_Bid_records  set  payment_status =1 where bid_records_id=" + item.bid_records_id.ToString();
                                DbHelperSQL.ExecuteSql(sql);
                            }
                            #endregion


                            //Response.Write("还款验签成功!");
                        }
                        else
                        {
                            //Response.Write("还款更新失败!" + bucd.ToString());
                        }
                    }


                    //Response.Write("出现错误! " + Utils.GetReturnCode(Int32.Parse(Re.RespCode)));

                    /*
                     * string cc = Utils.GetReturnCode(Int32.Parse(Re.RespCode));
                     *
                     *
                     * if (cc.Contains("还款金额超过还款总额") || cc.Contains("重复的还款请求"))
                     * {
                     *
                     *  B_member_table o = new B_member_table();
                     *  M_member_table p = new M_member_table();
                     *  M_member_table p2 = new M_member_table();
                     *  M_Capital_account_water baw = new M_Capital_account_water();
                     *  V_borrowing_Bid_records_income_statement item = new V_borrowing_Bid_records_income_statement();
                     *  int income_statement_id = int.Parse(Re.MerPriv);
                     *  item = ef.V_borrowing_Bid_records_income_statement.AsNoTracking().Where(ps => ps.income_statement_id == income_statement_id).FirstOrDefault();
                     *  p = o.GetModel(int.Parse(item.borrower_registerid.ToString())); //借款人用户对象
                     *
                     *
                     *  int ic = ef.hx_income_statement.Where(c => c.bid_records_id == item.bid_records_id && c.targetid == item.targetid && c.orderid== Re.OrdId).Update(c => new hx_income_statement { payment_status = 1, repayment_period = DateTime.Now });
                     *
                     *  if (ic > 0)
                     *  {
                     *      int userid = int.Parse(item.borrower_registerid.ToString());
                     *      UserInfoData ud = new UserInfoData();
                     *      ReQueryBalanceBg retloan = ud.Querybalance(userid);
                     *
                     *      if (retloan.RespCode == "000")
                     *      {
                     *          // sql = "update  hx_member_table  set  available_balance=" + decimal.Parse(retloan.AvlBal) + " ,frozen_sum=" + decimal.Parse(retloan.FrzBal) + " where  registerid=" + userid.ToString() + "";
                     *
                     *          //sql = "update  hx_member_table  set  available_balance=" + decimal.Parse(retloan.AvlBal) + "  where  registerid=" + userid.ToString() + "";
                     *
                     *          //DbHelperSQL.RunSql(sql);
                     *          B_usercenter bu = new B_usercenter();
                     *          bu.DataSync(retloan, userid.ToString());
                     *
                     *
                     *
                     *
                     *      }
                     *  }
                     */
                }
            }



            return(Content(str));
        }