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 MerchantQrCodeController : BaseController { public MerchantQrCodeController(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 MerchantId = int.Parse(function.CheckInt(data["MerchantId"].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(MerchantId) ?? new MerchantInfo(); IQueryable query = maindb.MerchantQrCode.Where(m => m.MerchantId == MerchantId); if (PageNum == 1) { query = query.Take(PageSize); } else { int skipNum = PageSize * (PageNum - 1); query = query.Skip(skipNum).Take(PageSize); } foreach (MerchantQrCode subdata in query.ToList()) { Dictionary curData = new Dictionary(); curData.Add("SnNo", subdata.SnNo); //Sn编号 curData.Add("Id", subdata.Id); //Id curData.Add("BindDate", subdata.CreateDate == null ? "" : subdata.CreateDate.Value.ToString("yyyy-MM-dd")); //绑定时间 curData.Add("MerchantName", merchant.Name); //商户名称 PosMachines machine = PosMachinesDbconn.Instance.Get(subdata.MachineId) ?? new PosMachines(); curData.Add("MachineSnNo", machine.PosSn); //音箱SN curData.Add("BindFlag", machine.BindingState); //绑定状态 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); int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户 string SnNo = data["SnNo"].ToString(); //Sn编号 string MachineNo = data["Machine"].ToString(); //音箱码 Dictionary Obj = new Dictionary(); MerchantQrCode query = new MerchantQrCodeService().Query(SnNo); PosMachinesTwo pos = new PosMachinesTwoService().Query(SnNo); MerchantInfo merchant = new MerchantInfoService().Query(MerchantId); if(pos.Id == 0) { return new AppResultJson() { Status = "-1", Info = "绑定失败" }; } if(pos.BuyUserId == 0) { return new AppResultJson() { Status = "-1", Info = "码牌未授权" }; } if (query.Id > 0) { if (query.MerchantId > 0) { return new AppResultJson() { Status = "-1", Info = "此二维码已被绑定,请勿重复绑定" }; } } Dictionary fields = new Dictionary(); fields.Add("MerchantId", MerchantId); fields.Add("SnNo", SnNo); fields.Add("QueryCount", 1); new MerchantQrCodeService().Add(fields); // query = maindb.MerchantQrCode.Add(new MerchantQrCode() // { // CreateDate = DateTime.Now, //绑定时间 // MerchantId = MerchantId, //商户 // SnNo = SnNo, // }).Entity; // maindb.SaveChanges(); string DataId = query.Id + "_0"; MachineForQrCode forQrCode = maindb.MachineForQrCode.FirstOrDefault(m => m.DataId == DataId); if (forQrCode == null) { forQrCode = maindb.MachineForQrCode.Add(new MachineForQrCode(){ DataId = DataId, SnNo = SnNo, MachineSnNo = MachineNo, BindDate = DateTime.Now, MerchantId = MerchantId, }).Entity; } else { forQrCode.SnNo = SnNo; forQrCode.MachineSnNo = MachineNo; forQrCode.BindDate = DateTime.Now; forQrCode.MerchantId = MerchantId; } maindb.SaveChanges(); fields = new Dictionary(); fields.Add("BuyUserId", merchant.UserId); fields.Add("UserId", merchant.UserId); fields.Add("BindingTime", DateTime.Now); fields.Add("BindingState", 1); fields.Add("BindMerchantId", MerchantId); fields.Add("OpId", 1); new PosMachinesTwoService().Edit(fields, pos.Id); // pos.BuyUserId = merchant.UserId; // pos.UserId = merchant.UserId; // pos.BindingTime = DateTime.Now; // pos.BindingState = 1; // pos.BindMerchantId = MerchantId; // maindb.SaveChanges(); PublicFunction.BindUserMachineData(maindb, merchant.UserId, 0, 1, SnNo); return new AppResultJson() { Status = "1", Info = "绑定成功" }; } #endregion #region 商户-确认替换音箱码 [Authorize] public JsonResult ConfirmReplace(string value) { value = DesDecrypt(value); JsonData data = JsonMapper.ToObject(value); AppResultJson result = ConfirmReplaceDo(value); return Json(new AppResultJson() { Status = result.Status, Info = result.Info, Data = result.Data }); } public AppResultJson ConfirmReplaceDo(string value) { JsonData data = JsonMapper.ToObject(value); int MerchantId = int.Parse(function.CheckInt(data["MerchantId"].ToString())); //商户 int MachineId = int.Parse(function.CheckInt(data["MachineId"].ToString())); //设备 int QrCodeId = int.Parse(function.CheckInt(data["QrCodeId"].ToString())); //收款码Id Dictionary Obj = new Dictionary(); MerchantQrCode query = new MerchantQrCodeService().Query(QrCodeId) ?? new MerchantQrCode(); PosMachines machine = PosMachinesDbconn.Instance.Get(MachineId) ?? new PosMachines(); query.MerchantId = MerchantId; //商户 query.MachineId = MachineId; //设备 MachineForQrCode qrcode = new MachineForQrCode() { MerchantId = MerchantId, DataId = QrCodeId + "_" + MachineId, BindDate = DateTime.Now, SnNo = query.SnNo, MachineSnNo = machine.PosSn, }; 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 MerchantId = int.Parse(function.CheckInt(data["MerchantId"].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 == MerchantId && m.DataId.StartsWith(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])); MerchantQrCode qrCode = maindb.MerchantQrCode.FirstOrDefault(m => m.MerchantId == MachineId); if (qrCode != null) { qrCode.MerchantId = 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 } }