LocationService.cs 4.7 KB
using Hh.Mes.Common.log;
using Hh.Mes.Common.Request;
using Hh.Mes.Pojo.System;
using Hh.Mes.POJO.Entity;
using Hh.Mes.POJO.Response;
using Hh.Mes.Service.Repository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;

namespace Hh.Mes.Service.Warehous
{
    public class LocationService : RepositorySqlSugar<base_location>
    {
        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="pageReq"></param>
        /// <param name="model"></param>
        /// <returns></returns>
        public Response Load(PageReq pageReq, base_location model)
        {
            var list = GetLoad(model);
            var table = list;
            if (pageReq != null)
            {
                table = list.OrderByDescending(x => x.createTime).Skip((pageReq.page - 1) * pageReq.limit).Take(pageReq.limit).ToList();
            }
            var result = new Response
            {
                Result = table,
                Count = list.Count
            };
            return result;
        }

        /// <summary>
        /// 查询条件
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public List<base_location> GetLoad(base_location model)
        {
            var exp = Expressionable.Create<base_location>();
          
            if (!string.IsNullOrWhiteSpace(model.lineCode)) exp.And(x => x.lineCode==model.lineCode);
            if (model.zoneCode!=0) exp.And(x => x.zoneCode == model.zoneCode);
            if (!string.IsNullOrWhiteSpace(model.locationType)) exp.And(x => x.locationType == model.locationType);
            if (model.locationStatus!=0) exp.And(x => x.locationStatus == model.locationStatus);
 
            var wheres = exp.ToExpression();//拼接表达式
            return GetList(wheres);
        }


        /// <summary>
        /// 新增
        /// </summary>
        public dynamic Ins(base_location model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.keys = Guid.NewGuid();
                model.createBy = base.sysWebUser.Account;
                var inventory = base.Context.Queryable<base_inventory>().First(x => x.warehouseCode == model.warehouseCode);
                model.lineCode = inventory.lineCode;
                model.createTime = DateTime.Now;
                response.Status = Add(model);
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });
        }

        /// <summary>
        /// 更新
        /// </summary>
        public dynamic Upd(base_location model)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                model.updateBy = base.sysWebUser.Account;
                model.updateTime = DateTime.Now;
                response.Status = base.Context.Updateable(model).
                                       IgnoreColumns(it => new { it.createBy, it.createTime,it.lineCode }).ExecuteCommand() > 0;
                if (!response.Status) response.Message = SystemVariable.dataActionError;
                return response;
            });

        }

        /// <summary>
        /// 删除 code
        /// </summary>
        public dynamic DelByIds(string[] ids)
        {
            var response = new Response();
            return ExceptionsHelp.Instance.ExecuteT(() =>
            {
                var index = 0;
                foreach (var code in ids)
                {
                    var isExit = Context.Queryable<base_inventory>().Any(u => u.locationCode == code);//是否存在
                    if (isExit)
                    {
                        index++;
                        //提示
                        response.Code = 300;
                        response.Message += $"库位料点数据:{code}在库存里面存在数据不能删除  <br>";
                    }
                }
                if (index > 0) return response;

                foreach (var code in ids)
                {
                    var isExit = Context.Queryable<bus_agv_task>().Any(u => u.materialFrameCode == code);
                    if (isExit) return response.ResponseError($"库位料点数据:{code}在AGV任务数据里面存在数据不能删除  <br>");
                }

                var temp = Context.Deleteable<base_location>().In(x =>x.locationCode, ids).ExecuteCommand()>0;              
                if (!temp)
                {
                    response.Message = SystemVariable.dataActionError;
                }
                return response;
             
            });
        }
    }
}