using AutoMapper;
using OASystem.Domain.Entities.Groups;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OASystem.Infrastructure.Repositories.Groups
{
    public class GroupCostParameterRepository : BaseRepository<Grp_GroupCostParameter, Grp_GroupCostParameterView>
    {
        public GroupCostParameterRepository(SqlSugarClient sqlSugar) :
        base(sqlSugar)
        {
        }

        public List<Grp_GroupCostParameter> GetGroupCostParameterListByDiid(int diid)
        {
           return Query(x=>x.DiId == diid).ToList();
        }

        public Grp_GroupCostParameter GetGroupCostParameterMainByDiid(int diid)
        {
            return Query(x => x.DiId == diid).First(x => x.CostType == "A");
        }

        public async Task<bool> UpdateIsShareById(int id,int IsShare)
        {
            return await UpdateAsync(x => x.Id == id, x=>new Grp_GroupCostParameter { IsShare = IsShare });
        }

        public async Task<bool> DeleteGroupCostParameterListByDiid(int diid) 
        {
            return await SoftDeleteAsync(x=>x.DiId == diid);
        }

        public async Task<bool> SaveAsync(List<Grp_GroupCostParameter> List,int Userid, int diid)
        {
            bool isTrue = false;
            try
            {
                BeginTran();
                isTrue = await DeleteGroupCostParameterListByDiid(diid);
                if (List.Count > 0)
                {
                    List.ForEach(x => { x.CreateUserId = Userid; x.CreateTime = DateTime.Now; });
                    isTrue = Adds(List) > 0;
                }
                CommitTran();
            }
            catch (Exception ex)
            {
                RollbackTran();
                isTrue = false;
            }
            return isTrue;
        }
    }
}
