username@email.com
2024-09-09 e8fd9aa8a76c638991e60544ccab53e2e5bd5b6a
CoreCms.Net.Web.Admin/Controllers/Distribution/CoreCmsDistributionController.cs
@@ -1,7 +1,7 @@
/***********************************************************************
 *            Project: CoreCms
 *            Project: baifenBinfa
 *        ProjectName: 百分兵法管理系统                               
 *                Web: hhtp://chuanyin.com
 *                Web: http://chuanyin.com
 *             Author:                                        
 *              Email:                               
 *         CreateTime: 202403/02   
@@ -9,6 +9,7 @@
 ***********************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Linq.Expressions;
@@ -26,11 +27,14 @@
using CoreCms.Net.Utility.Extensions;
using CoreCms.Net.Utility.Helper;
using CoreCms.Net.Web.Admin.Infrastructure;
using Essensoft.Paylink.Alipay.Domain;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis.Operations;
using NPOI.SS.Formula.Functions;
using SqlSugar;
using ZstdSharp.Unsafe;
namespace CoreCms.Net.Web.Admin.Controllers
{
@@ -52,6 +56,8 @@
        private readonly ICoreCmsAreaServices _areaServices;
        private readonly ICoreCmsSettingServices _coreCmsSettingServices;
        private readonly IUnitOfWork _unitOfWork;
         private readonly ICoreCmsStoreServices _coreCmsStoreServices;
        private readonly ICoreCmsClerkServices _coreCmsClerkServices;
        /// <summary>
@@ -64,7 +70,9 @@
            ICoreCmsUserServices userServices,
            ICoreCmsAreaServices areaServices,
            ICoreCmsSettingServices coreCmsSettingServices,
            IUnitOfWork unitOfWork)
            IUnitOfWork unitOfWork,
            ICoreCmsStoreServices coreCmsStoreServices,
            ICoreCmsClerkServices coreCmsClerkServices)
        {
            _webHostEnvironment = webHostEnvironment;
            _coreCmsDistributionServices = coreCmsDistributionServices;
@@ -74,6 +82,9 @@
            _areaServices = areaServices;
            _coreCmsSettingServices = coreCmsSettingServices;
            _unitOfWork = unitOfWork;
            _coreCmsStoreServices = coreCmsStoreServices;
            _coreCmsClerkServices= coreCmsClerkServices;
        }
        #region 获取列表============================================================
@@ -133,7 +144,7 @@
            //经销商名称 nvarchar
            var name = Request.Form["name"].FirstOrDefault();
            if (!string.IsNullOrEmpty(name)) where = where.And(p => p.name.Contains(name));
            //分销等级 int
            //经销等级 int
            var gradeId = Request.Form["gradeId"].FirstOrDefault().ObjectToInt(0);
            if (gradeId > 0) where = where.And(p => p.gradeId == gradeId);
            //用户等级 int
@@ -307,7 +318,7 @@
        {
            var jm = new AdminUiCallBack();
            var user = await _userServices.QueryByClauseAsync(p => p.mobile == entity.registeredUserMobile);
            var user = await _userServices.QueryByClauseAsync(p => p.id == entity.userId);
            if (user == null)
            {
                jm.msg = "提交的【已注册手机号】未查询到注册用户";
@@ -317,27 +328,130 @@
            var isDistribution = await _coreCmsDistributionServices.ExistsAsync(p => p.userId == user.id);
            if (isDistribution)
            {
                jm.msg = "提交的【已注册手机号】已经是经销商了。";
                jm.msg = "提交的用户已经有经销商记录,请找到相关项进行审核";
                return jm;
            }
            var model = new CoreCmsDistribution();
            model.userId = user.id;
            model.mobile = entity.registeredUserMobile;
            model.userId = user.id;
            model.name = entity.name;
            model.gradeId = entity.gradeId;
            model.mobile = string.IsNullOrEmpty(entity.mobile) ? user.mobile : entity.mobile;
            model.weixin = entity.weixin;
            model.qq = entity.qq;
            model.profession = entity.profession;
            model.storeCoordinate = entity.storeCoordinate;
            model.storeLogo = entity.storeBanner;
            model.storeName= entity.storeName;
            model.sales=entity.sales;
            model.areaCode=entity.areaCode;
            model.address=entity.address;
            model.verifyStatus = entity.verifyStatus;
            model.createTime = DateTime.Now;
            model.isDelete = false;
            if (model.verifyStatus == (int)GlobalEnumVars.DistributionVerifyStatus.VerifyYes) model.verifyTime = DateTime.Now;
            try
            {
                _unitOfWork.BeginTran();
                if (model.verifyStatus == (int)GlobalEnumVars.DistributionVerifyStatus.VerifyYes)
                {
                    model.verifyTime = DateTime.Now;
                    //老模型未通过审核,新模型未通过审核,那么为审核通过过程
                    var allConfigs = await _coreCmsSettingServices.GetConfigDictionaries();
                    var kc = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.DictionaryUserGroupKey);
                    var group = await _userGradeServices.GetUserGradeByValueKey(kc);
                    if (group != null)
                    {
            var id = await _coreCmsDistributionServices.InsertAsync(model);
                        //关联经销商用户等级
                        if (user != null)
                        {
                            user.grade = group.id;
                            user.updataTime = DateTime.Now;
                            await _userServices.UpdateAsync(user);
            jm.code = id > 0 ? 0 : 1;
            jm.msg = id > 0 ? GlobalConstVars.CreateSuccess : GlobalConstVars.CreateFailure;
                        }
                    }
                    //处理店铺
                    if (entity.profession == "文具店")
                    {
                        //新建店铺名称
                        var store = await _coreCmsStoreServices.QueryByClauseAsync(x => x.userID == model.userId);
                        if (store != null)
                        {
                            //存在店铺
                            store.coordinate = entity.storeCoordinate;
                            store.storeName = entity.storeName;
                            //用logo代替门头照片
                            store.logoImage = model.storeLogo;
                            store.en = true;
                            store.updateTime = DateTime.Now;
                            await _coreCmsStoreServices.UpdateAsync(store);
                        }
                        else
                        {
                            var areaCode = model.areaCode.Split(',').OrderByDescending(x => x).FirstOrDefault();
                            var db = _unitOfWork.GetDbClient();
                            string[] lat = [];
                            if (!string.IsNullOrEmpty(model.storeCoordinate))
                                lat = model.storeCoordinate.Split(",");
                            //新建地铺
                            store = new CoreCmsStore
                            {
                                userID = model.userId,
                                address = model.address,
                                areaId = areaCode.ToInt32OrDefault(510000),
                                storeName = model.storeName,
                                coordinate = model.storeCoordinate,
                                createTime = DateTime.Now,
                                mobile = model.mobile,
                                linkMan = user.nickName,
                            };
                            if (lat.Length == 2)
                            {
                                store.latitude = lat[0];
                                store.longitude = lat[1];
                            }
                            var storeid = await _unitOfWork.GetDbClient().Insertable(store).ExecuteReturnIdentityAsync();
                            //将自己设置为店员
                            CoreCmsClerk clerk = new CoreCmsClerk()
                            {
                                createTime = DateTime.Now,
                                storeId = storeid,
                                userId = entity.userId,
                            };
                            await _coreCmsClerkServices.InsertAsync(clerk);
                        }
                    }
                }
                await _coreCmsDistributionServices.InsertAsync(model);
                _unitOfWork.CommitTran();
            }
            catch (Exception)
            {
                _unitOfWork.RollbackTran();
                throw;
            }
            jm.code = 1;
            jm.msg =  GlobalConstVars.CreateSuccess ;
            return jm;
        }
        #endregion
@@ -405,20 +519,24 @@
            oldModel.name = entity.name;
            oldModel.gradeId = entity.gradeId;
            oldModel.mobile = entity.mobile;
            oldModel.weixin = entity.weixin;
            oldModel.qq = entity.qq;
            //oldModel.weixin = entity.weixin;
            //oldModel.qq = entity.qq;
            try
            {
                _unitOfWork.BeginTran();
                if (oldModel.verifyStatus != (int)GlobalEnumVars.DistributionVerifyStatus.VerifyYes && entity.verifyStatus == (int)GlobalEnumVars.DistributionVerifyStatus.VerifyYes)
                {
                    //老模型未通过审核,新模型未通过审核,那么为审核通过过程
                    var allConfigs = await _coreCmsSettingServices.GetConfigDictionaries();
                    var kc = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.DictionaryUserGroupKey);
                    var olduser = await _userServices.QueryByIdAsync(oldModel.userId);
                    var group = await _userGradeServices.GetUserGradeByValueKey(kc);
                    if (group != null)
                    {
                        var olduser = await _userServices.QueryByIdAsync(oldModel.userId);
                        //关联经销商用户等级
                        if (olduser != null)
                        {
                            olduser.grade = group.id;
@@ -430,6 +548,74 @@
                    //如果是通过审核
                    oldModel.verifyTime = DateTime.Now;
                    //处理店铺
                    if(entity.profession=="文具店")
                    {
                        //新建店铺名称
                        var store = await _coreCmsStoreServices.QueryByClauseAsync(x => x.userID == oldModel.userId);
                        if(store!=null)
                        {
                            //存在店铺
                             store.coordinate= entity.storeCoordinate;
                            store.storeName = entity.storeName;
                            //用logo代替门头照片
                            store.logoImage = entity.storeLogo;
                            store.en = true;
                            store.updateTime = DateTime.Now;
                         await   _coreCmsStoreServices.UpdateAsync(store);
                        }
                        else
                        {
                            var areaCode = oldModel.areaCode.Split(',').OrderByDescending(x=>x).FirstOrDefault();
                            var db = _unitOfWork.GetDbClient();
                            string[] lat = [];
                            if (!string.IsNullOrEmpty(oldModel.storeCoordinate))
                                lat = oldModel.storeCoordinate.Split(",");
                            //新建地铺
                            store = new CoreCmsStore
                            {
                                userID = oldModel.userId,
                                address = oldModel.address,
                                areaId = areaCode.ToInt32OrDefault(510000),
                                storeName = oldModel.storeName,
                                coordinate = oldModel.storeCoordinate,
                                createTime = DateTime.Now,
                                mobile = oldModel.mobile,
                                linkMan = olduser.userName,
                            };
                            if(lat.Length==2)
                            {
                                store.latitude= lat[0];
                                store.longitude= lat[1];
                            }
                          var id= await  _unitOfWork.GetDbClient().Insertable(store).ExecuteReturnIdentityAsync();
                            //将自己设置为店员
                            CoreCmsClerk clerk = new CoreCmsClerk()
                            {
                                createTime = DateTime.Now,
                                storeId = id,
                                userId = entity.userId,
                            };
                           await  _coreCmsClerkServices.InsertAsync(clerk);
                        }
                    }
                }
                else
                if (oldModel.verifyStatus == (int)GlobalEnumVars.DistributionVerifyStatus.VerifyYes && entity.verifyStatus != (int)GlobalEnumVars.DistributionVerifyStatus.VerifyYes)
@@ -444,7 +630,7 @@
                    {
                        var olduser = await _userServices.QueryByIdAsync(oldModel.userId);
                        if (olduser != null)
                        {
                        {//处理用户等级
                            if (olduser.grade == group.id)
                            {
                                if (defgroup != null)
@@ -462,6 +648,18 @@
                            }
                        }
                        var store = await _coreCmsStoreServices.QueryByClauseAsync(x => x.userID == oldModel.userId);
                        //处理店铺  取消已有店铺的使能
                       if(store != null )
                            if(store.en== true)
                        {
                            store.en = false;
                            store.updateTime= DateTime.Now;
                          await  _coreCmsStoreServices.UpdateAsync(store);
                        }
                    }
                }
@@ -542,11 +740,76 @@
                jm.msg = GlobalConstVars.DataisNo;
                return jm;
            }
            try
            {
                _unitOfWork.BeginTran();
                    //如果是取消审核
            var bl = await _coreCmsDistributionServices.DeleteByIdAsync(entity.id);
            jm.code = bl ? 0 : 1;
            jm.msg = bl ? GlobalConstVars.DeleteSuccess : GlobalConstVars.DeleteFailure;
            return jm;
                    var allConfigs = await _coreCmsSettingServices.GetConfigDictionaries();
                    var kc = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.DictionaryUserGroupKey);
                var group = await _userGradeServices.GetUserGradeByValueKey(kc);
                var defgroup = await _userGradeServices.GetDefaultUserGrade();
                    if (defgroup != null)
                    {
                        var olduser = await _userServices.QueryByIdAsync(model.userId);
                        if (olduser != null)
                        {//处理用户等级
                            if (olduser.grade == group.id)
                            {
                                if (defgroup != null)
                                {
                                    olduser.grade = defgroup.id;
                                }
                                else
                                {
                                    olduser.grade = 0;
                                }
                                olduser.updataTime = DateTime.Now;
                                await _userServices.UpdateAsync(olduser);
                            }
                        }
                        var store = await _coreCmsStoreServices.QueryByClauseAsync(x => x.userID == model.userId);
                        //处理店铺  取消已有店铺的使能
                       if(store != null )
                        {
                        // var clerk= await _unitOfWork.GetDbClient().Queryable<CoreCmsClerk>().Where(x => x.storeId == store.id).ToListAsync();
                        //删除店铺员工
                         await _unitOfWork.GetDbClient().Deleteable<CoreCmsClerk>().Where(x=>x.storeId==store.id).ExecuteCommandAsync();
                        //删除店铺
                        await _coreCmsStoreServices.DeleteByIdAsync(store.id);
                    }
                    }
                var  bl = await _coreCmsDistributionServices.DeleteByIdAsync(entity.id);
                _unitOfWork.CommitTran();
                jm.code = bl ? 0 : 1;
                    jm.msg = bl ? GlobalConstVars.DeleteSuccess : GlobalConstVars.DeleteFailure;
                    return jm;
            }
            catch (Exception)
            {
                _unitOfWork.RollbackTran();
                jm.code = 1;
                jm.msg = GlobalConstVars.DeleteFailure;
                return jm;
            }
        }
@@ -587,5 +850,44 @@
        }
        #endregion
        #region 预览数据============================================================
        // POST: Api/CoreCmsDistribution/GetDetailsAchievement/10
        /// <summary>
        ///     预览数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("预览数据")]
        public async Task<AdminUiCallBack> GetDetailsAchievement([FromBody] FMIntId entity)
        {
            var jm = new AdminUiCallBack();
            var model = await _coreCmsDistributionServices.QueryByIdAsync(entity.id);
            if (model == null)
            {
                jm.msg = "不存在此信息";
                return jm;
            }
            var  distributionRankingDTOs = _coreCmsDistributionServices.QueryAchievement(model.userId);
            jm.code = 0;
            jm.data = distributionRankingDTOs;
            return jm;
        }
        #endregion
    }
}