using Edu.Common.Enum;
using Edu.Model.Entity.Web;
using Edu.Model.ViewModel.Web;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using VT.FW.DB.Dapper;

namespace Edu.Repository.Web
{
    /// <summary>
    /// 招聘岗位申请仓储层
    /// </summary>
    public class RB_Web_JobApplyRepository : BaseRepository<RB_Web_JobApply>
    {
        /// <summary>
        /// 获取招聘申请分页列表
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowsCount"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List<RB_Web_JobApply_ViewModel> GetWebJobApplyPageRepository(int pageIndex, int pageSize, out long rowsCount, RB_Web_JobApply_ViewModel query)
        {
            rowsCount = 0;
            var parameters = new DynamicParameters();
            StringBuilder builder = new StringBuilder();
            builder.Append(@"
SELECT A.*,IFNULL(B.PositionName,'') AS  PositionName
FROM RB_Web_JobApply AS A LEFT JOIN rb_web_recruitment AS B ON A.RecruitmentId=B.Id
WHERE 1=1
");
            builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Web_JobApply_ViewModel.Status), (int)DateStateEnum.Normal);
            if (query == null)
            {
                return new List<RB_Web_JobApply_ViewModel>();
            }
            else
            {
                if (query.Group_Id > 0)
                {
                    builder.AppendFormat(@" AND A.{0}={1} ", nameof(RB_Web_JobApply_ViewModel.Group_Id), query.Group_Id);
                }
                if (!string.IsNullOrEmpty(query.LinkMan))
                {
                    builder.AppendFormat(" AND A.{0} LIKE @LinkMan ", nameof(RB_Web_JobApply_ViewModel.LinkMan));
                    parameters.Add("LinkMan", "%" + query.LinkMan.Trim() + "%");
                }
                if (!string.IsNullOrEmpty(query.PositionName))
                {
                    builder.AppendFormat(" AND B.{0} LIKE @PositionName ", nameof(RB_Web_JobApply_ViewModel.PositionName));
                    parameters.Add("PositionName", "%" + query.PositionName.Trim() + "%");
                }
            }

            builder.AppendFormat(" ORDER BY A.{0} DESC ", nameof(RB_Web_JobApply_ViewModel.Id));
            return GetPage<RB_Web_JobApply_ViewModel>(pageIndex, pageSize, out rowsCount, builder.ToString(), parameters).ToList();
        }
    }
}
