码迷,mamicode.com
首页 > 其他好文 > 详细

DataContext的CRUD处理

时间:2015-02-25 06:58:44      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:

CRUD

技术分享
 public class UpdateCommitteeBll : BllBaseEx
    {
        /// <summary>
        /// 要求キー-委員会
        /// </summary>
        public const string REQUEST_KEY_COMMITTEE = "DB_COMMITTEE_REGISTER_COMMITTEE";

        /// <summary>
        /// 要求キー-出席者
        /// </summary>
        public const string REQUEST_KEY_ATTENDEE = "DB_COMMITTEE_REGISTER_ATTENNDEE";

        /// <summary>
        /// 返却キー-委員会No
        /// </summary>
        public const string RESPONSE_KEY_COMMITTEE_NO = "DB_COMMITTEE_REGISTER_COMMITTEE_NO";

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        protected override Dictionary<string, object> Perform()
        {
            DateTime now = DateTime.Now;

            TBAAMS_COMMITTEE committee = Request[REQUEST_KEY_COMMITTEE] as TBAAMS_COMMITTEE;
            List<RESULT_ATTENDEE> attendee = Request[REQUEST_KEY_ATTENDEE] as List<RESULT_ATTENDEE>;
            
            TBAAMS_COMMITTEE result = (from row in DataContext.TBAAMS_COMMITTEE
                         where row.COMMITTEE_NO == committee.COMMITTEE_NO
                         select row).FirstOrDefault();

            //委員会
            if (result == null)
            {
                //登録処理
                committee.COMMITTEE_NO = ApplicationBllUtility.GenerateCommitteeNo(DataContext);
                committee.HOLD_INFORM_FLG = 0;
                committee.PROGRAM_INFORM_FLG = 0;
                committee.DISCUSSION_RESULT_INFORM_FLG = 0;
                committee.DEL_FLG = 0;
                committee.INS_USERID = this.StaffInfo.StfCd;
                committee.INS_IP = this.StaffInfo.Ip;
                committee.INS_DATE = now;
                committee.UPD_USERID = this.StaffInfo.StfCd;
                committee.UPD_IP = this.StaffInfo.Ip;
                committee.UPD_DATE = now;

                DataContext.TBAAMS_COMMITTEE.InsertOnSubmit(committee);
            }
            else
            {
                //更新処理
                //排他処理チェック
                if (result.UPD_IP == committee.UPD_IP
                && result.UPD_USERID == committee.UPD_USERID
                && result.UPD_DATE == committee.UPD_DATE)
                {
                    result.HOLD_DATE = committee.HOLD_DATE;
                    result.HOLD_PLACE = committee.HOLD_PLACE;
                    result.START_TIME = committee.START_TIME;
                    result.END_TIME = committee.END_TIME;
                    result.REMARKS = committee.REMARKS;
                    result.UPD_USERID = this.StaffInfo.StfCd;
                    result.UPD_IP = this.StaffInfo.Ip;
                    result.UPD_DATE = now;
                }
                else
                {
                    //排他エラー
                    throw new Exception(Messages.ERROR013.Replace("{0}", "委員会"));
                }
            }

            //出席者(Delete-Insert)
            List<TBAARS_COMMITTEE_ATTENDEE> resDel = (from delRow in DataContext.TBAARS_COMMITTEE_ATTENDEE
                          where delRow.COMMITTEE_NO == committee.COMMITTEE_NO
                          select delRow).ToList ();

            DataContext.TBAARS_COMMITTEE_ATTENDEE.DeleteAllOnSubmit (resDel);

            foreach (RESULT_ATTENDEE insRow in attendee)
            {
                if (insRow.ATTENDEE_FLG .Equals (1))
                {
                    TBAARS_COMMITTEE_ATTENDEE insAttendee = new TBAARS_COMMITTEE_ATTENDEE();

                    insAttendee.COMMITTEE_NO = committee.COMMITTEE_NO;
                    insAttendee.STF_CODE = insRow.STF_CODE;
                    insAttendee.DEL_FLG = 0;
                    insAttendee.INS_USERID = this.StaffInfo.StfCd;
                    insAttendee.INS_IP = this.StaffInfo.Ip;
                    insAttendee.INS_DATE = now;
                    insAttendee.UPD_USERID = this.StaffInfo.StfCd;
                    insAttendee.UPD_IP = this.StaffInfo.Ip;
                    insAttendee.UPD_DATE = now;

                    DataContext.TBAARS_COMMITTEE_ATTENDEE.InsertOnSubmit(insAttendee);
                }
            }

            Dictionary<string, object> response = new Dictionary<string, object>();
            response.Add(RESPONSE_KEY_COMMITTEE_NO, committee.COMMITTEE_NO );
            return response;
        }

        protected override void Validate()
        {
            base.Validate();

            TBAAMS_COMMITTEE committee = Request[REQUEST_KEY_COMMITTEE] as TBAAMS_COMMITTEE;
            List<RESULT_ATTENDEE> attendee = Request[REQUEST_KEY_ATTENDEE] as List<RESULT_ATTENDEE>;

            MessageException ex = new MessageException();

            //開催日
            //必須チェック
            if (String.IsNullOrEmpty (committee.HOLD_DATE))
            {
                ex.Messages.Add(string.Format(Messages.ERROR001, "開催日"));
            }

            //開催場所
            //必須チェック
            if (String.IsNullOrEmpty(committee.HOLD_PLACE ))
            {
                ex.Messages.Add(string.Format(Messages.ERROR001, "開催場所"));
            }

            //時間
            //前後チェック
            if (!string.IsNullOrEmpty(committee.START_TIME) && !string.IsNullOrEmpty(committee.END_TIME))
            {
                if (!CheckUtility.CheckTimeBigAndSmall(
                    committee.START_TIME.Split (:)[0],
                    committee.START_TIME.Split (:)[1],
                    committee.END_TIME.Split (:)[0],
                    committee.END_TIME.Split (:)[1]))
                {
                    ex.Messages.Add("開始時間と終了時間の前後関係が正しくありません。");
                }
            }

            //出席者
            //委員の出席者に1人でも出席者がいるかをチェック
            var chk = (from row in attendee
                      where row.ATTENDEE_FLG == 1
                      && (row.ROLE_CODE == Role.Committee .Value 
                      || row.ROLE_CODE == Role.Chairperson .Value )
                      select row).ToList ();

            if(chk.Count == 0){
                ex.Messages.Add(string.Format(Messages.ERROR010.Replace (,), "出席委員が1人"));
            }

            if (ex.Messages.Count != 0)
            {
                throw ex;
            }
        }
    }
View Code

 

DataContext的CRUD处理

标签:

原文地址:http://www.cnblogs.com/haiy/p/4155675.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!