#region Copyright

/*
 * Copyright 2005-2008 the Seasar Foundation and the Others.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

#endregion

using System;
using Seasar.Quill.Attrs;
using S2FormExample.Logics.Dao;
using S2FormExample.Logics.Dto;
using S2FormExample.Logics.Page;

namespace S2FormExample.Logics.Service.Impl
{
    /// <summary>
    /// 幮堳搊榐梡僒乕價僗梡幚憰僋儔僗
    /// </summary>
    public class EmployeeEditServiceImpl : BaseServiceImpl, IEmployeeEditService
    {
        /// <summary>
        /// 幮堳DAO
        /// </summary>
        protected IEmployeeDao dao;

        /// <summary>
        /// 僐儞僗僩儔僋僞
        /// </summary>
        public EmployeeEditServiceImpl()
        {
            ;
        }

        #region IEmployeeEditService Members

        /// <summary>
        /// 幮堳僨乕僞傪庢摼偡傞
        /// </summary>
        /// <param name="id">幮堳ID</param>
        /// <returns>幮堳僨乕僞</returns>
        public EmployeeEditPage GetData(int id)
        {
            EmployeeEditPage page = new EmployeeEditPage();

            EmployeeDto dto = dao.GetData(id);
            if (dto != null)
            {
                page.Code = dto.Code;
                page.Depart = dto.DeptNo;
                page.Entry = dto.EntryDay;
                page.Gender = dto.Gender;
                page.Id = dto.Id;
                page.Name = dto.Name;
            }
            else
            {
                page = null;
            }
            return page;
        }

        /// <summary>
        /// 幮堳僨乕僞傪搊榐偡傞
        /// </summary>
        /// <param name="data">搊榐幮堳僨乕僞</param>
        /// <returns>搊榐審悢</returns>
        [Transaction]
        public virtual int ExecUpdate(EmployeeEditPage data)
        {
            if (data == null)
                throw new ArgumentNullException("data");

            EmployeeDto dto = new EmployeeDto();
            dto.Code = data.Code;
            dto.DeptNo = data.Depart;
            dto.EntryDay = data.Entry;
            dto.Gender = data.Gender;
            dto.Id = data.Id;
            dto.Name = data.Name;

            if (data.Id.HasValue)
            {
                EmployeeDto e1 = dao.GetData(data.Id.Value);
                if (e1 != null)
                    return (dao.UpdateData(dto));
                else
                    return (dao.InsertData(dto));
            }
            else
            {
                return (dao.InsertData(dto));
            }
        }

        /// <summary>
        /// 幮堳僨乕僞傪嶍彍偡傞
        /// </summary>
        /// <param name="id">嶍彍幮堳ID</param>
        /// <returns>嶍彍審悢</returns>
        [Transaction]
        public virtual int ExecDelete(int id)
        {
            EmployeeDto data = new EmployeeDto();
            data.Id = id;

            return (dao.DeleteData(data));
        }

        #endregion
    }
}