using BLL;
using Model;
using Model.APIItem.HSSE;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Http;
namespace WebAPI.Controllers
{
    /// 
    /// 塔吊分信息
    /// 
    public class TowerCraneController : ApiController
    {
        #region 根据类型获取图型数据
        /// 
        /// 根据类型获取图型数据
        ///   
        /// 
        [HttpPost]
        public Model.ResponeData saveTowerCraneRecord([FromBody] TowerCraneRecordItem recordItem)
        {
            var responeData = new Model.ResponeData();
            try
            {
                using (Model.SGGLDB db = new Model.SGGLDB(Funs.ConnString))
                {
                    var towerCrane = db.HSSE_TowerCrane.FirstOrDefault(x => x.TowerCraneCode == recordItem.Id);
                    if (towerCrane != null)
                    {
                        HSSE_TowerCraneRecord record = new HSSE_TowerCraneRecord();
                        record.TowerCraneRecordId = Guid.NewGuid().ToString();
                        record.TowerCraneId = towerCrane.TowerCraneId;
                        record.AlarmType = recordItem.AlarmType;
                        record.AmountHoist = recordItem.AmountHoist;
                        record.Camber = recordItem.Camber;
                        record.Height = recordItem.Height;
                        record.Hoist = recordItem.Hoist;
                        record.Moment = recordItem.Moment;
                        record.Range = recordItem.Range;
                        record.RotationAngle = recordItem.RotationAngle;
                        record.WindSpeed = recordItem.WindSpeed;
                        record.Hoist = recordItem.Hoist;  
                        record.Date = DateTime.Now; 
                        db.HSSE_TowerCraneRecord.InsertOnSubmit(record);
                        db.SubmitChanges();
                        if (!string.IsNullOrEmpty(record.AlarmType))
                        {
                            try
                            {
                                Dictionary alarmMap = new Dictionary();
                                alarmMap.Add("111", "重量预警");
                                alarmMap.Add("112", "重量报警");
                                alarmMap.Add("121", "风速预警");
                                alarmMap.Add("122", "风速报警");
                                alarmMap.Add("131", "内限位报警");
                                alarmMap.Add("132", "内限位预警");
                                alarmMap.Add("133", "外限位预警");
                                alarmMap.Add("134", "外限位报警");
                                alarmMap.Add("141", "倾角预警");
                                alarmMap.Add("142", "倾角报警");
                                alarmMap.Add("151", "上限位预警");
                                alarmMap.Add("152", "上限位报警");
                                alarmMap.Add("201", "障碍物碰撞报警");
                                alarmMap.Add("202", "塔机群碰撞报警");
                                var alarms = record.AlarmType.Replace("[", "").Replace("]", "").Split(',');
                                string res = "";
                                foreach (string alarm in alarms)
                                {
                                    if (alarmMap.ContainsKey(alarm))
                                    {
                                        res += alarmMap[alarm] + ",";
                                    }
                                }
                                res = res.TrimEnd(',');
                                var user = db.Project_ProjectUser.FirstOrDefault(x => x.ProjectId == towerCrane.ProjectId && x.UnitId == Const.UnitId_CWCEC && x.RoleName == "安全总监");
                                APICommonService.SendSubscribeMessage(user.UserId, "塔吊预警", towerCrane.TowerCraneName + ":" + res, string.Format("{0:yyyy-MM-dd HH:mm:ss}", DateTime.Now));
                            }
                            catch (Exception e)
                            {
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                responeData.code = 0;
                responeData.message = ex.Message;
            }
            return responeData;
        }
        #endregion
    }
}