username@email.com
2024-09-09 e8fd9aa8a76c638991e60544ccab53e2e5bd5b6a
CoreCms.Net.Web.WebApi/Controllers/baifenbingfa/DistributorGoodsController.cs
@@ -1,8 +1,32 @@
using CoreCms.Net.Model.Entities.baifenbingfa.jon;
using AutoMapper;
using CoreCms.Net.Auth.HttpContextUser;
using CoreCms.Net.Caching.AutoMate.RedisCache;
using CoreCms.Net.Configuration;
using CoreCms.Net.DTO.ComponentsDTO;
using CoreCms.Net.Filter;
using CoreCms.Net.IRepository.UnitOfWork;
using CoreCms.Net.IServices;
using CoreCms.Net.IServices.baifenbingfa;
using CoreCms.Net.Model.Entities;
using CoreCms.Net.Model.Entities.baifenbingfa.jon;
using CoreCms.Net.Model.Entities.Expression;
using CoreCms.Net.Model.FromBody;
using CoreCms.Net.Model.ViewModels.UI;
using CoreCms.Net.Services;
using CoreCms.Net.Services.Basic;
using CoreCms.Net.Utility.Extensions;
using CoreCms.Net.Utility.Helper;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Nest;
using Newtonsoft.Json;
using SqlSugar;
using Swashbuckle.AspNetCore.Annotations;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Threading.Tasks;
namespace CoreCms.Net.Web.WebApi.Controllers.baifenbingfa
@@ -13,23 +37,468 @@
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    [DPremisssionFilter]//经销商访问控制
    public class DistributorGoodsController : ControllerBase
    {
        private readonly IHttpContextUser _user;
        private readonly IUnitOfWork _worke;
        private readonly ICoreCmsBrandServices _brandServices;
        private readonly ICoreCmsGoodsCategoryExtendServices _categoryExtendServices;
        private readonly ICoreCmsGoodsCategoryServices _coreCmsGoodsCategoryServices;
        private readonly ICoreCmsGoodsServices _coreCmsGoodsServices;
        private readonly ICoreCmsGoodsGradeServices _goodsGradeServices;
        private readonly ICoreCmsGoodsParamsServices _goodsParamsServices;
        private readonly ICoreCmsGoodsTypeSpecServices _goodsTypeSpecServices;
        private readonly ICoreCmsLabelServices _labelServices;
        private readonly ICoreCmsProductsServices _productsServices;
        private readonly ICoreCmsSettingServices _settingServices;
        private readonly ICoreCmsGoodsTypeSpecValueServices _typeSpecValueServices;
        private readonly ICoreCmsUserGradeServices _userGradeServices;
        private readonly ICoreCmsProductsDistributionServices _productsDistributionServices;
        private readonly IWebHostEnvironment _webHostEnvironment;
        private readonly IRedisOperationRepository _redisOperationRepository;
        private readonly ICoreCmsGoodsCategoryExtendServices _goodsCategoryExtendServices;
        /// <summary>
        ///
        ///     构造函数
        /// </summary>
        public DistributorGoodsController(IWebHostEnvironment webHostEnvironment
            , ICoreCmsGoodsServices coreCmsGoodsServices
            , ICoreCmsSettingServices settingServices
            , ICoreCmsBrandServices brandServices
            , ICoreCmsGoodsCategoryServices coreCmsGoodsCategoryServices
            , ICoreCmsUserGradeServices userGradeServices
            , ICoreCmsGoodsParamsServices goodsParamsServices
            , ICoreCmsGoodsTypeSpecValueServices typeSpecValueServices
            , ICoreCmsGoodsGradeServices goodsGradeServices
            , ICoreCmsProductsServices productsServices
            , ICoreCmsGoodsCategoryExtendServices categoryExtendServices
            , ICoreCmsLabelServices labelServices
            , ICoreCmsProductsDistributionServices productsDistributionServices
            , ICoreCmsGoodsTypeSpecServices goodsTypeSpecServices,
            IRedisOperationRepository redisOperationRepository,
            ICoreCmsGoodsCategoryExtendServices goodsCategoryExtendServices
            ,IHttpContextUser user)
        {
            _webHostEnvironment = webHostEnvironment;
            _coreCmsGoodsServices = coreCmsGoodsServices;
            _settingServices = settingServices;
            _brandServices = brandServices;
            _coreCmsGoodsCategoryServices = coreCmsGoodsCategoryServices;
            _userGradeServices = userGradeServices;
            _goodsParamsServices = goodsParamsServices;
            _typeSpecValueServices = typeSpecValueServices;
            _goodsGradeServices = goodsGradeServices;
            _productsServices = productsServices;
            _categoryExtendServices = categoryExtendServices;
            _labelServices = labelServices;
            _productsDistributionServices = productsDistributionServices;
            _goodsTypeSpecServices = goodsTypeSpecServices;
            _redisOperationRepository = redisOperationRepository;
            _goodsCategoryExtendServices = goodsCategoryExtendServices;
            _user = user;
        }
        /// <summary>
        /// 获取商品初始化参数
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetGoodslist")]
        [SwaggerResponse(200, "返回数据", typeof(WebApiCallBack))]
        [SwaggerResponse(201, "Data类型", typeof(Job_jobSeting))]
        public async Task<WebApiCallBack> GetGoodslist()
        [HttpPost("GetInitParam")]
        public async Task<WebApiCallBack> GetInitParam()
        {
            var jm = new WebApiCallBack();
            //获取商品分类
            var categories = await _coreCmsGoodsCategoryServices.QueryListByClauseAsync(p => p.isShow, p => p.sort, OrderByType.Asc, true, true);
            //获取参数列表
            var paramsList = await _goodsParamsServices.QueryListByClauseAsync(p => p.id > 0, p => p.id, OrderByType.Desc, true, true);
            //获取SKU列表
            var skuList = await _goodsTypeSpecServices.QueryListByClauseAsync(p => p.id > 0, p => p.id, OrderByType.Desc, true, true);
            //获取品牌
            var brands = await _brandServices.QueryListByClauseAsync(p => p.id > 0 && p.isShow == true, p => p.id, OrderByType.Desc, true, true);
            //获取用户等级
            var userGrade = await _userGradeServices.QueryAsync();
            //获取商品经销enum
            var productsDistributionType = EnumHelper.EnumToList<GlobalEnumVars.ProductsDistributionType>();
            jm.data = new
            {
                categories = GoodsHelper.GetTree(categories, false),
                brands,
                userGrade,
                productsDistributionType,
                paramsList,
                skuList
            };
           
            jm.status = true;
            return jm;
        }
        /// <summary>
        /// 获取商品列表
        /// </summary>
        /// <returns></returns>
        [HttpPost("GetGoodsList")]
        public async Task<WebApiCallBack> GetGoodsList([FromBody] FMPageByWhereOrder entity)
        {
            var jm = new WebApiCallBack();
            var where = PredicateBuilder.True<GoodListDTO>();
            var className = string.Empty;
            if (!string.IsNullOrWhiteSpace(entity.where))
            {
                var obj = JsonConvert.DeserializeAnonymousType(entity.where, new
                {
                    priceFrom = "",
                    priceTo = "",
                    catId = "",
                    brandId = "",
                    labelId = "",
                    searchName = "",
                    areaCode = ""
                });
                if (!string.IsNullOrWhiteSpace(obj.priceFrom))
                {
                    var priceF = obj.priceFrom.ObjectToDouble(0);
                    if (priceF >= 0)
                    {
                        var f = Convert.ToDecimal(priceF);
                        where = where.And(p => p.price >= f);
                    }
                }
                if (!string.IsNullOrWhiteSpace(obj.priceTo))
                {
                    var priceT = obj.priceTo.ObjectToDouble(0);
                    if (priceT > 0)
                    {
                        var f = Convert.ToDecimal(priceT);
                        where = where.And(p => p.price <= f);
                    }
                }
                if (!string.IsNullOrWhiteSpace(obj.catId))
                {
                    var catId = obj.catId.ObjectToInt(0);
                    if (catId > 0)
                    {
                            where = where.And(p => p.goodsCategoryId==catId);
                    }
                }
                if (!string.IsNullOrWhiteSpace(obj.brandId))
                {
                    if (obj.brandId.Contains(","))
                    {
                        var brandIdsIntArray = CommonHelper.StringToIntArray(obj.brandId);
                        if (brandIdsIntArray.Any())
                        {
                            where = where.And(p => brandIdsIntArray.Contains(p.brandId));
                        }
                    }
                    else
                    {
                        var brandId = obj.brandId.ObjectToInt(0);
                        if (brandId > 0)
                        {
                            where = where.And(p => p.brandId == brandId);
                        }
                    }
                }
                if (!string.IsNullOrWhiteSpace(obj.labelId))
                {
                    where = where.And(p => (',' + p.labelIds.Trim(',') + ',').Contains(',' + obj.labelId.Trim(',') + ','));
                }
                if (!string.IsNullOrWhiteSpace(obj.searchName))
                {
                    where = where.And(p => p.name.Contains(obj.searchName));
                }
                if (!string.IsNullOrWhiteSpace(obj.areaCode))
                {
                    where = where.And(p => p.areaCode.Contains(obj.areaCode));
                }
            }
            //只获取当前用户的
            where = where.And(p => p.publisherId == _user.ID);
            //获取数据
            var list = await _coreCmsGoodsServices.QueryPageByDTOAsync(where, entity.page, entity.limit, true);
            jm.status = true;
            return jm;
        }
        /// <summary>
        /// 获取商品详情
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<WebApiCallBack> GetEdit([FromBody] FMIntId entity)
        {
            var jm = new WebApiCallBack();
            var model = await _coreCmsGoodsServices.QueryByClauseAsync(x=>x.id==entity.id&&x.publisherId==_user.ID);
            if (model == null)
            {
                jm.msg = "无权修改该商品";
                jm.status = false;
                return jm;
            }
            jm.code = 0;
            //获取商品分类
            var categories = await _coreCmsGoodsCategoryServices.QueryListByClauseAsync(p => p.isShow, p => p.sort, OrderByType.Asc, true, true);
            //获取用户等级
            var userGrade = await _userGradeServices.QueryAsync();
            //用户价格体系
            var goodsGrades = await _goodsGradeServices.QueryListByClauseAsync(p => p.goodsId == model.id);
            //货品信息
            var products = await _productsServices.QueryListByClauseAsync(p => p.goodsId == model.id && p.isDel == false);
            //扩展信息
            var categoryExtend = await _categoryExtendServices.QueryListByClauseAsync(p => p.goodsId == model.id);
            //获取商品经销enum
            var productsDistributionType = EnumHelper.EnumToList<GlobalEnumVars.ProductsDistributionType>();
            //获取参数列表
            var paramsList = await _goodsParamsServices.QueryListByClauseAsync(p => p.id > 0, p => p.id, OrderByType.Desc, true, true);
            //获取SKU列表
            var skuList = await _goodsTypeSpecServices.QueryListByClauseAsync(p => p.id > 0, p => p.id, OrderByType.Desc, true, true);
            //获取品牌
            var brands = await _brandServices.QueryListByClauseAsync(p => p.id > 0 && p.isShow == true, p => p.id, OrderByType.Desc, true, true);
            if (products != null && products.Any())
            {
                var pIds = products.Select(p => p.id).ToList();
                if (pIds.Any())
                {
                    // 获取商品经销明细
                    var pds = await _productsDistributionServices.QueryListByClauseAsync(p => pIds.Contains(p.productsId), p => p.id, OrderByType.Asc);
                    products.ForEach(p =>
                    {
                        foreach (var o in pds.Where(o => o.productsId == p.id))
                        {
                            p.levelOne = o.levelOne;
                            p.levelTwo = o.levelTwo;
                            p.levelThree = o.levelThree;
                        }
                    });
                    jm.otherData = pds;
                }
            }
            //获取参数信息
            var goodsTypeSpec = new List<CoreCmsGoodsTypeSpec>();
            var goodsParams = new List<CoreCmsGoodsParams>();
            //获取参数
            if (!string.IsNullOrEmpty(model.goodsParamsIds))
            {
                var paramsIds = Utility.Helper.CommonHelper.StringToIntArray(model.goodsParamsIds);
                goodsParams = await _goodsParamsServices.QueryListByClauseAsync(p => paramsIds.Contains(p.id));
            }
            //获取属性
            if (!string.IsNullOrEmpty(model.goodsSkuIds))
            {
                var specIds = Utility.Helper.CommonHelper.StringToIntArray(model.goodsSkuIds);
                var typeSpecs = skuList.Where(p => specIds.Contains(p.id)).ToList();
                var typeSpecValues = await _typeSpecValueServices.QueryListByClauseAsync(p => specIds.Contains(p.specId));
                typeSpecs.ForEach(p =>
                {
                    p.specValues = typeSpecValues.Where(o => o.specId == p.id).ToList();
                });
                goodsTypeSpec = typeSpecs;
            }
            jm.data = new
            {
                model,
                categories = GoodsHelper.GetTree(categories, false),
                brands,
                userGrade,
                goodsGrades,
                products,
                categoryExtend,
                goodsTypeSpec,
                goodsParams,
                productsDistributionType,
                paramsList,
                skuList
            };
            jm.status = true;
            return jm;
        }
        // POST: Admins/CoreCmsGoods/Edit
        /// <summary>
        ///     编辑提交
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("编辑提交")]
        public async Task<WebApiCallBack> DoEdit([FromBody] FMGoodsInsertModel entity)
        {
            var jm = await _coreCmsGoodsServices.UpdateAsync(entity);
             var ret=new WebApiCallBack();
            ret.status = jm.code == 0 ? true : false;
            ret.data = jm.data;
            ret.msg=jm.msg;
            return ret;
        }
        // POST: Api/CoreCmsGoods/DoCreate
        /// <summary>
        ///     创建提交
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("创建提交")]
        public async Task<WebApiCallBack> DoCreate([FromBody] FMGoodsInsertModel entity)
        {
            entity.goods.auditStatus = AuditStatusType.Pending;
            entity.goods.publisherId = _user.ID;
            var jm = await _coreCmsGoodsServices.InsertAsync(entity);
            var ret = new WebApiCallBack();
            ret.status = jm.code == 0 ? true : false;
            ret.data = jm.data;
            ret.msg = jm.msg;
            return ret;
        }
        // POST: Api/CoreCmsGoods/GetDetails/10
        /// <summary>
        ///     预览数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        [Description("预览数据")]
        public async Task<WebApiCallBack> GetDetails([FromBody] FMIntId entity)
        {
            var jm = new WebApiCallBack();
            var model = await _coreCmsGoodsServices.QueryByIdAsync(entity.id);
            if (model == null)
            {
                jm.msg = "不存在此信息";
                jm.data = entity.id;
                jm.status = false;
                return jm;
            }
            jm.code = 0;
            //获取商品分类
            var categories = await _coreCmsGoodsCategoryServices.QueryListByClauseAsync(p => p.isShow, p => p.sort, OrderByType.Asc, true, true);
            //获取用户等级
            var userGrade = await _userGradeServices.QueryAsync();
            //用户价格体系
            var goodsGrades = await _goodsGradeServices.QueryListByClauseAsync(p => p.goodsId == model.id);
            //货品信息
            var products =
                await _productsServices.QueryListByClauseAsync(p => p.goodsId == model.id && p.isDel == false);
            //扩展信息
            var categoryExtend = await _categoryExtendServices.QueryListByClauseAsync(p => p.goodsId == model.id);
            //获取商品经销enum
            var productsDistributionType = EnumHelper.EnumToList<GlobalEnumVars.ProductsDistributionType>();
            //获取参数列表
            var paramsList = await _goodsParamsServices.QueryListByClauseAsync(p => p.id > 0, p => p.id, OrderByType.Desc, true, true);
            //获取SKU列表
            var skuList = await _goodsTypeSpecServices.QueryListByClauseAsync(p => p.id > 0, p => p.id, OrderByType.Desc, true, true);
            //获取品牌
            var brands = await _brandServices.QueryListByClauseAsync(p => p.id > 0 && p.isShow == true, p => p.id, OrderByType.Desc, true, true);
            if (products != null && products.Any())
            {
                var pIds = products.Select(p => p.id).ToList();
                if (pIds.Any())
                {
                    // 获取商品经销明细
                    var pds = await _productsDistributionServices.QueryListByClauseAsync(p => pIds.Contains(p.productsId), p => p.id, OrderByType.Asc);
                    products.ForEach(p =>
                    {
                        foreach (var o in pds.Where(o => o.productsId == p.id))
                        {
                            p.levelOne = o.levelOne;
                            p.levelTwo = o.levelTwo;
                            p.levelThree = o.levelThree;
                        }
                    });
                    jm.otherData = pds;
                }
            }
            //获取参数信息
            var goodsTypeSpec = new List<CoreCmsGoodsTypeSpec>();
            var goodsParams = new List<CoreCmsGoodsParams>();
            //获取参数
            if (!string.IsNullOrEmpty(model.goodsParamsIds))
            {
                var paramsIds = Utility.Helper.CommonHelper.StringToIntArray(model.goodsParamsIds);
                goodsParams = await _goodsParamsServices.QueryListByClauseAsync(p => paramsIds.Contains(p.id));
            }
            //获取属性
            if (!string.IsNullOrEmpty(model.goodsSkuIds))
            {
                var specIds = Utility.Helper.CommonHelper.StringToIntArray(model.goodsSkuIds);
                var typeSpecs = await _goodsTypeSpecServices.QueryListByClauseAsync(p => specIds.Contains(p.id));
                var typeSpecValues = await _typeSpecValueServices.QueryListByClauseAsync(p => specIds.Contains(p.specId));
                typeSpecs.ForEach(p =>
                {
                    p.specValues = typeSpecValues.Where(o => o.specId == p.id).ToList();
                });
                goodsTypeSpec = typeSpecs;
            }
            jm.data = new
            {
                model,
                categories = GoodsHelper.GetTree(categories, false),
                brands,
                userGrade,
                goodsGrades,
                products,
                categoryExtend,
                goodsTypeSpec,
                goodsParams,
                productsDistributionType,
                paramsList,
                skuList
            };
            jm.status = true;
            return jm;
        }
    }
}