using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Authorization;
using System.Web;
using MySystem.MainModels;
using LitJson;
using Library;
using System.Text;
///
/// 分仓向总仓申请机具接口
///
namespace MySystem.Areas.Api.Controllers.v1
{
[Area("Api")]
[Route("/Api/v1/[controller]/[action]")]
public class ApplyMachineController : BaseController
{
public ApplyMachineController(IHttpContextAccessor accessor, ILogger logger, IOptions setting) : base(accessor, logger, setting)
{
}
// #region 创客-首页-仓库管理-仓库列表
// ///
// /// 创客仓库列表
// ///
// ///
// ///
// [Authorize]
// public JsonResult StoreHouseList(string value)
// {
// value = DesDecrypt(value);
// JsonData data = JsonMapper.ToObject(value);
// List> dataList = StoreHouseListDo(value);
// return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
// }
// public List> StoreHouseListDo(string value)
// {
// JsonData data = JsonMapper.ToObject(value);
// int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客Id
// int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
// int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
// List> dataList = new List>();
// List query = ApplyMachineDbconn.Instance.GetList(UserId, PageNum, PageSize);
// foreach (int StoreId in query)
// {
// StoreHouse subdata = ApplyMachineDbconn.Instance.Get(StoreId) ?? new StoreHouse();
// Dictionary curData = new Dictionary();
// curData.Add("Id", subdata.Id); //Id
// curData.Add("Name", subdata.ProductName); //产品仓库名称
// curData.Add("LaveNum", subdata.LaveNum); //剩余数量
// dataList.Add(curData);
// }
// return dataList;
// }
// #endregion
#region 创客-首页-仓库管理-机具申请-返回可申请机具数量
[Authorize]
public JsonResult StoreApply(string value)
{
value = DesDecrypt(value);
JsonData data = JsonMapper.ToObject(value);
AppResultJson result = StoreApplyDo(value);
return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
}
public AppResultJson StoreApplyDo(string value)
{
// value = DesDecrypt(value);
JsonData data = JsonMapper.ToObject(value);
int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客Id
int StoreId = int.Parse(function.CheckInt(data["StoreId"].ToString())); //仓库Id
int BrandId = int.Parse(function.CheckInt(data["BrandId"].ToString())); //产品类型
DateTime LastTime = DateTime.Parse(DateTime.Now.AddMonths(-1).AddDays(1 - DateTime.Now.Day).ToString("yyyy-MM-dd 00:00:00"));
DateTime ThisTime = DateTime.Parse(DateTime.Now.AddDays(1 - DateTime.Now.Day).AddDays(-1).ToString("yyyy-MM-dd 23:59:59"));
//剩余库存量
var storeHousesLaveNum = maindb.StoreHouse.FirstOrDefault(m => m.UserId == UserId && m.Id == StoreId).LaveNum;
//上月出货总量
int applyDeviceNum = 0;
bool check = maindb.MachineApply.Any(m => m.UserId == UserId && m.StoreId == StoreId && m.ApplyTime >= LastTime && m.ApplyTime <= ThisTime);
if (check)
{
applyDeviceNum = maindb.MachineApply.Where(m => m.UserId == UserId && m.StoreId == StoreId && m.ApplyTime >= LastTime && m.ApplyTime <= ThisTime).Sum(m => m.ApplyDeviceNum);
}
//上月出货量两倍
int LastApplyTwo = applyDeviceNum * 2;
//最低库存数 固定
int minInventory = 0;
//电签
if (BrandId == 1 || BrandId == 2 || BrandId == 4 || BrandId == 6)
{
minInventory = 60;
}
//大Pos
else if (BrandId == 3 || BrandId == 5)
{
minInventory = 25;
}
// //金控电签
// if (BrandId == 1)
// {
// minInventory = 60;
// }
// //开店宝电签
// else if (BrandId == 2)
// {
// minInventory = 60;
// }
// //金控大POS
// else if (BrandId == 3)
// {
// minInventory = 25;
// }
// //乐刷电签
// else if (BrandId == 4)
// {
// minInventory = 60;
// }
//申请数量上限(上月出库量的2倍或最低库存量)
int findInventory = 0;
if (LastApplyTwo > minInventory)
{
findInventory = (LastApplyTwo - storeHousesLaveNum) < 0 ? 0 : (LastApplyTwo - storeHousesLaveNum);
}
else
{
findInventory = minInventory - storeHousesLaveNum < 0 ? 0 : minInventory - storeHousesLaveNum;
}
//箱数
int boxes = 0;
//电签
if (BrandId == 1 || BrandId == 2 || BrandId == 4 || BrandId == 6)
{
boxes = findInventory / 50;
}
//大Pos
else if (BrandId == 3 || BrandId == 5)
{
boxes = findInventory / 10;
}
// //电签
// if (BrandId == 1 || BrandId == 1 || BrandId == 1 || BrandId == 1)
// {
// boxes = findInventory / 50;
// }
// //开店宝电签
// else if (BrandId == 2)
// {
// boxes = findInventory / 50;
// }
// //金控大POS
// else if (BrandId == 3)
// {
// boxes = findInventory / 10;
// }
// //乐刷电签
// else if (BrandId == 4)
// {
// boxes = findInventory / 50;
// }
// //乐刷大POS
// else if (BrandId == 5)
// {
// boxes = findInventory / 10;
// }
// //立刷电签
// else if (BrandId == 6)
// {
// boxes = findInventory / 50;
// }
//每箱台数
int boxNum = 0;
if (BrandId == 1 || BrandId == 2 || BrandId == 4 || BrandId == 6)
{
boxNum = 50;
}
else if (BrandId == 3 || BrandId == 5)
{
boxNum = 10;
}
Machines machines = new Machines();
machines.findInventory = findInventory;
machines.boxes = boxes;
machines.boxeNum = boxNum;
return new AppResultJson() { Status = "1", Info = "", Data = machines };
}
#endregion
// #region 创客-首页-仓库管理-机具申请--确认申请
// [Authorize]
// public JsonResult ConfirmApply(string value)
// {
// value = DesDecrypt(value);
// JsonData data = JsonMapper.ToObject(value);
// AppResultJson result = ConfirmApplyDo(value);
// return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data });
// }
// public AppResultJson ConfirmApplyDo(string value)
// {
// JsonData data = JsonMapper.ToObject(value);
// int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客Id
// int StoreId = int.Parse(function.CheckInt(data["StoreId"].ToString())); //仓库Id
// int BrandId = int.Parse(function.CheckInt(data["BrandId"].ToString())); //产品类型
// int ApplyNum = int.Parse(function.CheckInt(data["ApplyNum"].ToString())); //申请箱数
// DateTime LastTime = DateTime.Parse(DateTime.Now.AddMonths(-1).AddDays(1 - DateTime.Now.Day).ToString("yyyy-MM-dd 00:00:00"));
// DateTime ThisTime = DateTime.Parse(DateTime.Now.AddDays(1 - DateTime.Now.Day).AddDays(-1).ToString("yyyy-MM-dd 23:59:59"));
// //剩余库存量
// var storeHousesLaveNum = maindb.StoreHouse.FirstOrDefault(m => m.UserId == UserId && m.Id == StoreId).LaveNum;
// //上月出货总量
// int applyDeviceNum = 0;
// bool check = maindb.MachineApply.Any(m => m.UserId == UserId && m.StoreId == StoreId && m.ApplyTime >= LastTime && m.ApplyTime <= ThisTime);
// if (check)
// {
// applyDeviceNum = maindb.MachineApply.Where(m => m.UserId == UserId && m.StoreId == StoreId && m.ApplyTime >= LastTime && m.ApplyTime <= ThisTime).Sum(m => m.ApplyDeviceNum);
// }
// //上月出货量两倍
// int LastApplyTwo = applyDeviceNum * 2;
// //最低库存数
// int minInventory = 0;
// //电签
// if (BrandId == 1 || BrandId == 2 || BrandId == 4 || BrandId == 6)
// {
// minInventory = 60;
// }
// //大Pos
// else if (BrandId == 3 || BrandId == 5)
// {
// minInventory = 25;
// }
// //申请数量上限(上月出库量的2倍或最低库存量)
// int findInventory = 0;
// if (LastApplyTwo > minInventory)
// {
// findInventory = (LastApplyTwo - storeHousesLaveNum) < 0 ? 0 : (LastApplyTwo - storeHousesLaveNum);
// }
// else
// {
// findInventory = minInventory - storeHousesLaveNum < 0 ? 0 : minInventory - storeHousesLaveNum;
// }
// //每箱台数
// int boxNum = 0;
// if (BrandId == 1 || BrandId == 2 || BrandId == 4 || BrandId == 6)
// {
// boxNum = 50;
// }
// else if (BrandId == 3 || BrandId == 5)
// {
// boxNum = 10;
// }
// bool checks = maindb.StoreMachineApply.Any(m => m.UserId == UserId && m.StoreId == StoreId && m.BrandId == BrandId && m.Status == 0);
// if (checks)
// {
// return new AppResultJson() { Status = "-1", Info = "您有相同机具类型的申请正在进行中,请等待完成后再次申请" };
// }
// if (!checks)
// {
// if (findInventory < boxNum)
// {
// return new AppResultJson() { Status = "-1", Info = "可申请机具数量低于机具整箱数量,不可申请" };
// }
// if (storeHousesLaveNum <= applyDeviceNum || storeHousesLaveNum <= minInventory)
// {
// Users users = maindb.Users.Find(UserId);
// StoreHouse storehouse = maindb.StoreHouse.Find(StoreId);
// Orders orders = new Orders();
// orders.Status = 0;
// orders.CreateDate = DateTime.Now;
// orders.StoreId = StoreId;
// orders.Areas = storehouse.Areas;
// orders.Address = storehouse.Address;
// orders.Mobile = users.Mobile;
// orders.StoreContactMobile = storehouse.ManageMobile;
// orders.RealName = users.RealName;
// orders.UserId = storehouse.UserId;
// maindb.Orders.Add(orders);
// maindb.SaveChanges();
// StoreMachineApply storeMachineApply = new StoreMachineApply();
// storeMachineApply.Version = 0;
// storeMachineApply.CreateDate = DateTime.Now;
// storeMachineApply.ApplyTime = DateTime.Now;
// storeMachineApply.ApplyNo = "AS" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + function.get_Random(8);
// storeMachineApply.BrandId = BrandId;
// storeMachineApply.ApplyNum = ApplyNum;
// storeMachineApply.BoxNum = boxNum;
// storeMachineApply.LastApply = applyDeviceNum;
// storeMachineApply.MaxApply = findInventory;
// storeMachineApply.ActualApply = ApplyNum * boxNum;
// storeMachineApply.UserId = UserId;
// storeMachineApply.StoreId = StoreId;
// maindb.StoreMachineApply.Add(storeMachineApply);
// maindb.SaveChanges();
// storeMachineApply.OrderId = orders.Id;
// orders.Sort = storeMachineApply.Id;
// maindb.SaveChanges();
// }
// }
// return new AppResultJson() { Status = "1", Info = "申请成功", Data = "" };
// }
// #endregion
// #region 创客-首页-仓库管理-机具申请-申请记录列表
// ///
// /// 机具申请记录列表
// ///
// ///
// ///
// [Authorize]
// public JsonResult StoreApplyRecordList(string value)
// {
// value = DesDecrypt(value);
// JsonData data = JsonMapper.ToObject(value);
// List> dataList = StoreApplyRecordListDo(value);
// return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
// }
// public List> StoreApplyRecordListDo(string value)
// {
// JsonData data = JsonMapper.ToObject(value);
// int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客Id
// int BrandId = int.Parse(function.CheckInt(data["BrandId"].ToString()));//产品类型
// int StoreId = int.Parse(function.CheckInt(data["StoreId"].ToString()));//仓库Id
// int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
// int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
// List> dataList = new List>();
// List query = ApplyMachineDbconn.Instance.GetApplyList(UserId, StoreId, PageNum, PageSize);
// foreach (int Id in query)
// {
// StoreMachineApply subdata = ApplyMachineDbconn.Instance.GetApply(Id) ?? new StoreMachineApply();
// Dictionary curData = new Dictionary();
// curData.Add("Id", subdata.Id); //Id
// curData.Add("ApplyTime", Convert.ToDateTime(subdata.ApplyTime).ToString("yyyy-MM-dd HH:mm:ss")); //申请时间
// curData.Add("BrandId", subdata.BrandId); //机具类型
// curData.Add("ApplyNum", subdata.ApplyNum); //申请箱数
// curData.Add("Status", subdata.Status); //申请状态
// dataList.Add(curData);
// }
// return dataList;
// }
// #endregion
// #region 创客-首页-仓库管理-机具申请-申请记录详情
// ///
// /// 机具申请列表详情
// ///
// ///
// ///
// [Authorize]
// public JsonResult StoreApplyDetail(string value)
// {
// value = DesDecrypt(value);
// JsonData data = JsonMapper.ToObject(value);
// List> dataList = StoreApplyDetailDo(value);
// return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
// }
// public List> StoreApplyDetailDo(string value)
// {
// JsonData data = JsonMapper.ToObject(value);
// int UserId = int.Parse(function.CheckInt(data["UserId"].ToString())); //创客Id
// int StoreId = int.Parse(function.CheckInt(data["StoreId"].ToString()));//仓库Id
// int StoreApplyId = int.Parse(function.CheckInt(data["StoreApplyId"].ToString()));//申请记录Id
// List> dataList = new List>();
// List query = ApplyMachineDbconn.Instance.GetApplyDetailList(UserId, StoreId, StoreApplyId);
// foreach (int Id in query)
// {
// StoreMachineApply subdata = ApplyMachineDbconn.Instance.GetApply(Id) ?? new StoreMachineApply();
// var temp = subdata.SwapSnExpand;
// temp = temp.Substring(1);
// temp = temp.Substring(0, temp.Length - 1);
// var SwapSnExpand = temp.Split(",,");
// Orders orders = OrdersDbconn.Instance.Get(subdata.OrderId.Value) ?? new Orders();
// Dictionary curData = new Dictionary();
// curData.Add("Id", subdata.Id); //Id
// curData.Add("ApplyTime", Convert.ToDateTime(subdata.ApplyTime).ToString("yyyy-MM-dd HH:mm:ss")); //申请时间
// curData.Add("BrandId", subdata.BrandId); //快递单号
// curData.Add("ApplyNum", subdata.ApplyNum); //申请箱数
// curData.Add("SendNum", subdata.SendNum); //发货箱数
// curData.Add("BoxNum", subdata.BoxNum); //每箱台数
// curData.Add("ErpMode", orders.ErpMode); //配送方式
// curData.Add("OrderNo", orders.OrderNo); //快递单号
// curData.Add("SwapSnExpand", SwapSnExpand); //机具SN号
// dataList.Add(curData);
// }
// return dataList;
// }
// #endregion
#region 创客-首页-仓库管理-订单列表-库存
[Authorize]
public JsonResult StoreStockList(string value)
{
value = DesDecrypt(value);
JsonData data = JsonMapper.ToObject(value);
List> dataList = StoreStockListDo(value);
return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
}
public List> StoreStockListDo(string value)
{
JsonData data = JsonMapper.ToObject(value);
int StoreId = int.Parse(function.CheckInt(data["StoreId"].ToString())); //仓库Id
int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
List> dataList = new List>();
IQueryable query = maindb.PosMachinesTwo.Where(m => m.StoreId == StoreId && m.UserId == 0 && m.BuyUserId == 0 && m.PreUserId == 0 && m.Status > -1);//库存和预发机分离
int skipNum = PageSize * (PageNum - 1);
query = query.Skip(skipNum).Take(PageSize);
foreach (var item in query.ToList())
{
PosMachinesTwo subdata = PosMachinesTwoDbconn.Instance.Get(item.Id) ?? new PosMachinesTwo();
Dictionary curData = new Dictionary();
curData.Add("PosSn", subdata.PosSn);
curData.Add("CreateDate", subdata.CreateDate);
curData.Add("PreUserId", subdata.PreUserId);
dataList.Add(curData);
}
return dataList;
}
#endregion
#region 创客-首页-仓库管理-订单列表-预发机
[Authorize]
public JsonResult PreStoreStockList(string value)
{
value = DesDecrypt(value);
JsonData data = JsonMapper.ToObject(value);
List> dataList = PreStoreStockListDo(value);
return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList });
}
public List> PreStoreStockListDo(string value)
{
JsonData data = JsonMapper.ToObject(value);
int StoreId = int.Parse(function.CheckInt(data["StoreId"].ToString())); //仓库Id
int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString()));
int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString()));
List> dataList = new List>();
IQueryable query = maindb.PreSendStockDetail.Where(m => m.FromStoreId == StoreId && m.Status >= 0 && m.Status <= 1 && m.ApplyFlag == 0);
int skipNum = PageSize * (PageNum - 1);
query = query.Skip(skipNum).Take(PageSize);
foreach (var item in query.ToList())
{
Dictionary curData = new Dictionary();
curData.Add("PosSn", item.SnNo);
curData.Add("CreateDate", item.CreateDate);
curData.Add("isGrant", item.AuthFlag); //占用小分仓额度标记
dataList.Add(curData);
}
return dataList;
}
#endregion
#region 检查签名是否合法,合法返回1,不合法返回提示信息
///
/// 检查签名是否合法,合法返回1,不合法返回提示信息
///
/// 请求的参数(json字符串)
/// 要签名的字段
///
private string CheckSign(string value, string[] signField)
{
JsonData json = JsonMapper.ToObject(value);
Dictionary dic = new Dictionary();
for (int i = 0; i < signField.Length; i++)
{
dic.Add(signField[i], json[signField[i]].ToString());
}
string sign = json["sign"].ToString(); //客户端签名字符串
return new Sign().sign(dic, sign);
}
#endregion
#region 私有类-获取最多申请台数、箱数、每箱台数
public class Machines
{
//最多申请台数
public int findInventory { get; set; }
//最多申请箱数
public int boxes { get; set; }
//每箱台数
public int boxeNum { get; set; }
}
#endregion
}
}