using CRM.Core.BLL.EventBus.Events; using CRM.Core.BLL.Util; using CRM.Core.DTO; using CRM.Core.Model.Entity; using System; using System.Collections.Generic; using System.Linq; using WX.CRM.Common; namespace CRM.Core.BLL.Wx { public class WX_OrderActive_BL : DbContextRepository { private readonly WX_PRODUCT_BL _product = new WX_PRODUCT_BL(); private readonly WX_PRODUCTGIFT_BL _gift = new WX_PRODUCTGIFT_BL(); private readonly CACHE_BL cache_BL = new CACHE_BL(); public List GetList(string productCode, string companyCode, DateTime orderCdate) { using (var db = new zxdContext()) { var queryData = db.Wx_OrderActive.AsQueryable(); if (!string.IsNullOrWhiteSpace(productCode)) { queryData = queryData.Where(m => m.ProductCode == productCode); } if (!string.IsNullOrWhiteSpace(companyCode)) { queryData = queryData.Where(m => m.CompanyCode == companyCode); } //queryData = queryData.Where(m => m.StartTime <= orderCdate && orderCdate <= m.EndTime); return queryData.OrderBy(m => m.IsFollowOrder).ToList(); } } public List GetListByManual(string productCode, string companyCode) { using (var db = new zxdContext()) { var queryData = db.Wx_OrderActive.AsQueryable(); if (!string.IsNullOrWhiteSpace(productCode)) { queryData = queryData.Where(m => m.ProductCode == productCode); } if (!string.IsNullOrWhiteSpace(companyCode)) { queryData = queryData.Where(m => m.CompanyCode == companyCode); } return queryData.ToList(); } } public WX_OrderActive GetModelById(int id) { using (var db = new zxdContext()) { var queryData = db.Wx_OrderActive.AsQueryable(); return queryData.Where(m => m.Id == id).SingleOrDefault(); } } public retMsg Add(string productCode, int channel, string activeCode, int isfolloworder, string gifType) { var product = _product.GetSubProduct(productCode); if (product == null) return new retMsg() { result = false, retcode = 100, retmsg = "notExists" }; var giftProduct = _product.GetSubProduct(activeCode); if (giftProduct == null) return new retMsg() { result = false, retcode = 100, retmsg = "notExists" }; if (giftProduct.PRICE != 0) { return new retMsg() { result = false, retcode = 101, retmsg = "wrongProduct" }; } if (product.PRICE == 0 && giftProduct.PRICE == 0) { return new retMsg() { result = false, retcode = 102, retmsg = "免费产品不能赠送其他免费产品" }; } //var mm = Get(m => m.ProductCode == productCode && m.IsFollowOrder == isfolloworder); //if (mm != null) //{ // return new retMsg() { result = false, retcode = 104, retmsg = "位置必须严格按照1,2,3,4" }; //} if (gifType.Contains("[Order]]") && isfolloworder > 2) { return new retMsg() { result = false, retcode = 102, retmsg = "订单赠送只能在位置1、2中进行勾选。" }; } var allist = GetList(M => M.ProductCode == productCode).OrderBy(m => m.IsFollowOrder); var companyList = cache_BL.GetCompanyCode(); var company = companyList.FirstOrDefault(p => p.Min <= channel && p.Max >= channel); var companyCode = company.CompanyCode; var isCf = Get(m => m.CompanyCode == companyCode && m.ProductCode == productCode && m.ActiveCode == activeCode);//赠送产品不能重复 if (isCf != null) { return new retMsg() { result = false, retcode = 103, retmsg = "赠送产品不能重复!" }; } var model = new WX_OrderActive(); model.ProductId = product.PRODUCTID; model.ProductCode = product.PRODUCTCODE; model.ProductName = product.SUBPRODUCTNAME; model.ProductType = Convert.ToInt32(product.PRODUCTTYPE); model.StartTime = DateTime.Now; model.EndTime = model.StartTime.AddYears(50); model.ActiveCode = activeCode; model.ActiveType = 1; model.CompanyCode = companyCode; model.MinCount = 1; model.DonateDay = 0; model.IsFollowOrder = isfolloworder; model.giftype = gifType; model.activename = giftProduct.SUBPRODUCTNAME;//赠送产品名称 var result = Add(model) > 0; model.isdelete = model.isdelete.HasValue ? model.isdelete : 0; Common.EventBus.EventBus.Instance.Publish(new AddOrUpdateOrderActiveEvent(model));//推送数据下去 return new retMsg() { result = result, retcode = 200, retmsg = "success" }; } public retMsg Update(int id, int isfolloworder, string gifType) { var entry = Get(m => m.Id == id); if (entry == null) { return new retMsg() { result = false, retcode = 102, retmsg = "找不到这个赠送信息!" }; } var giftProduct = _product.GetSubProduct(entry.ActiveCode); entry.activename = giftProduct.SUBPRODUCTNAME;//赠送产品名称 if (entry.IsFollowOrder != isfolloworder)//如果位置发生了变化,那么需要修改数据 { var giflist = _gift.GetList(m => m.activeid == entry.Id); foreach (var item in giflist) { item.Type = isfolloworder; if (_gift.Update(item)) Common.EventBus.EventBus.Instance.Publish(new AddOrUpdateGiftEvent(item));//推送数据下去 } } entry.IsFollowOrder = isfolloworder; entry.giftype = gifType; var result = Update(entry); entry.isdelete = entry.isdelete.HasValue ? entry.isdelete : 0; Common.EventBus.EventBus.Instance.Publish(new AddOrUpdateOrderActiveEvent(entry));//推送数据下去 return new retMsg() { result = result, retcode = 200, retmsg = "success" }; } public retMsg Delete(string productCode, int channel, string activeCode) { var companyList = cache_BL.GetCompanyCode(); var company = companyList.FirstOrDefault(p => p.Min <= channel && p.Max >= channel); var companyCode = company.CompanyCode; var product = _product.GetSubProduct(productCode); if (product == null) return new retMsg() { result = false, retcode = 100, retmsg = "para" }; var active = Get(p => p.ProductCode == productCode && p.CompanyCode == companyCode && p.ActiveCode == activeCode); var hasGift = _gift.Exists(p => p.activeid == active.Id); if (hasGift) return new retMsg() { result = false, retcode = 300, retmsg = "exists" }; var entr = Get(p => p.ProductCode == productCode && p.CompanyCode == companyCode && p.ActiveCode == activeCode); Delete(entr); entr.isdelete = 1; Common.EventBus.EventBus.Instance.Publish(new AddOrUpdateOrderActiveEvent(entr));//推送数据下去 return new retMsg() { result = true, retcode = 200, retmsg = "success" }; } } }