using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;
using SqlSugar;
using WCS.BLL;
using WCS.BLL.DbModels;
using WCS.BLL.HardWare;
using WCS.BLL.Manager;
using WCS.BLL.Services.IService;
using WCS.BLL.Services.Service;
using WCS.DAL.Db;
using WCS.DAL.DbModels;
using WCS.Model;
using WCS.Model.ApiModel.AGV;
using WCS.Model.ApiModel.Home;
using WCS.Model.ApiModel.MatBaseInfo;
using WCS.Model.ApiModel.MatDetailHistoryInfo;
using WCS.Model.ApiModel.StoreInfo;
using Mode = WCS.BLL.HardWare.Mode;

namespace WCS.WebApi.Controllers
{
    /// <summary>
    /// 主页面的接口
    /// </summary>
    [ApiController]
    [Route("[controller]")]
    public class MatDetailHistoryInfoController : ControllerBase
    {
        public MatDetailHistoryInfoController()
        {

        }

        [Route("getMatDetailHistoryInfos")]
        [HttpPost(Name = "getMatDetailHistoryInfos")]
        public async Task<PageQueryResponse<MatDetailHistoryInfo>> getMatDetailHistoryInfos(GetMatDetailHistoryInfosRequest request)
        {
            try
            {
                var recordsQueryable = DbHelp.db.Queryable<MatDetailHistoryInfo>()
                                                .WhereIF(request.RecordType != null, t => t.RecordType == request.RecordType)
                                                .WhereIF(request.FunctionType != null, t => t.FunctionType == request.FunctionType)
                                                .WhereIF(!string.IsNullOrEmpty(request.ShelfCode), t => t.ShelfCode.Contains(request.ShelfCode))
                                                .WhereIF(!string.IsNullOrEmpty(request.MatCode), t => t.MatCode.Contains(request.MatCode))
                                                .WhereIF(!string.IsNullOrEmpty(request.MatName), t => t.MatName.Contains(request.MatName))
                                                ;

                var totalCount = await recordsQueryable.CountAsync();
                var records = await recordsQueryable
                                    .OrderByDescending(t => t.ModifyTime)
                                    .Skip((request.PageNumber - 1) * request.PageSize).Take(request.PageSize)
                                    .ToListAsync();
                //生成序号
                for (int i = 0; i < records.Count; i++)
                {
                    records[i].RowNumber = (request.PageNumber - 1) * request.PageSize + i + 1;
                }
                return new PageQueryResponse<MatDetailHistoryInfo>()
                {
                    Code = 200,
                    Message = $"success",
                    Data = new PageQueryResponseData<MatDetailHistoryInfo>()
                    {
                        TotalCount = totalCount,
                        MaxPage = request.PageSize == 0 ? 0 : (int)Math.Ceiling((decimal)totalCount / request.PageSize),
                        Count = records.Count,
                        Lists = records.ToList()
                    }
                };
            }
            catch (Exception ex)
            {
                return new PageQueryResponse<MatDetailHistoryInfo>()
                {
                    Code = 300,
                    Message = $"操作失败:{ex.Message}",
                };
            }
        }

        /// <summary>
        /// 删除-假删除
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [Route("deleteMatDetailHistoryInfo")]
        [HttpPost(Name = "deleteMatDetailHistoryInfo")]
        public async Task<ResponseBase> deleteMatDetailHistoryInfo(DeleteInfosRequest request)
        {

            try
            {
                //先查询出具体的Id
                var matDetailHistoryInfos = await DbHelp.db.Queryable<MatDetailHistoryInfo>()
                                               .Where(t => request.needDeleteIds.Contains(t.Id))
                                               .ToListAsync();
                //执行删除
                var deleteRows = await DbHelp.db.Deleteable(matDetailHistoryInfos).ExecuteCommandAsync();
                return new ResponseCommon<Object>
                {
                    Code = 200,
                    Message = $"已删除{deleteRows}条数据！",
                    Data = null
                };
            }
            catch (Exception ex)
            {
                var response = new ResponseCommon<Object>
                {
                    Code = 300,
                    Message = $"操作失败:{ex.Message}",
                    Data = null
                };
                return response;
            }

        }

    }
}
