コード例 #1
0
ファイル: DictService.cs プロジェクト: radtek/work-1
        public OptResult Add(Dict dict)
        {
            OptResult rst = null;
            //1、编号类型是否存在
            var count = _dictTypeRep.Count(Predicates.Field <DictType>(t => t.type_code, Operator.Eq, dict.dict_type));

            if (count < 1)
            {
                rst = OptResult.Build(ResultCode.DataNotFound, string.Format("{0},编号类型{1}不存在!", Msg_Add, dict.dict_type));
                return(rst);
            }
            //2、相同类型下,编号是否已存在
            PredicateGroup pg = new PredicateGroup {
                Operator = GroupOperator.And, Predicates = new List <IPredicate>()
            };

            pg.Predicates.Add(Predicates.Field <Dict>(d => d.dict_type, Operator.Eq, dict.dict_type));
            pg.Predicates.Add(Predicates.Field <Dict>(d => d.dict_code, Operator.Eq, dict.dict_code));
            count = _dictRep.Count(pg);
            if (count > 0)
            {
                rst = OptResult.Build(ResultCode.DataRepeat, string.Format("{0},类型{1}下已存在编号{2}!", Msg_Add, dict.dict_type, dict.dict_code));
                return(rst);
            }
            //3、相同类型下,只能有一个默认值
            if (dict.dict_default == true)
            {
                pg.Predicates.Clear();
                pg.Predicates.Add(Predicates.Field <Dict>(d => d.dict_type, Operator.Eq, dict.dict_type));
                pg.Predicates.Add(Predicates.Field <Dict>(d => d.dict_default, Operator.Eq, true));

                count = _dictRep.Count(pg);
                if (count > 0)
                {
                    rst = OptResult.Build(ResultCode.DataRepeat, string.Format("{0},类型{1}下已存在默认值!", Msg_Add, dict.dict_type));
                    return(rst);
                }
            }

            //4、新增
            dict.dict_id = GuidExtension.GetOne();
            try
            {
                var val = _dictRep.Insert(dict);

                rst = OptResult.Build(ResultCode.Success, Msg_Add);
            }
            catch (Exception ex)
            {
                LogHelper.LogError(Msg_Add, ex);
                rst = OptResult.Build(ResultCode.DbError, Msg_Add);
            }
            return(rst);
        }
コード例 #2
0
        public OptResult Delete(string typeCode)
        {
            OptResult rst = null;
            //1、是否存在
            var predicate = Predicates.Field <DictType>(t => t.type_code, Operator.Eq, typeCode);
            var type      = _dictTypeRep.GetList(predicate).FirstOrDefault();

            if (type == null)
            {
                rst = OptResult.Build(ResultCode.DataNotFound, string.Format("{0},编号为{1}的字典类型不存在!", Msg_Delete, typeCode));
                return(rst);
            }
            //2、是否系统预制
            if (type.type_system)
            {
                rst = OptResult.Build(ResultCode.DataSystem, string.Format("{0},编号{1}为系统预制类型!", Msg_Delete, typeCode));
                return(rst);
            }
            //3、是否已被引用
            var count = _dictRep.Count(Predicates.Field <Dict>(t => t.dict_type, Operator.Eq, typeCode));

            if (count > 0)
            {
                rst = OptResult.Build(ResultCode.DataInUse, string.Format("{0},编号为{1}的字典类型已被引用!", Msg_Delete, typeCode));
                return(rst);
            }
            //3、删除
            try
            {
                bool val = _dictTypeRep.Delete(predicate);

                rst = OptResult.Build(val ? ResultCode.Success : ResultCode.Fail, Msg_Delete);
            }
            catch (Exception ex)
            {
                LogHelper.LogError(Msg_Delete, ex);
                rst = OptResult.Build(ResultCode.DbError, Msg_Delete);
            }
            return(rst);
        }