/// <summary> /// 检查账本是否错误 /// lz /// 2016-09-20 /// </summary> /// <param name="model"></param> /// <returns></returns> public bool ZBError(Tz_Hy_Je model) { try { bool result = true; if (model != null && !string.IsNullOrEmpty(model.id_masteruser) && !string.IsNullOrEmpty(model.id_hy)) { Hashtable ht = new Hashtable(); ht.Add("id_masteruser", model.id_masteruser); ht.Add("id_hy", model.id_hy); var flowList = DAL.QueryList <Tz_Hy_Je_Flow>(typeof(Tz_Hy_Je_Flow), ht); if (flowList != null && flowList.Count() > 0) { var flowJe = flowList.Sum(d => d.je); var flowJeZs = flowList.Sum(d => d.je_zs); if (model.je_qm == flowJe && model.je_qm_zs == flowJeZs) { return(false); } } else { if (model.je_qm == 0 && model.je_qm_zs == 0) { return(false); } } } return(result); } catch (Exception ex) { return(true); } }
/// <summary> /// 检查数据秘钥是否有效 /// lz /// 2016-09-19 /// </summary> /// <param name="model"></param> /// <returns></returns> public bool RecordKeyError(Tz_Hy_Je model) { try { bool result = true; if (model != null) { string recordKey = this.GetRecordKey(model); if (recordKey == model.recodekey) { return(false); } } return(result); } catch (Exception ex) { return(true); } }
/// <summary> /// 获取指定model的秘钥 /// lz /// 2016-09-19 /// </summary> /// <param name="model"></param> /// <returns></returns> public string GetRecordKey(Tz_Hy_Je model) { try { string RecordKey = ""; if (model != null) { StringBuilder strSql = new StringBuilder(); strSql.Append(model.id_masteruser); strSql.Append(model.id); strSql.Append(model.id_hy); strSql.Append(double.Parse(model.je_qm.ToString()).ToString("0.0000000")); strSql.Append(double.Parse(model.je_qm_zs.ToString()).ToString("0.0000000")); strSql.Append(PublicSign.sign.ToString()); RecordKey = MD5Encrypt.Md5(strSql.ToString()); } return(RecordKey); } catch (Exception ex) { return(""); } }
/// <summary> /// 根据消费金额和余额获取消费的赠送金额 /// </summary> /// <param name="money"></param> /// <param name="balance"></param> /// <returns></returns> public Tz_Hy_Je GetMonenyCost(decimal money, Tz_Hy_Je balance) { try { Tz_Hy_Je jeModel = new Tz_Hy_Je(); jeModel.je_qm = money; jeModel.je_qm_zs = 0; if (balance != null) { if (balance.je_qm > 0 && balance.je_qm_zs <= 0) { jeModel.je_qm = money; jeModel.je_qm_zs = 0; } else if (balance.je_qm <= 0 && balance.je_qm_zs > 0) { jeModel.je_qm = 0; jeModel.je_qm_zs = money; } else if (balance.je_qm > 0 && balance.je_qm_zs > 0) { jeModel.je_qm = Frame.Common.OperationHelper.Digit(((money / (balance.je_qm + balance.je_qm_zs)) * balance.je_qm), 2); jeModel.je_qm_zs = money - jeModel.je_qm; } } return(jeModel); } catch (Exception ex) { Tz_Hy_Je jeModel = new Tz_Hy_Je(); jeModel.je_qm = money; jeModel.je_qm_zs = 0; return(jeModel); } }
public BaseResult AddWork(dynamic entity) { #region 获取数据 Hashtable param = (Hashtable)entity; BaseResult br = new BaseResult(); Hashtable ht = new Hashtable(); #endregion #region 验证参数 if (!param.ContainsKey("Type") || string.IsNullOrEmpty(param["Type"].ToString())) { br.Success = false; br.Message.Add("业务数据异常!"); return(br); } if (decimal.Parse(param["je"].ToString()) == 0) { br.Success = false; br.Message.Add("操作金额不能等于0!"); return(br); } #endregion if (param["Type"].ToString() == "CZ") { #region 充值 #region 检查会员是否有效 br = base.CheckHY(param); if (!br.Success) { throw new CySoftException(br); } Tb_Hy_Detail hy_detail = (Tb_Hy_Detail)br.Data; #endregion #region 检查是否符合充值设置的金额 if (decimal.Parse(param["je"].ToString()) > 0) { br = this.CheckCZMaxMoney(param); if (!br.Success) { throw new CySoftException(br); } } #endregion #region 获取赠送金额等信息 hy_detail.Tb_Hy_Shop.id_shop = param["id_shop"].ToString(); var czrule_zssp_list = new List <Tb_Hy_Czrule_Zssp_Api_Query>(); decimal je_zs = GetJe_Qm_Zs(hy_detail, decimal.Parse(param["je"].ToString()), ref czrule_zssp_list); string dh = GetNewDH(param["id_masteruser"].ToString(), param["id_shop"].ToString(), Enums.FlagDJLX.DHCZ); string dh_pay = ""; if (param.ContainsKey("dh") && !string.IsNullOrEmpty(param["dh"].ToString())) { dh = param["dh"].ToString(); } if (param.ContainsKey("dh_pay") && !string.IsNullOrEmpty(param["dh_pay"].ToString())) { dh_pay = param["dh_pay"].ToString(); } string id = GetGuid; decimal je_ye = 0; #endregion #region 更新/插入Tz_Hy_Je表 ht.Clear(); ht.Add("id_shop", param["id_shop"].ToString()); ht.Add("id_masteruser", param["id_masteruser"].ToString()); ht.Add("id_hy", param["id_hy"].ToString()); var brJe = this.GetOne(ht); if (!brJe.Success) { throw new CySoftException(br); } else { var dbJeModel = (Tz_Hy_Je)brJe.Data; if (dbJeModel != null && !string.IsNullOrEmpty(dbJeModel.id)) { #region 更新数据 if (RecordKeyError(dbJeModel)) { br.Success = false; br.Message.Clear(); br.Message.Add("会员金额数据非法!"); throw new CySoftException(br); } else if (ZBError(dbJeModel)) { br.Success = false; br.Message.Clear(); br.Message.Add("会员金额账本数据非法!"); throw new CySoftException(br); } else { if (decimal.Parse(param["je"].ToString()) < 0 && (dbJeModel.je_qm + decimal.Parse(param["je"].ToString())) < 0) { br.Success = false; br.Message.Clear(); br.Message.Add(String.Format("操作失败,账户余额(" + dbJeModel.je_qm + ") + 充值金额(" + decimal.Parse(param["je"].ToString()) + ") 不能为负!")); throw new CySoftException(br); } je_ye = (decimal)dbJeModel.je_qm + (decimal)dbJeModel.je_qm_zs; dbJeModel.je_qm = dbJeModel.je_qm + decimal.Parse(param["je"].ToString()); dbJeModel.je_qm_zs = dbJeModel.je_qm_zs + je_zs; dbJeModel.recodekey = GetRecordKey(dbJeModel); DAL.Update(dbJeModel); } #endregion } else { #region 插入数据 Tz_Hy_Je addModel = new Tz_Hy_Je() { id_masteruser = param["id_masteruser"].ToString(), id = GetGuid, id_hy = param["id_hy"].ToString(), je_qm = 0, je_qm_zs = 0 }; if (ZBError(addModel)) { br.Success = false; br.Message.Clear(); br.Message.Add("会员金额账本数据非法!"); throw new CySoftException(br); } if (decimal.Parse(param["je"].ToString()) < 0) { br.Success = false; br.Message.Clear(); br.Message.Add(String.Format("操作失败,账户余额(0) + 充值金额(" + decimal.Parse(param["je"].ToString()) + ") 不能为负!")); throw new CySoftException(br); } addModel.je_qm = decimal.Parse(param["je"].ToString()); addModel.je_qm_zs = je_zs; addModel.recodekey = GetRecordKey(addModel); DAL.Add(addModel); #endregion } } #endregion #region 插入流水表 ht.Clear(); ht.Add("id", GetGuid); ht.Add("id_masteruser", param["id_masteruser"].ToString()); ht.Add("id_bill", id); ht.Add("bm_djlx", "HY020"); ht.Add("rq", DateTime.Parse(param["rq"].ToString())); ht.Add("id_shop", param["id_shop"].ToString()); ht.Add("id_hy", param["id_hy"].ToString()); ht.Add("je", decimal.Parse(param["je"].ToString())); ht.Add("je_zs", je_zs); ht.Add("bz", param["bz"].ToString()); var addFlowNum = Tz_Hy_Je_FlowDAL.AddFlowWithExists(typeof(Tz_Hy_Je_Flow), ht); if (addFlowNum == 0) { br.Success = false; br.Message.Clear(); br.Message.Add(string.Format("充值单据已存在!")); throw new CySoftException(br); } #endregion #region 插入表头 ht.Clear(); ht.Add("id_masteruser", param["id_masteruser"].ToString()); ht.Add("id", id); ht.Add("dh", dh); ht.Add("rq", DateTime.Parse(param["rq"].ToString())); ht.Add("id_shop", param["id_shop"].ToString()); ht.Add("bm_djlx", "HY020"); ht.Add("id_pay", param["id_pay"].ToString()); ht.Add("id_jbr", param["id_create"].ToString()); ht.Add("je_mxtotal", decimal.Parse(param["je"].ToString())); ht.Add("flag_sh", (byte)Enums.FlagSh.HadSh); ht.Add("flag_cancel", (byte)Enums.FlagCancel.NoCancel); ht.Add("bz", param["bz"].ToString()); ht.Add("id_create", param["id_create"].ToString()); ht.Add("flag_delete", (byte)Enums.FlagDelete.NoDelete); ht.Add("id_user_sh", param["id_create"].ToString()); ht.Add("rq_sh", DateTime.Parse(param["rq"].ToString())); ht.Add("dh_pay", dh_pay); var addHeadNum = Td_Hy_Cz_1DAL.AddWithExists(typeof(Td_Hy_Cz_1), ht); if (addHeadNum == 0) { br.Success = false; br.Message.Clear(); br.Message.Add(string.Format("充值单据已存在!")); throw new CySoftException(br); } #endregion #region 插入表体 var td_Hy_Cz_2 = new Td_Hy_Cz_2() { id_masteruser = param["id_masteruser"].ToString(), id = GetGuid, id_hy = param["id_hy"].ToString(), id_bill = id, sort_id = 1, je = decimal.Parse(param["je"].ToString()), je_zs = je_zs, bz = "接口充值", rq_create = DateTime.Now, je_ye = je_ye }; DAL.Add(td_Hy_Cz_2); #endregion #region 读取充值后金额 并验证 ht.Clear(); ht.Add("id_shop", param["id_shop"].ToString()); ht.Add("id_masteruser", param["id_masteruser"].ToString()); ht.Add("id_hy", param["id_hy"].ToString()); var brJeNow = this.Get(ht); if (!brJeNow.Success) { br.Success = false; br.Message.Add(string.Format("操作失败 查询金额失败 !")); throw new CySoftException(br); } else { //回写 Td_Hy_Cz_2 的 je_ye 为当前的余额 目前取消掉了 var dbJeModel = (Tz_Hy_Je)brJeNow.Data; //ht.Clear(); //ht.Add("id", td_Hy_Cz_2.id); //ht.Add("id_masteruser", param["id_masteruser"].ToString()); //ht.Add("new_je_ye", dbJeModel.je_qm); //DAL.UpdatePart(typeof(Td_Hy_Cz_2), ht); if (ZBError(dbJeModel)) { br.Success = false; br.Message.Clear(); br.Message.Add("操作失败 会员金额账本数据非法 !"); throw new CySoftException(br); } } #endregion #region 返回 var jeNowModel = (Tz_Hy_Je)brJeNow.Data; br.Message.Add(String.Format("操作成功!")); br.Success = true; br.Data = new { add_je = decimal.Parse(param["je"].ToString()), add_je_zs = je_zs, je_qm = jeNowModel.je_qm, je_qm_zs = jeNowModel.je_qm_zs, id = id, czrule_zssp = czrule_zssp_list }; return(br); #endregion #endregion } else if (param["Type"].ToString() == "XF") { #region 消费 #region 检查会员是否有效 br = base.CheckHY(param); if (!br.Success) { throw new CySoftException(br); } var tempModel = new Tz_Hy_Je() { je_qm = decimal.Parse(param["je"].ToString()), je_qm_zs = 0 }; #endregion #region 验证会员金额是否非法 并更新账户以及插入流水帐 ht.Clear(); ht.Add("id_shop", param["id_shop"].ToString()); ht.Add("id_masteruser", param["id_masteruser"].ToString()); ht.Add("id_hy", param["id_hy"].ToString()); var brJe = this.GetOne(ht); if (!brJe.Success) { throw new CySoftException(br); } else { var dbJeModel = (Tz_Hy_Je)brJe.Data; if (dbJeModel != null && !string.IsNullOrEmpty(dbJeModel.id)) { #region 验证会员金额数据是否非法 if (RecordKeyError(dbJeModel)) { br.Success = false; br.Message.Clear(); br.Message.Add("会员金额数据非法!"); throw new CySoftException(br); } #endregion #region 验证会员金额账本数据是否非法 if (ZBError(dbJeModel)) { br.Success = false; br.Message.Clear(); br.Message.Add("会员金额账本数据非法!"); throw new CySoftException(br); } #endregion #region 插入流水表 tempModel = GetMonenyCost(decimal.Parse(param["je"].ToString()), dbJeModel); ht.Clear(); ht.Add("id", GetGuid); ht.Add("id_masteruser", param["id_masteruser"].ToString()); ht.Add("id_bill", param["id_bill"].ToString()); ht.Add("bm_djlx", param["bm_djlx"].ToString()); ht.Add("rq", DateTime.Parse(param["rq"].ToString())); ht.Add("id_shop", param["id_shop"].ToString()); ht.Add("id_hy", param["id_hy"].ToString()); ht.Add("je", tempModel.je_qm * (-1)); ht.Add("je_zs", tempModel.je_qm_zs * (-1)); ht.Add("bz", param["bz"].ToString()); var addFlowNum = Tz_Hy_Je_FlowDAL.AddFlowForXFWithExists(typeof(Tz_Hy_Je_Flow), ht); if (addFlowNum == 0) { br.Success = false; br.Message.Clear(); br.Message.Add("消费单据已存在!"); throw new CySoftException(br); } #endregion #region 验证账户余额是否充足 if (dbJeModel.je_qm < tempModel.je_qm) { br.Success = false; br.Message.Clear(); br.Message.Add(String.Format("操作失败,正常账户余额(" + dbJeModel.je_qm + ") < 正常消费金额(" + tempModel.je_qm + ")!")); throw new CySoftException(br); } if (dbJeModel.je_qm_zs < tempModel.je_qm_zs) { br.Success = false; br.Message.Clear(); br.Message.Add(String.Format("操作失败,赠送账户余额(" + dbJeModel.je_qm_zs + ") < 赠送消费金额(" + tempModel.je_qm_zs + ")!")); throw new CySoftException(br); } #endregion #region 更新数据 dbJeModel.je_qm = dbJeModel.je_qm - tempModel.je_qm; dbJeModel.je_qm_zs = dbJeModel.je_qm_zs - tempModel.je_qm_zs; dbJeModel.recodekey = GetRecordKey(dbJeModel); DAL.Update(dbJeModel); #endregion } else { #region 操作失败 会员还未充值过 br.Success = false; br.Message.Clear(); br.Message.Add(string.Format("操作失败 会员还未充值过!")); throw new CySoftException(br); #endregion } } #endregion #region 查询消费后会员的金额 ht.Clear(); ht.Add("id_shop", param["id_shop"].ToString()); ht.Add("id_masteruser", param["id_masteruser"].ToString()); ht.Add("id_hy", param["id_hy"].ToString()); var brJeNow = this.Get(ht); if (!brJeNow.Success) { br.Success = false; br.Message.Clear(); br.Message.Add(string.Format("操作完毕 查询金额失败!")); throw new CySoftException(br); } else { var dbJeModel = (Tz_Hy_Je)brJeNow.Data; if (ZBError(dbJeModel)) { br.Success = false; br.Message.Clear(); br.Message.Add("操作失败 会员金额账本数据非法 !"); throw new CySoftException(br); } } #endregion #region 返回 var jeNowModel = (Tz_Hy_Je)brJeNow.Data; br.Message.Add(String.Format("操作成功!")); br.Success = true; br.Data = new { add_je = tempModel.je_qm, add_je_zs = tempModel.je_qm_zs, je_qm = jeNowModel.je_qm, je_qm_zs = jeNowModel.je_qm_zs }; return(br); #endregion #endregion } else { #region 返回 br.Message.Add(String.Format("操作失败,无此操作类型!")); br.Success = false; return(br); #endregion } }