using BLL.DAL;
using BLL.IDAL;
using Common;
using Json;
using Lib;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;

namespace wms.Areas.BasicInfo.Controllers
{
    public class BarCodeAjaxController : AjaxPage
    {
        // GET: BasicInfo/BarCodeAjax
        public ActionResult GetPalletNoList()
        {
            var dd = Request["ajaxdata"];

            var pnc = new JavaScriptSerializer().Deserialize<PalletNoCount>(dd);
            if (pnc.count > 0)
            {
                IDALBarCode bc = new DALBarCode();

                int startNo = bc.GetStartNo();
                IList<BarCode> lst = new List<BarCode>();
                IList<string> lst2 = new List<string>();
                DateTime t = DateTime.Now;
                string strT = t.ToString("yyyyMM");
                string strPalletType = "";

                strPalletType = pnc.type;
                if (pnc.type == "B")
                {
                    strPalletType += "0";
                }
                else
                {
                    if (pnc.SpecificationType == "01")
                    {
                        strPalletType += "3";
                    }
                    if (pnc.SpecificationType == "02")
                    {
                        strPalletType += "2";
                    }
                    if (pnc.SpecificationType == "03")
                    {
                        strPalletType += "1";
                    }
                }

                for (int i = 0; i < pnc.count; i++)
                {
                    string palletNo = "";
                    BarCode barCode = new BarCode();
                    if (startNo < 10)
                    {
                        palletNo += strPalletType;
                        palletNo += strT;
                        palletNo += "00";
                        palletNo += startNo.ToString();
                        startNo++;
                        if (startNo > 999)
                        {
                            startNo = 1;
                        }
                        barCode.code = palletNo;
                        lst.Add(barCode);
                        lst2.Add(barCode.code);
                        continue;
                    }
                    if ((startNo >= 10) && (startNo < 100))
                    {
                        palletNo += strPalletType;
                        palletNo += strT;
                        palletNo += "0";
                        palletNo += startNo.ToString();
                        startNo++;
                        if (startNo > 999)
                        {
                            startNo = 1;
                        }
                        barCode.code = palletNo;
                        lst.Add(barCode);
                        lst2.Add(barCode.code);
                        continue;
                    }
                    if ((startNo >= 100))
                    {
                        palletNo += strPalletType;
                        palletNo += strT;
                        palletNo += startNo.ToString();
                        startNo++;
                        if (startNo > 999)
                        {
                            startNo = 1;
                        }
                        barCode.code = palletNo;
                        lst.Add(barCode);
                        lst2.Add(barCode.code);
                        continue;
                    }
                }
                int n = bc.UpdateStartNo(startNo);
                string json = JsonHelper.IListToJson<BarCode>(lst, "List");
                //ReturnJson.AddProperty("Result", lst2);
                ReturnJson.AddProperty("Result", new JsonObject(json));
                ReturnJson.AddProperty("Code", 1);
                ReturnJson.AddProperty("Message", "响应成功");
                ReturnJson.AddProperty("SubCode", 0);
                ReturnJson.AddProperty("SubMessage", "");
                return Content(this.ReturnJson.ToString());
            }           

            return Content(null);
        }

        public ActionResult GetPosList()
        {

            if (true)
            {
                IDALBarCode bc = new DALBarCode();

                IList<DepotsLocation> lst = new List<DepotsLocation>();
                lst = (IList<DepotsLocation>)bc.GetPosList();

                string json = JsonHelper.IListToJson<DepotsLocation>(lst, "List");
                ReturnJson.AddProperty("Result", new JsonObject(json));
                ReturnJson.AddProperty("Code", 1);
                ReturnJson.AddProperty("Message", "响应成功");
                ReturnJson.AddProperty("SubCode", 0);
                ReturnJson.AddProperty("SubMessage", "");
                return Content(this.ReturnJson.ToString());
            }


            //return Content(null);
        }
    }
}