using Ant.Service.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MES.Production.Service.IService;
using ChangFa.Machinery.WebPage.Controllers;
using Ant.ORM;
using Ant.Data;
using MES.Production.Entity;
using JsonHelper = Ant.Service.Common.JsonHelper;

namespace ChangFa.Machinery.WebPage.Areas.SysManage.Controllers
{
    /// <summary>
    /// 省市县镇级联控制器
    /// add 作者: 季健国 QQ:181589805 by 2016-06-16
    /// </summary>
    public class CodeAreaController : BaseController
    {
        ICodeAreaManage CodeAreaManage { get; set; }
        /// <summary>
        /// 获取省份
        /// </summary>
        public ActionResult Prov()
        {
            var json = new JsonHelper() { Status = "y", Msg = "Success" };
            using (AntORM orm = new AntORM())
            {
                orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
                RequestModel res = new RequestModel
                {
                    newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取公告信息" }
                };
                var reslut = orm.Queryable<BaseProvince>().Where(p => p.Id > 0).ToList(res);
                if (reslut.IsSuccess)
                {
                    var data = reslut.ResultModel;
                    json.Data = JsonConverter.Serialize(data);
                }
            }

            return Json(json);
        }
        /// <summary>
        /// 根据省份获取城市信息
        /// </summary>
        /// <param name="id">省份ID</param>
        /// <returns></returns>
        public ActionResult City(string id)
        {
            var json = new JsonHelper() { Status = "y", Msg = "Success" };
            if (string.IsNullOrEmpty(id))
            {
                json.Msg = "Error";
                json.Status = "n";
            }
            else
            {
                using (AntORM orm = new AntORM())
                {
                    orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
                    RequestModel res = new RequestModel
                    {
                        newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取公告信息" }
                    };
                    var reslut = orm.Queryable<BaseArea>().Where(p => p.ParentCode == id).ToList(res);
                    if (reslut.IsSuccess)
                    {
                        var data = reslut.ResultModel;
                        json.Data = JsonConverter.Serialize(data);
                    }
                }
            }
            return Json(json);
        }
        /// <summary>
        /// 根据城市获取县级市信息
        /// </summary>
        /// <param name="id">城市ID</param>
        /// <returns></returns>
        public ActionResult Country(string id)
        {
            var json = new JsonHelper() { Status = "y", Msg = "Success" };
            if (string.IsNullOrEmpty(id))
            {
                json.Msg = "Error";
                json.Status = "n";
            }
            else
            {
                using (AntORM orm = new AntORM())
                {
                    orm.db = DataAccessFactory.CreateDataConnection("CyclingItem");
                    RequestModel res = new RequestModel
                    {
                        newSt = new SqlNote() { Author = "季健国", NewSt = new System.Diagnostics.StackTrace(true), SqlDesc = "获取公告信息" }
                    };
                    var reslut = orm.Queryable<BaseArea>().Where(p => p.ParentCode == id).ToList(res);
                    if (reslut.IsSuccess)
                    {
                        var data = reslut.ResultModel;
                        json.Data = JsonConverter.Serialize(data);
                    }
                }
            }
            return Json(json);
        }
    }
}