using NFine.Code;
using NFine.Domain.Entity.SystemManage;
using NFine.Domain.IRepository.SystemManage;
using NFine.Repository.SystemManage;
using System;
using System.Collections.Generic;
using System.Linq;

namespace NFine.Application.SystemManage
{
    public class CustomerApp
    {
        private ICustomerRepository service = new CustomerRepository();

        public List<CustomerEntity> GetList()
        {
            return service.IQueryable().OrderBy(t => t.F_CreatorTime).ToList();
        }

        public List<CustomerEntity> GetList(Pagination pagination, string keyword)
        {
            var expression = ExtLinq.True<CustomerEntity>();
            if (!string.IsNullOrEmpty(keyword))
            {
                expression = expression.And(t => t.F_CusCode.Contains(keyword));
                expression = expression.Or(t => t.F_CusName.Contains(keyword));
            }
            expression = expression.And(t => t.F_CusCode != "admin");
            return service.FindList(expression, pagination);
        }

        public List<CustomerEntity> GetListCus()
        {
            var expression = ExtLinq.True<CustomerEntity>();
            return service.IQueryable(expression).OrderBy(t => t.F_CusCode).ToList();
        }

        public CustomerEntity GetForm(string keyValue)
        {
            return service.FindEntity(keyValue);
        }

        public void DeleteForm(string keyValue)
        {
            service.DeleteForm(keyValue);
        }

        public void SubmitForm(CustomerEntity cusEntity, string keyValue)
        {
            if (!string.IsNullOrEmpty(keyValue))
            {
                cusEntity.Modify(keyValue);
            }
            else
            {
                cusEntity.Create();
            }
            service.SubmitForm(cusEntity, keyValue);
        }

        public void UpdateForm(CustomerEntity cusEntity)
        {
            service.Update(cusEntity);
        }
    }
}