using System;
using System.Linq;
using System.Web.Mvc;
using DSWeb.Areas.Account.DAL.Chfee_Cheque;
using DSWeb.Areas.Account.Models.Chfee_cheque;
using DSWeb.MvcShipping.Helper;
using DSWeb.MvcShipping.Comm.Cookie;
using DSWeb.Areas.CommMng.DAL;
using System.Collections.Generic;
using HcUtility.Comm;
using HcUtility.Core;
using DSWeb.EntityDA;
using DSWeb.Areas.Account.Models.Chfee_Exrate;


namespace DSWeb.Areas.Account.Controllers
{
    [JsonRequestBehavior]
    public class Chfee_chequeController : Controller
    {
        //
        // GET: 
        public ActionResult Index()
        {
            return View();
        }

        //
        // GET: /
        public ActionResult Edit()
        {
            return View();
        }

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

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


        //
        // GET:

        #region 支票
        public ContentResult GetDataList(int start, int limit, string sort, string condition)
        {
            var dataList = ChChequeDAL.GetDataList(condition, Convert.ToString(Session["USERID"]), CookieConfig.GetCookie_UserCode(Request), Convert.ToString(Session["COMPANYID"]), 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 };
        }


        public ContentResult GetData(string handle, string condition)
        {
            ChCheque head = null;

            if (handle == "edit")
            {
                var list = ChChequeDAL.GetDataList(condition, Convert.ToString(Session["USERID"]), CookieConfig.GetCookie_UserCode(Request), Convert.ToString(Session["COMPANYID"]));
                if (list.Count > 0)
                    head = list[0];
            }

            if (head == null)
            {
                head = new ChCheque();
            }

            if (handle == "add")
            {
                head.CREATEUSER = Convert.ToString(Session["USERID"]);
                head.CREATEUSERREF = Convert.ToString(Session["SHOWNAME"]);
                head.COMPANYID = Convert.ToString(Session["COMPANYID"]);
            }

            if (handle == "copy")
            {
                head.CREATEUSER = Convert.ToString(Session["USERID"]);
                head.CREATEUSERREF = Convert.ToString(Session["SHOWNAME"]);
                head.COMPANYID = Convert.ToString(Session["COMPANYID"]);
            }

            var json = JsonConvert.Serialize(
               new { Success = true, Message = "查询成功", data = head });
            return new ContentResult() { Content = json };
        }



        public ContentResult Save(string opstatus, string data)
         {


             var headData = JsonConvert.Deserialize<ChCheque>(data);


            if (opstatus == "add")
            {
                headData.GID = Guid.NewGuid().ToString();
                headData.BILLNO = PubSysDAL.GetBillNo("0601");
                headData.COMPANYID = Convert.ToString(Session["COMPANYID"]);
                headData.CREATEUSER = Convert.ToString(Session["USERID"]);
                headData.CREATETIME = DateTime.Now.ToString();
                headData.DbOperationType = DbOperationType.DbotIns;
            }
            else if (opstatus == "edit")
            {
                headData.DbOperationType = DbOperationType.DbotUpd;
                headData.ModelUIStatus = "E";
            }
            else if (opstatus == "copy")
            {
                headData.GID = Guid.NewGuid().ToString();
                headData.BILLNO = PubSysDAL.GetBillNo("0601");
                headData.COMPANYID = Convert.ToString(Session["COMPANYID"]);
                headData.CREATEUSER = Convert.ToString(Session["USERID"]);
                headData.CREATETIME = DateTime.Now.ToString();
                headData.DbOperationType = DbOperationType.DbotIns;
            }
            else
            {
                headData.DbOperationType = DbOperationType.DbotDel;
            }
            if (headData.CHEQUEMAKETIME == "") headData.CHEQUEMAKETIME = null;

            var BILLNO = headData.BILLNO;

            var amountstr = string.Format("{0:0.00}", headData.CHEQUEAMOUNT);
            amountstr = amountstr.Replace(".","");
            headData.FEN = ChChequeDAL.Amount2STR(amountstr,1,headData.CHEQUECURRENCY);
            headData.JIAO = ChChequeDAL.Amount2STR(amountstr, 2, headData.CHEQUECURRENCY);
            headData.YUAN = ChChequeDAL.Amount2STR(amountstr, 3, headData.CHEQUECURRENCY);
            headData.SHI = ChChequeDAL.Amount2STR(amountstr, 4, headData.CHEQUECURRENCY);
            headData.BAI = ChChequeDAL.Amount2STR(amountstr, 5, headData.CHEQUECURRENCY);
            headData.QIAN = ChChequeDAL.Amount2STR(amountstr, 6, headData.CHEQUECURRENCY);
            headData.WAN = ChChequeDAL.Amount2STR(amountstr, 7, headData.CHEQUECURRENCY);
            headData.SHIWAN = ChChequeDAL.Amount2STR(amountstr, 8, headData.CHEQUECURRENCY);
            headData.BAIWAN= ChChequeDAL.Amount2STR(amountstr, 9, headData.CHEQUECURRENCY);
            headData.QIANWAN = ChChequeDAL.Amount2STR(amountstr, 10, headData.CHEQUECURRENCY);
            headData.YI = ChChequeDAL.Amount2STR(amountstr, 11, headData.CHEQUECURRENCY);
            headData.SHIYI = ChChequeDAL.Amount2STR(amountstr, 12, headData.CHEQUECURRENCY);
            headData.BAIYI = ChChequeDAL.Amount2STR(amountstr, 13, headData.CHEQUECURRENCY);

            //            headData.FEN=


            var modb = new ModelObjectDB();

            DBResult result = modb.Save(headData);
            if (result.Success == true)
                ChChequeDAL.UpdateCheNoUse(headData.CHEQUENO, headData.BANKID, Convert.ToString(Session["USERID"]));



            var jsonRespose = new JsonResponse
            {
                Success = result.Success,
                Message = result.Message,
                Data = ChChequeDAL.GetData(" BILLNO='" + BILLNO + "'", Convert.ToString(Session["USERID"]), CookieConfig.GetCookie_UserCode(Request), Convert.ToString(Session["COMPANYID"]))
            };

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

        }



         //


         public ContentResult Delete(string data)
         {
             var headData = JsonConvert.Deserialize<ChCheque>(data);
             DBResult result;
                     var modb = new ModelObjectDB();
                     result=modb.Delete(headData);
            if (result.Success == true)
                ChChequeDAL.UpdateCheNoCancelUse(headData.CHEQUENO,headData.BANKID);

             var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
             return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
         }




         public ContentResult DeleteUp(string data)
         {
             var headData = JsonConvert.Deserialize<ChCheque>(data);
             DBResult result;

                 result=ChChequeDAL.UpdateDelete(headData.BILLNO, Convert.ToString(Session["USERID"]));
             if (result.Success==true)
                 ChChequeDAL.UpdateCheNoDelete(headData.CHEQUENO, headData.BANKID, Convert.ToString(Session["USERID"]));

             var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
             return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
         }

         public ContentResult GetChequeNo(string condition)
         {
             if (condition == null || condition == "") { 
               condition=" b.COMPANYID='"+Convert.ToString(Session["COMPANYID"])+"'";
             } else {
                 condition = condition + " AND b.COMPANYID='" + Convert.ToString(Session["COMPANYID"]) + "'";             
             }

             var dataList = ChChequeDAL.GetChequeNo(condition);


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



         #region 锁定和撤销锁定

         public ContentResult Lock(string bill)
         {

             DBResult result = ChChequeDAL.Lock(bill);

             var json = JsonConvert.Serialize(result);
             return new ContentResult() { Content = json };
         }

         public ContentResult UnLock(string bill)
         {

             DBResult result = ChChequeDAL.UnLock(bill);

             var json = JsonConvert.Serialize(result);
             return new ContentResult() { Content = json };
         }

         public ContentResult LockList(string bills)
         {

             DBResult result = ChChequeDAL.LockList(bills);

             var json = JsonConvert.Serialize(result);
             return new ContentResult() { Content = json };
         }

         public ContentResult UnLockList(string bills)
         {

             DBResult result = ChChequeDAL.UnLockList(bills);

             var json = JsonConvert.Serialize(result);
             return new ContentResult() { Content = json };
         }




         #endregion


         public ContentResult UpdatePrint(string billno)
         {

             DBResult result = ChChequeDAL.UpdatePrint(billno);
             var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
             return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
         }
        #endregion

        #region 支票册
         public ContentResult GetBookDataList(int start, int limit, string sort, string condition)
         {
             var dataList = ChChequeDAL.GetBookDataList(condition, Convert.ToString(Session["COMPANYID"]), 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 };
         }

         public ContentResult GetBookData(string handle, string condition)
         {
             ChChequeBook head = null;

             if (handle == "edit")
             {
                 var list = ChChequeDAL.GetBookDataList(condition, Convert.ToString(Session["COMPANYID"]));
                 if (list.Count > 0)
                     head = list[0];
             }

             if (head == null)
             {
                 head = new ChChequeBook();
             }

             if (handle == "add")
             {
                 head.CREATEUSER = Convert.ToString(Session["USERID"]);
                 head.CREATEUSERREF = Convert.ToString(Session["SHOWNAME"]);
                 head.COMPANYID = Convert.ToString(Session["COMPANYID"]);
             }



             var json = JsonConvert.Serialize(
                new { Success = true, Message = "查询成功", data = head });
             return new ContentResult() { Content = json };
         }


         public ContentResult SaveBook(string opstatus, string data)
         {


             var headData = JsonConvert.Deserialize<ChChequeBook>(data);


             if (opstatus == "add")
             {
                 headData.GID = Guid.NewGuid().ToString();
                  headData.COMPANYID = Convert.ToString(Session["COMPANYID"]);
                 headData.CREATEUSER = Convert.ToString(Session["USERID"]);
                 headData.CREATETIME = DateTime.Now.ToString();
                 headData.DbOperationType = DbOperationType.DbotIns;
             }
             else if (opstatus == "edit")
             {
                 headData.DbOperationType = DbOperationType.DbotUpd;
                 headData.ModelUIStatus = "E";
             }
             else
             {
                 headData.DbOperationType = DbOperationType.DbotDel;
             }


             var BILLNO = headData.GID;
             var modb = new ModelObjectDB();

             DBResult result = modb.Save(headData);




             var jsonRespose = new JsonResponse
             {
                 Success = result.Success,
                 Message = result.Message,
                 Data = ChChequeDAL.GetBookData(" GID='" + BILLNO + "'", Convert.ToString(Session["COMPANYID"]))
             };

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

         }

         public ContentResult DeleteBook(string data)
         {
             var headData = JsonConvert.Deserialize<ChChequeBook>(data);
             var isfee = ChChequeDAL.GetMakeOutCount(headData.GID);
                         if (isfee)
                         {

                             var jsonRespose = new JsonResponse { Success = false, Message = "此票册已有开出支票,不允许删除!" };
                             return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
                         }
                         else
                         {

                             DBResult result;
                             var modb = new ModelObjectDB();
                             result = modb.Delete(headData);

                             var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
                             return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
                         }
         }

         public ContentResult CreateBookitems(string data)
         {
             var headData = JsonConvert.Deserialize<ChChequeBook>(data);

             DBResult result = ChChequeDAL.CreateBookitems(headData, Convert.ToString(Session["USERID"]));

             var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
             return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
         }

         public ContentResult LockBookitems(string data)
         {
             var headData = JsonConvert.Deserialize<List<ChChequeBookdetail>>(data);

             DBResult result = ChChequeDAL.LockBookitems(headData);

             var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
             return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
         }

         public ContentResult CancelLockBookitems(string data)
         {
             var headData = JsonConvert.Deserialize<List<ChChequeBookdetail>>(data);

             DBResult result = ChChequeDAL.CancelLockBookitems(headData);

             var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
             return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
         }

         public ContentResult GetBookitemsDataList(string condition, string sort)
         {
             var dataList = ChChequeDAL.GetBookitemsDataList(condition, sort);


             var json = JsonConvert.Serialize(new { Success = true, Message = "查询成功", totalCount = dataList.Count, data = dataList.ToList() });
             return new ContentResult() { Content = json };
         }
         public ContentResult DeleteUpBook(string data)
         {
             var headData = JsonConvert.Deserialize<ChChequeBook>(data);
             DBResult result;

             result = ChChequeDAL.DeleteUpBook(headData.GID, Convert.ToString(Session["USERID"]));

             var jsonRespose = new JsonResponse { Success = result.Success, Message = result.Message };
             return new ContentResult() { Content = JsonConvert.Serialize(jsonRespose) };
         }

        #endregion

        #region 参照部分

        #endregion

    }
}