using LinqToDB;
using MasaBlazorApp3.DataAccess.Dao;
using MasaBlazorApp3.Pages;
using MasaBlazorApp3.Pojo;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MasaBlazorApp3.DataAccess.Impl
{
    public class DrugManuNoDao : IDrugManuNoDao
    {
        private readonly AppDataConnection _connection;
        public DrugManuNoDao(AppDataConnection connection)
        {
            _connection = connection;
        }
        public int AddDrugManuNo(DrugManuNo drugManuNo)
        {
            if(drugManuNo!=null&& drugManuNo.EffDate!=null)
            {
                drugManuNo.EffDate= drugManuNo.EffDate.ToString().Length>10 ?new DateTime(drugManuNo.EffDate.Value.Year, drugManuNo.EffDate.Value.Month, drugManuNo.EffDate.Value.Day) : drugManuNo.EffDate;
            }
            return _connection.InsertWithInt32Identity(drugManuNo);
        }

        public bool DeleteDrugManuNo(string id)
        {
            if (!string.IsNullOrEmpty(id))
            {
                int manuNo= _connection.DrugManuNo.Where(dm => dm.Id == id).Count();
                if (manuNo > 0)
                {
                    return _connection.DrugManuNo.Where(dm => dm.Id == id).Delete() > 0;
                }
                else return true;
            }
            else
            {
                return true;
            }
        }

        public bool UpdateDrugManuNo(DrugManuNo drugManuNo)
        {
            var statement = _connection.DrugManuNo
                .Where(dm => dm.Id == drugManuNo.Id)
                .Set(dm => dm.ManuNo, drugManuNo.ManuNo)
                .Set(dm => dm.EffDate, drugManuNo.EffDate);
            return statement.Update() > 0;
        }
    }
}
