using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using BLL;
using Model;
using Model.CQMS;
namespace WebAPI.Controllers.CQMS
{
    /// 
    /// 共检通知单控制器
    /// 
    public class InspectionManagementController : ApiController
    {
        /// 
        /// 首次扫描执行,加载数据
        /// 
        /// 
        /// 
        [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验收日期、是否一次合格
        /// 
        /// 根据二维码ID修改验收日期、是否一次合格
        /// 
        /// 
        /// 
        [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);
        }
        
        /// 
        /// 返回实体
        /// 
        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 小程序端
        /// 
        ///   添加 主表+子表
        /// 
        /// 
        /// 
        [HttpPost]
        public ResponseData AddInspectionManagement([FromBody] InspectionManagement inspectionManagement)
        {
            ResponseData res = new ResponseData();
            try
            {
                res.successful = BLL.API.CQMS.ApiInspectionManagementService.AddInspectionManagement(inspectionManagement);
                res.successful = true;
            }
            catch (Exception e)
            {
            }
            return res;
        }
        /// 
        /// 获取观测点
        /// 
        /// 
        /// 
        /// 
        public ResponseData> getBreakdownProject(string projectId, string keyWord)
        {
            ResponseData> res = new ResponseData>();
            try
            {
                if (string.IsNullOrEmpty(keyWord))
                    keyWord = "";
                res.resultValue = BLL.API.CQMS.BreakdownProjectService.getBreakdowns(projectId, keyWord);
                res.successful = true;
            }
            catch (Exception e)
            {
            }
            return res;
        }
        /// 
        ///   获取明细列表数据
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        [HttpGet]
        public ResponseData> getInspectionManagement(string projectId, int index, int page, string state, string name)
        {
            ResponseData> res = new ResponseData>();
            try
            {
                res.successful = true;
                res.resultValue = BLL.API.CQMS.ApiInspectionManagementService.getInspectionManagement(projectId, index, page, state, name);
            }
            catch (Exception e)
            {
                res.successful = false;
            }
            return res;
        }
        /// 
        ///   获取明细列表数据
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        /// 
        [HttpGet]
        public ResponseData> getInspectionManagementDetail(string projectId, int index, int page, string state, string name)
        {
            ResponseData> res = new ResponseData>();
            try
            {
                res.successful = true;
                res.resultValue = BLL.API.CQMS.ApiInspectionManagementService.getInspectionManagementDetail(projectId, index, page, state, name);
            }
            catch (Exception e)
            {
                res.successful = false;
            }
            return res;
        }
        /// 
        /// 根据主表 获取子表数据
        /// 
        /// 
        /// 
        [HttpGet]
        public ResponseData> getInspectionManagementDetailByInspectionId(string InspectionId)
        {
            ResponseData> res = new ResponseData>();
            try
            {
                res.successful = true;
                res.resultValue = BLL.API.CQMS.ApiInspectionManagementService.getInspectionManagementDetailByinspectionId(InspectionId);
            }
            catch (Exception e)
            {
                res.successful = false;
            }
            return res;
        }
        #endregion
    }
}