LqYyjlService.cs 17 KB
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using NCC.ClayObject;
using NCC.Common.Configuration;
using NCC.Common.Core.Manager;
using NCC.Common.Enum;
using NCC.Common.Extension;
using NCC.Common.Filter;
using NCC.Common.Helper;
using NCC.Common.Model.NPOI;
using NCC.DataEncryption;
using NCC.Dependency;
using NCC.DynamicApiController;
using NCC.Extend.Entitys.Dto.LqYyjl;
using NCC.Extend.Entitys.lq_mdxx;
using NCC.Extend.Entitys.lq_yyjl;
using NCC.Extend.Interfaces.LqYyjl;
using NCC.FriendlyException;
using NCC.JsonSerialization;
using NCC.System.Entitys.Permission;
using SqlSugar;
using Yitter.IdGenerator;

namespace NCC.Extend.LqYyjl
{
    /// <summary>
    /// 预约记录服务
    /// </summary>
    [ApiDescriptionSettings(Tag = "绿纤预约记录服务", Name = "LqYyjl", Order = 200)]
    [Route("api/Extend/[controller]")]
    public class LqYyjlService : ILqYyjlService, IDynamicApiController, ITransient
    {
        private readonly ISqlSugarRepository<LqYyjlEntity> _lqYyjlRepository;
        private readonly SqlSugarScope _db;
        private readonly IUserManager _userManager;

        /// <summary>
        /// 初始化一个<see cref="LqYyjlService"/>类型的新实例
        /// </summary>
        public LqYyjlService(ISqlSugarRepository<LqYyjlEntity> lqYyjlRepository, IUserManager userManager)
        {
            _lqYyjlRepository = lqYyjlRepository;
            _db = _lqYyjlRepository.Context;
            _userManager = userManager;
        }
        #region 预约记录
        /// <summary>
        /// 获取预约记录
        /// </summary>
        /// <param name="id">参数</param>
        /// <returns></returns>
        [HttpGet("{id}")]
        public async Task<dynamic> GetInfo(string id)
        {
            var entity = await _db.Queryable<LqYyjlEntity>().FirstAsync(p => p.Id == id);
            var output = entity.Adapt<LqYyjlInfoOutput>();

            // 获取门店名称
            if (!string.IsNullOrEmpty(entity.Djmd))
            {
                var store = await _db.Queryable<LqMdxxEntity>().Where(u => u.Id == entity.Djmd).FirstAsync();
                output.djmdName = store?.Dm;
            }

            // 获取预约人姓名
            if (!string.IsNullOrEmpty(entity.Yyr))
            {
                var user = await _db.Queryable<UserEntity>().Where(u => u.Id == entity.Yyr).FirstAsync();
                output.yyrName = user?.RealName;
            }

            // 获取预约健康师姓名
            if (!string.IsNullOrEmpty(entity.Yyjks))
            {
                var healthCoach = await _db.Queryable<UserEntity>().Where(u => u.Id == entity.Yyjks).FirstAsync();
                output.yyjksName = healthCoach?.RealName;
            }

            return output;
        }
        #endregion

        #region 预约记录列表
        /// <summary>
        /// 获取预约记录列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet("")]
        public async Task<dynamic> GetList([FromQuery] LqYyjlListQueryInput input)
        {
            var sidx = input.sidx == null ? "id" : input.sidx;
            List<string> queryCzsj = input.czsj != null ? input.czsj.Split(',').ToObeject<List<string>>() : null;
            DateTime? startCzsj = queryCzsj != null ? Ext.GetDateTime(queryCzsj.First()) : null;
            DateTime? endCzsj = queryCzsj != null ? Ext.GetDateTime(queryCzsj.Last()) : null;
            List<string> queryYysj = input.yysj != null ? input.yysj.Split(',').ToObeject<List<string>>() : null;
            DateTime? startYysj = queryYysj != null ? Ext.GetDateTime(queryYysj.First()) : null;
            DateTime? endYysj = queryYysj != null ? Ext.GetDateTime(queryYysj.Last()) : null;
            List<string> queryYyjs = input.yyjs != null ? input.yyjs.Split(',').ToObeject<List<string>>() : null;
            DateTime? startYyjs = queryYyjs != null ? Ext.GetDateTime(queryYyjs.First()) : null;
            DateTime? endYyjs = queryYyjs != null ? Ext.GetDateTime(queryYyjs.Last()) : null;
            var data = await _db.Queryable<LqYyjlEntity>()
                .WhereIF(!string.IsNullOrEmpty(input.id), p => p.Id.Contains(input.id))
                .WhereIF(!string.IsNullOrEmpty(input.djmd), p => p.Djmd.Equals(input.djmd))
                .WhereIF(!string.IsNullOrEmpty(input.yyr), p => p.Yyr.Equals(input.yyr))
                .WhereIF(!string.IsNullOrEmpty(input.gklx), p => p.Gklx.Contains(input.gklx))
                .WhereIF(!string.IsNullOrEmpty(input.yytyxm), p => p.Yytyxm.Equals(input.yytyxm))
                .WhereIF(!string.IsNullOrEmpty(input.czr), p => p.Czr.Equals(input.czr))
                .WhereIF(queryCzsj != null, p => p.Czsj >= new DateTime(startCzsj.ToDate().Year, startCzsj.ToDate().Month, startCzsj.ToDate().Day, 0, 0, 0))
                .WhereIF(queryCzsj != null, p => p.Czsj <= new DateTime(endCzsj.ToDate().Year, endCzsj.ToDate().Month, endCzsj.ToDate().Day, 23, 59, 59))
                .WhereIF(!string.IsNullOrEmpty(input.gk), p => p.Gk.Equals(input.gk))
                .WhereIF(!string.IsNullOrEmpty(input.gkxm), p => p.Gkxm.Contains(input.gkxm))
                .WhereIF(!string.IsNullOrEmpty(input.yyjks), p => p.Yyjks.Equals(input.yyjks))
                .WhereIF(queryYysj != null, p => p.Yysj >= new DateTime(startYysj.ToDate().Year, startYysj.ToDate().Month, startYysj.ToDate().Day, 0, 0, 0))
                .WhereIF(queryYysj != null, p => p.Yysj <= new DateTime(endYysj.ToDate().Year, endYysj.ToDate().Month, endYysj.ToDate().Day, 23, 59, 59))
                .WhereIF(queryYyjs != null, p => p.Yyjs >= new DateTime(startYyjs.ToDate().Year, startYyjs.ToDate().Month, startYyjs.ToDate().Day, 0, 0, 0))
                .WhereIF(queryYyjs != null, p => p.Yyjs <= new DateTime(endYyjs.ToDate().Year, endYyjs.ToDate().Month, endYyjs.ToDate().Day, 23, 59, 59))
                .WhereIF(!string.IsNullOrEmpty(input.F_Status), p => p.F_Status.Equals(input.F_Status))
                .Select(it => new LqYyjlListOutput
                {
                    id = it.Id,
                    djmd = it.Djmd,
                    yyr = it.Yyr,
                    gklx = it.Gklx,
                    yytyxm = it.Yytyxm,
                    czr = it.Czr,
                    czsj = it.Czsj,
                    gk = it.Gk,
                    gkxm = it.Gkxm,
                    yyjks = it.Yyjks,
                    yysj = it.Yysj,
                    yyjs = it.Yyjs,
                    F_Status = it.F_Status,
                    yyrName = SqlFunc.Subqueryable<UserEntity>().Where(u => u.Id == it.Yyr).Select(u => u.RealName),
                    yyjksName = SqlFunc.Subqueryable<UserEntity>().Where(u => u.Id == it.Yyjks).Select(u => u.RealName),
                    djmdName = SqlFunc.Subqueryable<LqMdxxEntity>().Where(u => u.Id == it.Djmd).Select(u => u.Dm),
                })
                .MergeTable()
                .OrderBy(sidx + " " + input.sort)
                .ToPagedListAsync(input.currentPage, input.pageSize);
            return PageResult<LqYyjlListOutput>.SqlSugarPageResult(data);
        }
        #endregion

        #region 新建预约记录
        /// <summary>
        /// 新建预约记录
        /// </summary>
        /// <param name="input">参数</param>
        /// <returns></returns>
        [HttpPost("")]
        public async Task Create([FromBody] LqYyjlCrInput input)
        {
            var userInfo = await _userManager.GetUserInfo();
            var entity = input.Adapt<LqYyjlEntity>();
            entity.Id = YitIdHelper.NextId().ToString();
            entity.Czr = _userManager.UserId;
            entity.Czsj = DateTime.Now;
            entity.CreateTime = DateTime.Now;
            var isOk = await _db.Insertable(entity).IgnoreColumns(ignoreNullColumn: true).ExecuteCommandAsync();
            if (!(isOk > 0))
                throw NCCException.Oh(ErrorCode.COM1000);
        }
        #endregion

        #region 预约记录无分页列表
        /// <summary>
        /// 获取预约记录无分页列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [NonAction]
        public async Task<dynamic> GetNoPagingList([FromQuery] LqYyjlListQueryInput input)
        {
            var sidx = input.sidx == null ? "id" : input.sidx;
            List<string> queryCzsj = input.czsj != null ? input.czsj.Split(',').ToObeject<List<string>>() : null;
            DateTime? startCzsj = queryCzsj != null ? Ext.GetDateTime(queryCzsj.First()) : null;
            DateTime? endCzsj = queryCzsj != null ? Ext.GetDateTime(queryCzsj.Last()) : null;
            List<string> queryYysj = input.yysj != null ? input.yysj.Split(',').ToObeject<List<string>>() : null;
            DateTime? startYysj = queryYysj != null ? Ext.GetDateTime(queryYysj.First()) : null;
            DateTime? endYysj = queryYysj != null ? Ext.GetDateTime(queryYysj.Last()) : null;
            List<string> queryYyjs = input.yyjs != null ? input.yyjs.Split(',').ToObeject<List<string>>() : null;
            DateTime? startYyjs = queryYyjs != null ? Ext.GetDateTime(queryYyjs.First()) : null;
            DateTime? endYyjs = queryYyjs != null ? Ext.GetDateTime(queryYyjs.Last()) : null;
            var data = await _db.Queryable<LqYyjlEntity>()
                .WhereIF(!string.IsNullOrEmpty(input.id), p => p.Id.Contains(input.id))
                .WhereIF(!string.IsNullOrEmpty(input.djmd), p => p.Djmd.Equals(input.djmd))
                .WhereIF(!string.IsNullOrEmpty(input.yyr), p => p.Yyr.Equals(input.yyr))
                .WhereIF(!string.IsNullOrEmpty(input.gklx), p => p.Gklx.Contains(input.gklx))
                .WhereIF(!string.IsNullOrEmpty(input.yytyxm), p => p.Yytyxm.Equals(input.yytyxm))
                .WhereIF(!string.IsNullOrEmpty(input.czr), p => p.Czr.Equals(input.czr))
                .WhereIF(queryCzsj != null, p => p.Czsj >= new DateTime(startCzsj.ToDate().Year, startCzsj.ToDate().Month, startCzsj.ToDate().Day, 0, 0, 0))
                .WhereIF(queryCzsj != null, p => p.Czsj <= new DateTime(endCzsj.ToDate().Year, endCzsj.ToDate().Month, endCzsj.ToDate().Day, 23, 59, 59))
                .WhereIF(!string.IsNullOrEmpty(input.gk), p => p.Gk.Equals(input.gk))
                .WhereIF(!string.IsNullOrEmpty(input.gkxm), p => p.Gkxm.Contains(input.gkxm))
                .WhereIF(!string.IsNullOrEmpty(input.yyjks), p => p.Yyjks.Equals(input.yyjks))
                .WhereIF(queryYysj != null, p => p.Yysj >= new DateTime(startYysj.ToDate().Year, startYysj.ToDate().Month, startYysj.ToDate().Day, 0, 0, 0))
                .WhereIF(queryYysj != null, p => p.Yysj <= new DateTime(endYysj.ToDate().Year, endYysj.ToDate().Month, endYysj.ToDate().Day, 23, 59, 59))
                .WhereIF(queryYyjs != null, p => p.Yyjs >= new DateTime(startYyjs.ToDate().Year, startYyjs.ToDate().Month, startYyjs.ToDate().Day, 0, 0, 0))
                .WhereIF(queryYyjs != null, p => p.Yyjs <= new DateTime(endYyjs.ToDate().Year, endYyjs.ToDate().Month, endYyjs.ToDate().Day, 23, 59, 59))
                .WhereIF(!string.IsNullOrEmpty(input.F_Status), p => p.F_Status.Equals(input.F_Status))
                .Select(it => new LqYyjlListOutput
                {
                    id = it.Id,
                    djmd = it.Djmd,
                    yyr = it.Yyr,
                    gklx = it.Gklx,
                    yytyxm = it.Yytyxm,
                    czr = it.Czr,
                    czsj = it.Czsj,
                    gk = it.Gk,
                    gkxm = it.Gkxm,
                    yyjks = it.Yyjks,
                    yysj = it.Yysj,
                    yyjs = it.Yyjs,
                    F_Status = it.F_Status,
                    yyrName = SqlFunc.Subqueryable<UserEntity>().Where(u => u.Id == it.Yyr).Select(u => u.RealName),
                    yyjksName = SqlFunc.Subqueryable<UserEntity>().Where(u => u.Id == it.Yyjks).Select(u => u.RealName),
                    djmdName = SqlFunc.Subqueryable<LqMdxxEntity>().Where(u => u.Id == it.Djmd).Select(u => u.Dm),
                })
                .MergeTable()
                .OrderBy(sidx + " " + input.sort)
                .ToListAsync();
            return data;
        }
        #endregion

        #region 导出预约记录
        /// <summary>
        /// 导出预约记录
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        [HttpGet("Actions/Export")]
        public async Task<dynamic> Export([FromQuery] LqYyjlListQueryInput input)
        {
            var userInfo = await _userManager.GetUserInfo();
            var exportData = new List<LqYyjlListOutput>();
            if (input.dataType == 0)
            {
                var data = Clay.Object(await this.GetList(input));
                exportData = data.Solidify<PageResult<LqYyjlListOutput>>().list;
            }
            else
            {
                exportData = await this.GetNoPagingList(input);
            }
            List<ParamsModel> paramList =
                "[{\"value\":\"预约编号\",\"field\":\"id\"},{\"value\":\"单据门店\",\"field\":\"djmd\"},{\"value\":\"邀约人\",\"field\":\"yyr\"},{\"value\":\"顾客姓名\",\"field\":\"gkxm\"},{\"value\":\"操作人\",\"field\":\"czr\"},{\"value\":\"操作时间\",\"field\":\"czsj\"},{\"value\":\"预约体验项目\",\"field\":\"yytyxm\"},{\"value\":\"顾客类型\",\"field\":\"gklx\"},{\"value\":\"预约健康师\",\"field\":\"yyjks\"},{\"value\":\"预约开始时间\",\"field\":\"yysj\"},{\"value\":\"预约结束时间\",\"field\":\"yyjs\"},{\"value\":\"顾客\",\"field\":\"gk\"},{\"value\":\"预约状态\",\"field\":\"F_Status\"},]".ToList<ParamsModel>();
            ExcelConfig excelconfig = new ExcelConfig();
            excelconfig.FileName = "预约记录.xls";
            excelconfig.HeadFont = "微软雅黑";
            excelconfig.HeadPoint = 10;
            excelconfig.IsAllSizeColumn = true;
            excelconfig.ColumnModel = new List<ExcelColumnModel>();
            List<string> selectKeyList = input.selectKey.Split(',').ToList();
            foreach (var item in selectKeyList)
            {
                var isExist = paramList.Find(p => p.field == item);
                if (isExist != null)
                {
                    excelconfig.ColumnModel.Add(new ExcelColumnModel() { Column = isExist.field, ExcelColumn = isExist.value });
                }
            }
            var addPath = FileVariable.TemporaryFilePath + excelconfig.FileName;
            ExcelExportHelper<LqYyjlListOutput>.Export(exportData, excelconfig, addPath);
            var fileName = _userManager.UserId + "|" + addPath + "|xls";
            var output = new { name = excelconfig.FileName, url = "/api/File/Download?encryption=" + DESCEncryption.Encrypt(fileName, "NCC") };
            return output;
        }
        #endregion

        #region 批量删除预约记录
        /// <summary>
        /// 批量删除预约记录
        /// </summary>
        /// <param name="ids">主键数组</param>
        /// <returns></returns>
        [HttpPost("batchRemove")]
        public async Task BatchRemove([FromBody] List<string> ids)
        {
            var entitys = await _db.Queryable<LqYyjlEntity>().In(it => it.Id, ids).ToListAsync();
            if (entitys.Count > 0)
            {
                try
                {
                    //开启事务
                    _db.BeginTran();
                    //批量删除预约记录
                    await _db.Deleteable<LqYyjlEntity>().In(d => d.Id, ids).ExecuteCommandAsync();
                    //关闭事务
                    _db.CommitTran();
                }
                catch (Exception)
                {
                    //回滚事务
                    _db.RollbackTran();
                    throw NCCException.Oh(ErrorCode.COM1002);
                }
            }
        }
        #endregion

        #region 更新预约记录
        /// <summary>
        /// 更新预约记录
        /// </summary>
        /// <param name="id">主键</param>
        /// <param name="input">参数</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task Update(string id, [FromBody] LqYyjlUpInput input)
        {
            var entity = input.Adapt<LqYyjlEntity>();
            var isOk = await _db.Updateable(entity).IgnoreColumns(ignoreAllNullColumns: true).ExecuteCommandAsync();
            if (!(isOk > 0))
                throw NCCException.Oh(ErrorCode.COM1001);
        }
        #endregion

        #region 删除预约记录
        /// <summary>
        /// 删除预约记录
        /// </summary>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task Delete(string id)
        {
            var entity = await _db.Queryable<LqYyjlEntity>().FirstAsync(p => p.Id == id);
            _ = entity ?? throw NCCException.Oh(ErrorCode.COM1005);
            var isOk = await _db.Deleteable<LqYyjlEntity>().Where(d => d.Id == id).ExecuteCommandAsync();
            if (!(isOk > 0))
                throw NCCException.Oh(ErrorCode.COM1002);
        }
        #endregion
    }
}