using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using PushPlatform.Data;
using PushPlatform.Data.Bll;
using PushPlatform.Data.Criteria;
using PushPlatform.Data.MvcModels;
using PushPlatform.Common.Util;

namespace PushPlatform.Background.Areas.BackGround.Controllers
{
    public class WechatTemController : BaseController
    {
        BllWechatMsgmodel temBll = new BllWechatMsgmodel();
        // GET: BackGround/WechatTem
        #region 微信模板列表
        public ActionResult Index()
        {
            var user = BllSysUser.GetCurrentUser();
            var temList = temBll.GetWechatTem(user.CurrentProjectId);
            return View(temList);
        }
        #endregion

        #region 模板详情
        public ActionResult TempDetail(string Id)
        {
            WechatMsgmodel model = new WechatMsgmodel();
            if(!string.IsNullOrEmpty(Id))
            {
                model = temBll.Get(Id);
            }
            return PartialView(model);
        }
        #endregion

        #region 模板保存
        public JsonResult TempSave(WechatMsgmodel model)
        {
            JsonResult jr = new JsonResult();
            try
            {
                CurrentUserModel user = BllSysUser.GetCurrentUser();
                WechatMsgmodel saveModel = null;
                if (!string.IsNullOrEmpty(model.Id))
                {
                    saveModel = temBll.Get(model.Id);
                    saveModel.Update_Time = DateTime.Now;
                    saveModel.Update_User = user.UserId;
                    saveModel.IsEdit = true;
                }
                else
                {
                    saveModel = new WechatMsgmodel();
                    saveModel.Id = Guid.NewGuid().ToString();
                    saveModel.Create_Time = DateTime.Now;
                    saveModel.Create_User = user.UserId;
                    saveModel.Is_Deleted = 0;
                    saveModel.Timestamp = DateTimeHelper.GetCurrentTimestamp();
                }
                saveModel.ProjectId = user.CurrentProjectId;
                saveModel.Send_Cfg_Id = model.Send_Cfg_Id;
                saveModel.OriginalId = model.OriginalId;
                saveModel.Templete_Id = model.Templete_Id;
                saveModel.Title = model.Title;
                saveModel.Content_Temp = model.Content_Temp;
                List<WechatMsgmodel> sendList = new List<WechatMsgmodel>();
                sendList.Add(saveModel);
                if (temBll.SaveOrUpdate(sendList) > 0)
                {
                    jr.Data = new { result = 1, msg = "保存成功",Id= saveModel.Id };
                }
                else
                {
                    jr.Data = new { result = 0, msg = "保存失败！" };
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error(ex);
                jr.Data = new { result = 0, msg = "服务器异常，请联系管理员！" };
            }
            return jr;
        }
        #endregion
    }
}