using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using BLL;
using Model;
using Model.CQMS;

namespace WebAPI.Controllers.CQMS
{
    /// <summary>
    /// 共检通知单控制器
    /// </summary>
    public class InspectionManagementController : ApiController
    {
        /// <summary>
        /// 首次扫描执行,加载数据
        /// </summary>
        /// <param name="InspectionNoticeMenuId"></param>
        /// <returns></returns>
        [HttpGet]
        public Model.ResponeData GetPersonQuality(string InspectionNoticeMenuId)
        {
            var responeData = new Model.ResponeData();
            try
            {
                var model = BLL.InspectionManagementService.GetInspectionManagementById(InspectionNoticeMenuId);
                returnPersonQuality newModel = new returnPersonQuality();
                if (model != null)
                {
                    newModel.InspectionCode = model.InspectionCode;
                    newModel.IsOnceQualified = model.IsOnceQualified;
                    if (!string.IsNullOrEmpty(model.InspectionDate.ToString()))
                    {
                        newModel.InspectionDate = model.InspectionDate.ToString().Replace("/", "-");
                    }
                    newModel.UnqualifiedReason = model.UnqualifiedReason;
                    //附件
                    var Fjmodel = Funs.DB.AttachFile.FirstOrDefault(x => x.ToKeyId == InspectionNoticeMenuId && x.MenuId == Const.InspectionNoticeMenuId);
                    if (model != null)
                    {
                        newModel.AttachUrl = Fjmodel.AttachUrl;
                    }

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

            return responeData;
        }

        #region 根据二维码ID验收日期、是否一次合格
        /// <summary>
        /// 根据二维码ID修改验收日期、是否一次合格
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        public Model.ResponeData SavePersonQuality([FromBody] Model.ProcessControl_InspectionManagement model)
        {
            var responeData = new Model.ResponeData();
            try
            {
                InspectionManagementService.UpdateByInspectionManagementId(model);
                //上传附件
                SaveAttachFile(model.InspectionId, BLL.Const.InspectionNoticeMenuId, model.AttachUrl);
            }
            catch (Exception ex)
            {
                responeData.code = 0;
                responeData.message = ex.Message;
            }

            return responeData;
        }
        #endregion

        public static void SaveAttachFile(string dataId, string menuId, string url)
        {
            Model.ToDoItem toDoItem = new Model.ToDoItem
            {
                MenuId = menuId,
                DataId = dataId,
                UrlStr = url,
            };
            APIUpLoadFileService.SaveAttachUrl(toDoItem);
        }

        

        /// <summary>
        /// 返回实体
        /// </summary>
        public class returnPersonQuality
        {
            public string InspectionDate { get; set; }

            public bool? IsOnceQualified { get; set; }

            public int MyProperty { get; set; }

            public string InspectionCode { get; set; }

            public string UnqualifiedReason { get; set; }

            public string AttachUrl { get; set; }
        }

        #region 小程序端
        /// <summary>
        ///   添加 主表+子表
        /// </summary>
        /// <param name="inspectionManagement"></param>
        /// <returns></returns>
        [HttpPost]
        public ResponseData<string> AddInspectionManagement([FromBody] InspectionManagement inspectionManagement)
        {
            ResponseData<string> res = new ResponseData<string>();
            try
            {
                res.successful = BLL.API.CQMS.ApiInspectionManagementService.AddInspectionManagement(inspectionManagement);
                res.successful = true;
            }
            catch (Exception e)
            {

            }
            return res;

        }
        /// <summary>
        /// 获取观测点
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="keyWord"></param>
        /// <returns></returns>
        public ResponseData<List<BreakdownProject>> getBreakdownProject(string projectId, string keyWord)
        {
            ResponseData<List<BreakdownProject>> res = new ResponseData<List<BreakdownProject>>();
            try
            {
                if (string.IsNullOrEmpty(keyWord))
                    keyWord = "";
                res.resultValue = BLL.API.CQMS.BreakdownProjectService.getBreakdowns(projectId, keyWord);
                res.successful = true;
            }
            catch (Exception e)
            {

            }
            return res;

        }

        /// <summary>
        ///   获取明细列表数据
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="index"></param>
        /// <param name="page"></param>
        /// <param name="state"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet]
        public ResponseData<List<InspectionManagement>> getInspectionManagement(string projectId, int index, int page, string state, string name)
        {
            ResponseData<List<InspectionManagement>> res = new ResponseData<List<InspectionManagement>>();
            try
            {
                res.successful = true;
                res.resultValue = BLL.API.CQMS.ApiInspectionManagementService.getInspectionManagement(projectId, index, page, state, name);

            }
            catch (Exception e)
            {
                res.successful = false;

            }

            return res;
        }

        /// <summary>
        ///   获取明细列表数据
        /// </summary>
        /// <param name="projectId"></param>
        /// <param name="index"></param>
        /// <param name="page"></param>
        /// <param name="state"></param>
        /// <param name="name"></param>
        /// <returns></returns>
        [HttpGet]
        public ResponseData<List<InspectionManagementDetail>> getInspectionManagementDetail(string projectId, int index, int page, string state, string name)
        {
            ResponseData<List<InspectionManagementDetail>> res = new ResponseData<List<InspectionManagementDetail>>();
            try
            {
                res.successful = true;
                res.resultValue = BLL.API.CQMS.ApiInspectionManagementService.getInspectionManagementDetail(projectId, index, page, state, name);

            }
            catch (Exception e)
            {
                res.successful = false;

            }

            return res;
        }
        /// <summary>
        /// 根据主表 获取子表数据
        /// </summary>
        /// <param name="InspectionId"></param>
        /// <returns></returns>
        [HttpGet]
        public ResponseData<List<InspectionManagementDetail>> getInspectionManagementDetailByInspectionId(string InspectionId)
        {
            ResponseData<List<InspectionManagementDetail>> res = new ResponseData<List<InspectionManagementDetail>>();
            try
            {
                res.successful = true;
                res.resultValue = BLL.API.CQMS.ApiInspectionManagementService.getInspectionManagementDetailByinspectionId(InspectionId);

            }
            catch (Exception e)
            {
                res.successful = false;

            }

            return res;
        }
        #endregion
    }
}