using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using DSWeb.Areas.Import.Models.ReceiptNeed;
using DSWeb.Areas.CommMng.Models;
using DSWeb.TruckMng.Helper;
using DSWeb.TruckMng.Helper.Repository;
using Microsoft.Practices.EnterpriseLibrary.Data;
using HcUtility.Comm;
using HcUtility.Core;

namespace DSWeb.Areas.Import.Controllers
{
    /// <summary>
    /// 项目信息明细
    /// </summary>
    public class ReceiptNeedController : Controller
    {
        //
        // GET: /Import/RptImportFeedetail/

        public ActionResult Index()
        {
            return View();
        }

        public ContentResult GetDataList(int start, int limit, string sort, string condition)
        {
            var dataList = GetDataList(condition,sort);

            var list = dataList.Skip(start).Take(limit);
            
            var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = list.ToList() });
            return new ContentResult() { Content = json };
        }


        private static List<ReceiptNeedmb> GetDataList(string strCondition, string sort)
        {
            var strSql = new StringBuilder();
            strSql.Append(" select * from [code_Receipt_need] sd ");
            if (!string.IsNullOrEmpty(strCondition))
            {
                strSql.Append(" where " + strCondition);
            }

            strSql.Append(" union ALL ");
             strSql.Append(" select * from ( ");
             strSql.Append(" select E1.EnumValueName receiptname, ");
             strSql.Append(" E2.EnumValueName typename,0 need_recv,0 need_send,0 need_Repeat,0 need_return,null inserted ");
             strSql.Append(" from tSysEnumvalue E1,tSysEnumValue E2 where E1.EnumTypeID=2 ");
            strSql.Append(" and E2.Enumtypeid in(96005))t where t.receiptname+t.typename not in ( ");
            strSql.Append(" select receiptname+typename from [code_Receipt_need]) ");
            if (!string.IsNullOrEmpty(strCondition))
            {
                strSql.Append(" and " + strCondition);
            }
            return SetData(strSql);
        }

        private static List<ReceiptNeedmb> SetData(StringBuilder strSql)
        {
            var headList = new List<ReceiptNeedmb>();
            Database db = DatabaseFactory.CreateDatabase();
            using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (reader.Read())
                {
                    var data = new ReceiptNeedmb();
                    #region Set DB data to Object
                    data.ReceiptName = Convert.ToString(reader["ReceiptName"]);
                    data.TypeName = Convert.ToString(reader["TypeName"]);
                    data.Need_Recv = Convert.ToString(reader["Need_Recv"]);
                    data.Need_Send = Convert.ToString(reader["Need_Send"]);
                    data.Need_Repeat = Convert.ToString(reader["Need_Repeat"]);
                    data.Need_Return = Convert.ToString(reader["Need_Return"]);
                    data.inserted = Convert.ToString(reader["inserted"]);
                    #endregion
                    headList.Add(data);
                }
                reader.Close();
            }
            return headList;
        }

        public ContentResult Save(string CBBody, string CBDelBody)
        {
            var CBBodyList = JsonConvert.Deserialize<List<ReceiptNeedmb>>(CBBody);
            var CBDelBodyList = JsonConvert.Deserialize<List<ReceiptNeedmb>>(CBDelBody);

            var modb = new ModelObjectRepository();
            DBResult result = modb.SaveComm(
                   ModelObjectConvert<ReceiptNeedmb>.ToModelObjectList(CBBodyList),
                   ModelObjectConvert<ReceiptNeedmb>.ToModelObjectList(CBDelBodyList)
                  );

            //刷新父窗口上的父节点
            var jsonRespose = new JsonResponse
            {
                Success = result.Success,
                Message = result.Message,
                //Data = XXHDAL.GetData("M.ContractNo='" + head.ContractNo + "'")
            };

            return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
        }

           
    }
}