示例#1
0
        /// <summary>
        /// 提交模板数据
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        private string PostWXTempmsg(HttpContext context)
        {
            int autoId = Convert.ToInt32(context.Request["AutoId"]);
            KeyVauleDataInfo keyValue = new KeyVauleDataInfo();

            if (autoId > 0)
            {
                keyValue = bllKeyValueData.GetKeyData(autoId);
                if (keyValue == null)
                {
                    resp.errmsg = "原模板没有找到";
                    return(Common.JSONHelper.ObjectToJson(resp));
                }
                if (keyValue.WebsiteOwner != bllKeyValueData.WebsiteOwner)
                {
                    resp.errmsg = "原模板不是本站模板";
                    return(Common.JSONHelper.ObjectToJson(resp));
                }
            }
            else
            {
                keyValue.Creater      = currentUserInfo.UserID;
                keyValue.WebsiteOwner = bllKeyValueData.WebsiteOwner;
                keyValue.CreateTime   = DateTime.Now;
                keyValue.PreKey       = "0";
                keyValue.DataType     = EnumStringHelper.ToString(KeyVauleDataType.WXTmplmsg);
            }

            string oldKey = keyValue.DataKey;

            keyValue = bllKeyValueData.ConvertRequestToModel <KeyVauleDataInfo>(keyValue);
            //微信模板Id变化则清除以前的字段数据
            if (!string.IsNullOrWhiteSpace(oldKey) && oldKey != keyValue.DataKey)
            {
                bllKeyValueData.DeleteDataVaule(EnumStringHelper.ToString(KeyVauleDataType.WXTmplmsgData), null, oldKey, bllKeyValueData.WebsiteOwner);
            }

            string keyFieldsJson = context.Request["KeyFields"];
            List <KeyVauleDataInfo> newFieldList = Common.JSONHelper.JsonToModel <List <KeyVauleDataInfo> >(keyFieldsJson);//jSON 反序列化

            for (int i = 0; i < newFieldList.Count; i++)
            {
                newFieldList[i].DataType = EnumStringHelper.ToString(KeyVauleDataType.WXTmplmsgData);
            }
            List <KeyVauleDataInfo> oldFieldList = bllKeyValueData.GetKeyVauleDataInfoList(EnumStringHelper.ToString(KeyVauleDataType.WXTmplmsgData), keyValue.DataKey
                                                                                           , bllKeyValueData.WebsiteOwner);


            List <KeyVauleDataInfo> deleteFieldList = new List <KeyVauleDataInfo>();
            List <KeyVauleDataInfo> editFieldList   = new List <KeyVauleDataInfo>();
            List <KeyVauleDataInfo> addFieldList    = new List <KeyVauleDataInfo>();

            foreach (KeyVauleDataInfo item in oldFieldList)
            {
                KeyVauleDataInfo temp = newFieldList.FirstOrDefault(p => p.DataType == item.DataType && p.DataKey == item.DataKey);
                if (temp == null)
                {
                    deleteFieldList.Add(item);
                }
                else
                {
                    item.DataValue = temp.DataValue;
                    item.OrderBy   = temp.OrderBy;
                    editFieldList.Add(item);
                }
            }

            foreach (KeyVauleDataInfo item in newFieldList)
            {
                if (!oldFieldList.Exists(p => p.DataType == item.DataType && p.DataKey == item.DataKey))
                {
                    item.Creater      = currentUserInfo.UserID;
                    item.WebsiteOwner = bllKeyValueData.WebsiteOwner;
                    item.CreateTime   = DateTime.Now;
                    item.PreKey       = keyValue.DataKey;
                    addFieldList.Add(item);
                }
            }

            if (deleteFieldList.Count > 0)
            {
                string delIds = Common.MyStringHelper.ListToStr(deleteFieldList.Select(p => p.AutoId).ToList(), "", ",");
                bllKeyValueData.DeleteDataVaule(delIds);
            }

            BLLTransaction tran = new BLLTransaction();//事务

            try
            {
                if (keyValue.AutoId == 0)
                {
                    if (!bllKeyValueData.Add(keyValue, tran))
                    {
                        resp.errmsg = "添加模板失败";
                        tran.Rollback();
                        return(Common.JSONHelper.ObjectToJson(resp));
                    }
                }
                else
                {
                    if (!bllKeyValueData.Update(keyValue, tran))
                    {
                        resp.errmsg = "修改模板失败";
                        tran.Rollback();
                        return(Common.JSONHelper.ObjectToJson(resp));
                    }
                }

                foreach (KeyVauleDataInfo item in editFieldList)//添加问题表
                {
                    if (!bllKeyValueData.Update(item, tran))
                    {
                        resp.errmsg = "模板字段修改失败";
                        tran.Rollback();
                        return(Common.JSONHelper.ObjectToJson(resp));
                    }
                }
                foreach (KeyVauleDataInfo item in addFieldList)//添加问题表
                {
                    if (!bllKeyValueData.Add(item, tran))
                    {
                        resp.errmsg = "模板字段添加失败";
                        tran.Rollback();
                        return(Common.JSONHelper.ObjectToJson(resp));
                    }
                }
                tran.Commit();
                resp.isSuccess = true;
            }
            catch (Exception ex)
            {
                resp.errcode = (int)APIErrCode.OperateFail;
                resp.errmsg  = "提交失败," + ex.Message;
                tran.Rollback();
            }
            return(Common.JSONHelper.ObjectToJson(resp));
        }