using System; using System.Collections.Generic; using System.Linq; 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; namespace MySystem.Areas.Api.Controllers.v1 { [Area("Api")] [Route("Api/v1/[controller]/[action]")] public class PosMachinesController : BaseController { public PosMachinesController(IHttpContextAccessor accessor, ILogger logger, IOptions setting) : base(accessor, logger, setting) { } #region 商户-二维码关联音箱 [Authorize] public JsonResult List(string value) { value = DesDecrypt(value); JsonData data = JsonMapper.ToObject(value); List> dataList = ListDo(value); return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList }); } public List> ListDo(string value) { JsonData data = JsonMapper.ToObject(value); int BindMerchantId = int.Parse(function.CheckInt(data["BindMerchantId"].ToString())); //绑定商户 int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString())); int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString())); List> dataList = new List>(); MerchantInfo merchant = MerchantInfoDbconn.Instance.Get(BindMerchantId) ?? new MerchantInfo(); List query = new PosMachinesService().List(new List(), " and BindMerchantId=" + BindMerchantId, PageNum, PageSize); foreach (var subdata in query) { Dictionary curData = new Dictionary(); curData.Add("BindingTime", subdata.BindingTime == null ? "" : subdata.BindingTime.Value.ToString("yyyy-MM-dd HH:mm:ss")); //绑定时间 curData.Add("PosSn", subdata.PosSn); //SN编号 curData.Add("Id", subdata.Id); //Id curData.Add("MerchantName", merchant.Name); //商户名称 string CheckKey = "_" + subdata.Id; int BindCount = MachineForQrCodeDbconn.Instance.GetList(BindMerchantId).Count(m => m.DataId.EndsWith(CheckKey)); curData.Add("BindCount", BindCount); //关联数量 dataList.Add(curData); } return dataList; } #endregion #region 商户-已绑音箱 [Authorize] public JsonResult BindedMachines(string value) { value = DesDecrypt(value); JsonData data = JsonMapper.ToObject(value); List> dataList = BindedMachinesDo(value); return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList }); } public List> BindedMachinesDo(string value) { JsonData data = JsonMapper.ToObject(value); int BindMerchantId = int.Parse(function.CheckInt(data["BindMerchantId"].ToString())); //绑定商户 int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString())); int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString())); List> dataList = new List>(); MerchantInfo merchant = MerchantInfoDbconn.Instance.Get(BindMerchantId) ?? new MerchantInfo(); List query = PosMachinesDbconn.Instance.GetList(BindMerchantId, PageNum, PageSize); foreach (int MachineId in query) { PosMachines subdata = PosMachinesDbconn.Instance.Get(MachineId) ?? new PosMachines(); Dictionary curData = new Dictionary(); curData.Add("BindingTime", subdata.BindingTime == null ? "" : subdata.BindingTime.Value.ToString("yyyy-MM-dd")); //绑定时间 curData.Add("PosSn", subdata.PosSn); //SN编号 curData.Add("Id", subdata.Id); //Id curData.Add("MerchantName", merchant.Name); //商户名称 string CheckKey = "_" + subdata.Id; List QrCodes = MachineForQrCodeDbconn.Instance.GetList(BindMerchantId); QrCodes = QrCodes.Where(m => m.DataId.EndsWith(CheckKey)).ToList(); curData.Add("BindCount", QrCodes.Count); //关联数量 List> QrCodeList = new List>(); foreach (MachineForQrCode QrCode in QrCodes) { Dictionary item = new Dictionary(); string DataId = QrCode.DataId; if (DataId.Contains("_")) { DataId = DataId.Split('_')[0]; } item.Add("Id", DataId); item.Add("Type", 1); item.Add("BindDate", QrCode.BindDate == null ? "" : QrCode.BindDate.Value.ToString("yyyy-MM-dd")); item.Add("Sn", QrCode.SnNo); QrCodeList.Add(item); } curData.Add("QrCodeList", QrCodeList); //二维码列表 dataList.Add(curData); } return dataList; } #endregion #region 商户-替换音箱码 [Authorize] public JsonResult ReplaceMachines(string value) { value = DesDecrypt(value); JsonData data = JsonMapper.ToObject(value); List> dataList = ReplaceMachinesDo(value); return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList }); } public List> ReplaceMachinesDo(string value) { JsonData data = JsonMapper.ToObject(value); int BindMerchantId = int.Parse(function.CheckInt(data["BindMerchantId"].ToString())); //绑定商户 int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString())); int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString())); string CheckKey = "_" + BindMerchantId; List> dataList = new List>(); List query = new MerchantQrCodeService().List(new List(), " and MerchantId=" + BindMerchantId, 1, 1000); foreach (MerchantQrCode subdata in query) { Dictionary curData = new Dictionary(); curData.Add("BindingTime", subdata.UpdateDate == null ? "" : subdata.UpdateDate.Value.ToString("yyyy-MM-dd HH:mm:ss")); //绑定时间 curData.Add("PosSn", subdata.SnNo); //SN编号 curData.Add("Id", subdata.Id); //Id dataList.Add(curData); } return dataList; } #endregion #region 商户-确认关联音箱 [Authorize] public JsonResult Add(string value) { value = DesDecrypt(value); JsonData data = JsonMapper.ToObject(value); AppResultJson result = AddDo(value); return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data }); } public AppResultJson AddDo(string value) { JsonData data = JsonMapper.ToObject(value); int BindMerchantId = int.Parse(function.CheckInt(data["BindMerchantId"].ToString())); //绑定商户 // string QrCodeId = data["QrCodeId"].ToString(); //二维码Id int QrCodeId = int.Parse(function.CheckInt(data["QrCodeId"].ToString())); //二维码Id string MachineId = data["MachineId"].ToString(); //音箱Id Dictionary Obj = new Dictionary(); MerchantQrCode qrcode = new MerchantQrCodeService().Query(QrCodeId); PosMachines machine = PosMachinesDbconn.Instance.Get(MachineId) ?? new PosMachines(); MachineForQrCode query = new MachineForQrCode() { DataId = QrCodeId + "_" + MachineId, MerchantId = BindMerchantId, BindDate = DateTime.Now, SnNo = qrcode.SnNo, MachineSnNo = machine.PosSn, }; machine.BindingState = 1; qrcode.MachineId = int.Parse(MachineId); return new AppResultJson() { Status = "1", Info = "", Data = Obj }; } #endregion #region 商户-绑定列表 [Authorize] public JsonResult BindList(string value) { value = DesDecrypt(value); JsonData data = JsonMapper.ToObject(value); List> dataList = BindListDo(value); return Json(new AppResultJson() { Status = "1", Info = "", Data = dataList }); } public List> BindListDo(string value) { JsonData data = JsonMapper.ToObject(value); int BindMerchantId = int.Parse(function.CheckInt(data["BindMerchantId"].ToString())); //绑定商户 int PageSize = int.Parse(function.CheckInt(data["PageSize"].ToString())); int PageNum = int.Parse(function.CheckInt(data["PageNum"].ToString())); List> dataList = new List>(); string condition = ""; if (!string.IsNullOrEmpty(data["BindMerchantId"].ToString())) { condition += " and BindMerchantId=" + BindMerchantId; } List query = new PosMachinesService().List(new List(), condition, PageNum, PageSize); foreach (var subdata in query) { Dictionary curData = new Dictionary(); curData.Add("BindingTime", subdata.BindingTime == null ? "" : subdata.BindingTime.Value.ToString("yyyy-MM-dd HH:mm:ss")); //绑定时间 curData.Add("PosSn", subdata.PosSn); //SN编号 curData.Add("MerchantName", ""); //商户名称 curData.Add("MerchantLogo", ""); //商户Logo dataList.Add(curData); } return dataList; } #endregion #region 商户-绑定机具 [Authorize] public JsonResult Bind(string value) { value = DesDecrypt(value); JsonData data = JsonMapper.ToObject(value); AppResultJson result = BindDo(value); return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data }); } public AppResultJson BindDo(string value) { JsonData data = JsonMapper.ToObject(value); string PosSn = data["PosSn"].ToString(); //SN编号 int BindMerchantId = int.Parse(function.CheckInt(data["BindMerchantId"].ToString())); //绑定商户 Dictionary Obj = new Dictionary(); PosMachines query = new PosMachines(); query = new PosMachines() { CreateDate = DateTime.Now, //创建时间 UpdateDate = DateTime.Now, //修改时间 PosSn = PosSn, //SN编号 BindMerchantId = BindMerchantId, //绑定商户 }; return new AppResultJson() { Status = "1", Info = "", Data = Obj }; } #endregion #region 商户-已绑音箱解除关联 [Authorize] public JsonResult Remove(string value) { value = DesDecrypt(value); JsonData data = JsonMapper.ToObject(value); AppResultJson result = RemoveDo(value); return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data }); } public AppResultJson RemoveDo(string value) { JsonData data = JsonMapper.ToObject(value); int BindMerchantId = int.Parse(function.CheckInt(data["BindMerchantId"].ToString())); //绑定商户 int Id = int.Parse(function.CheckInt(data["Id"].ToString())); Dictionary Obj = new Dictionary(); string CheckKey = "_" + Id; var list = maindb.MachineForQrCode.Select(m => new { m.MerchantId, m.DataId }).Where(m => m.MerchantId == BindMerchantId && m.DataId.EndsWith(CheckKey)).ToList(); foreach (var sub in list) { MachineForQrCode edit = maindb.MachineForQrCode.FirstOrDefault(m => m.DataId == sub.DataId); if (edit != null) { maindb.MachineForQrCode.Remove(edit); int MachineId = int.Parse(function.CheckInt(edit.DataId.Split('_')[1])); PosMachines machine = maindb.PosMachines.FirstOrDefault(m => m.BindMerchantId == MachineId); if (machine != null) { machine.BindMerchantId = 0; } } } maindb.SaveChanges(); return new AppResultJson() { Status = "1", Info = "", Data = Obj }; } #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 } }