using Edu.Model.Entity.Duty;
using Edu.Model.ViewModel.Duty;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Edu.Repository.Duty
{
    /// <summary>
    /// 值班内容仓储层
    /// </summary>
    public class RB_Duty_ContentRepository : BaseRepository<RB_Duty_Content>
    {
        /// <summary>
        /// 获取值班内容列表
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Duty_Content_ViewModel> GetDutyContentListRepository(RB_Duty_Content_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
SELECT A.* ,IFNULL(B.ItemName,'') AS ItemName
FROM RB_Duty_Content AS A LEFT JOIN rb_duty_item AS B ON A.ItemId=B.Id
WHERE 1=1
");
            if (query == null)
            {
                return new List<RB_Duty_Content_ViewModel>();
            }
            else
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Duty_Content_ViewModel.Group_Id), query.Group_Id);
                }
                if (query.PlanId > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Duty_Content_ViewModel.PlanId), query.PlanId);
                }
                if (!string.IsNullOrEmpty(query.QPlanIds))
                {
                    builder.AppendFormat(@" AND A.{0} IN({1}) ", nameof(RB_Duty_Content_ViewModel.PlanId), query.QPlanIds);
                }
                if (query.PlanShift > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Duty_Content_ViewModel.PlanShift), query.PlanShift);
                }
                if (query.PlanType > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Duty_Content_ViewModel.PlanType), query.PlanType);
                }
                if (!string.IsNullOrEmpty(query.HandoverDlandetails))
                {
                    builder.AppendFormat(@" AND FIND_IN_SET('{0}', A.HandoverDlandetails)>0 ", query.HandoverDlandetails);
                }
                if (query.CreateBy > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Duty_Content_ViewModel.CreateBy), query.CreateBy);
                }
            }
            return Get<RB_Duty_Content_ViewModel>(builder.ToString()).ToList();
        }

        /// <summary>
        /// 交接班次
        /// </summary>
        /// <param name="HandoverDlandetails"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public bool UpdateHandoverShiftRepository(string HandoverDlandetails, RB_Duty_Content_ViewModel query)
        {
            StringBuilder builder = new StringBuilder();
            builder.AppendFormat(@"
UPDATE RB_Duty_Content 
SET HandoverDlandetails='{0}' 
", HandoverDlandetails);
            builder.AppendFormat(@" WHERE PlanId={0} AND PlanType=2 AND PlanShift={1} AND CreateBy={2} ", query.PlanId, query.PlanShift, query.CreateBy);

            return base.Execute(builder.ToString()) > 0;
        }
    }
}
