using BLL;
using System;
using System.Data.Linq.SqlClient;
using System.Linq;
using System.Web.Http;

namespace WebAPI.Controllers.HSSE
{
    public class RectifyController : ApiController
    {
        #region 获取问题类型
        public Model.ResponeData getProcessTypes(string RectifyName, int PageNumber, int PageSize)
        {
            var responeData = new Model.ResponeData();
            try
            {
                var query = (from x in Funs.DB.Technique_Rectify
                             where x.IsEndLever == true
                             select new
                             {
                                 x.RectifyId,
                                 x.RectifyName,
                             }).ToList();
                if (!string.IsNullOrEmpty(RectifyName))
                {
                    query = query.Where(x => x.RectifyName.Contains(RectifyName)).ToList();
                }

                var paginatedQuery = query
                    .Skip((PageNumber - 1) * PageSize)
                    .Take(PageSize)
                    .ToList();

                responeData.data = paginatedQuery;
            }
            catch (Exception ex)
            {
                responeData.code = 0;
                responeData.message = ex.Message;
            }

            return responeData;

        }
        #endregion

        #region 根据问题类型ID获取问题描述
        public Model.ResponeData getProblemDescs(string RectifyId, string keyword, int PageNumber, int PageSize)
        {
            var responeData = new Model.ResponeData();
            try
            {
                var query = (from x in Funs.DB.Technique_RectifyItem
                             where x.RectifyId == RectifyId
                             select new
                             {
                                 x.RectifyId,
                                 x.HazardSourcePoint,
                                 x.RiskAnalysis,
                                 x.RiskPrevention
                             }).ToList();
                if (!string.IsNullOrEmpty(keyword))
                {
                    query = query.Where(x => x.HazardSourcePoint.Contains(keyword)).ToList();
                }

                var paginatedQuery = query
                    .Skip((PageNumber - 1) * PageSize)
                    .Take(PageSize)
                    .ToList();
                responeData.data = paginatedQuery;
            }
            catch (Exception ex)
            {
                responeData.code = 0;
                responeData.message = ex.Message;
            }
            return responeData;
        }
        #endregion

        #region 保存问题描述
        [HttpPost]
        public Model.ResponeData SaveRectifyItem(Model.Technique_RectifyItem rectifyItem)
        {
            var responeData = new Model.ResponeData();
            try
            {
                Model.SGGLDB db = Funs.DB;
                int count = db.Technique_RectifyItem.Count(x=>x.HazardSourcePoint == rectifyItem.HazardSourcePoint);
                if(count > 0)
                {
                    responeData.code = 0;
                    responeData.message = "描述已存在";
                } else
                {
                    Model.Technique_RectifyItem item = new Model.Technique_RectifyItem
                    {
                        RectifyItemId = SQLHelper.GetNewID(),
                        RectifyId = rectifyItem.RectifyId,
                        HazardSourcePoint = rectifyItem.HazardSourcePoint,
                        RiskPrevention = rectifyItem.RiskPrevention,
                        CompileMan = rectifyItem.CompileMan,
                        CompileDate = rectifyItem.CompileDate,
                        UnitId = rectifyItem.UnitId,
                    };
                    db.Technique_RectifyItem.InsertOnSubmit(item);
                    db.SubmitChanges();
                }
     
      
            } catch (Exception ex)
            {
                responeData.code = 0;
                responeData.message = ex.Message;
            }
            return responeData;
        }
        #endregion
        #region 获取集合
        /// <summary>
        ///  获取集合
        /// </summary>
        /// <returns></returns>
        public Model.ResponeData getRectifyList(string RectifyName, int PageNumber, int PageSize)
        {
            var responeData = new Model.ResponeData();
            try
            {
                var query = (from x in Funs.DB.Technique_RectifyItem
                    join y in Funs.DB.Technique_Rectify
                        on x.RectifyId equals y.RectifyId
                    select new
                    {
                        y.RectifyId,
                        y.RectifyName,
                        x.HazardSourcePoint,
                        x.RiskAnalysis,
                        x.RiskPrevention
                    }).ToList();
                if (!string.IsNullOrEmpty(RectifyName))
                {
                    query = query.Where(x => x.RectifyName.Contains(RectifyName)).ToList();
                }
                
                var paginatedQuery = query
                    .Skip((PageNumber - 1) * PageSize)  
                    .Take(PageSize)                     
                    .ToList();  
                
                responeData.data = paginatedQuery;
            }
            catch (Exception ex)
            {
                responeData.code = 0;
                responeData.message = ex.Message;
            }

            return responeData;
        }
        #endregion
    }
}