using DOAN.Model.MES.base_;
using DOAN.Model;
using DOAN.Model.MES.product;
using DOAN.Model.MES.product.Dto;
using DOAN.Service.MES.product.IService;
using Infrastructure.Attribute;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DOAN.Repository;
using DOAN.Model.MES.base_.Dto;
using Mapster;
using Microsoft.AspNetCore.Authentication;
namespace DOAN.Service.MES.product
{
///
/// 生产工单Service业务层处理
///
[AppService(ServiceType = typeof(IProWorkorderScheduleService), ServiceLifetime = LifeTime.Transient)]
public class ProWorkorderScheduleService : BaseService, IProWorkorderScheduleService
{
///
/// 查询生产工单列表
///
///
///
public PagedInfo GetList(ProWorkorderQueryDto parm)
{
if (parm.WorkorderDate != null && parm.WorkorderDate.Length > 0)
{
parm.WorkorderDate[0] = parm.WorkorderDate[0].Date;
parm.WorkorderDate[1] = parm.WorkorderDate[1].Date;
}
var predicate = Expressionable.Create()
.AndIF(!string.IsNullOrEmpty(parm.ProductionName), it => it.ProductionName.Contains(parm.ProductionName))
.AndIF(!string.IsNullOrEmpty(parm.ProductionCode), it => it.ProductionCode.Contains(parm.ProductionCode))
.AndIF(parm.WorkorderDate != null && parm.WorkorderDate[0] > DateTime.MinValue, it => it.WorkorderDate >= parm.WorkorderDate[0])
.AndIF(parm.WorkorderDate != null && parm.WorkorderDate[1] > DateTime.MinValue, it => it.WorkorderDate <= parm.WorkorderDate[1])
;
var response = Queryable()
.Where(predicate.ToExpression())
.OrderBy(it => it.WorkorderDate)
.ToPage(parm);
return response;
}
///
/// 获取工序
///
///
///
public List GetworkProcess(int WorkFkRouteCode)
{
var query = Context.Queryable().Where(it => it.FkWorkRoute == WorkFkRouteCode);
return Context.Queryable(query)
.LeftJoin((q, p) => q.FkWorkProcesses == p.Id)
.Select((q, p) => p)
.ToList()
.Adapt>();
}
///
/// 获取工位列表 (选中为 1 未选中为0)
///
/// 工单号(不是工单id)
/// 工序id
/// 工序绑定的工位列表
public List GetworkStation(string WorkorderId, int WorkProcessID)
{
// 获取工序下可以选择的工位
List Choices = Context.Queryable()
.Where(it => it.FkWorkProcesses == WorkProcessID)
.ToList().Adapt>();
//被选中的工位
int Selected_id = Context.Queryable()
.Where(it => it.FkWorkorderId == WorkorderId)
.Where(it => it.FkProcessId == WorkProcessID)
.Select(it => it.FkStationId)
.First();
if (Choices != null && Choices.Count() > 0)
{
foreach (BaseWorkStationDto3 choice in Choices)
{
if (choice.Id == Selected_id)
{
choice.flag = 1;
}
else
{
choice.flag = 0;
}
}
}
return Choices;
}
///
/// 修改
///
///
///
public int UpdateSelectedWorkstation(ProRelWorkorderLineBodyDto lineBodyDto)
{
int result = Context.Updateable()
.SetColumns(it => new ProRelWorkorderLineBody() { FkStationId = lineBodyDto.FkStationId })
.Where(it => it.FkWorkorderId == lineBodyDto.FkWorkorderId)
.Where(it => it.FkProcessId == lineBodyDto.FkProcessId).ExecuteCommand();
if (result == 0)
{
ProRelWorkorderLineBody lineBody = new ProRelWorkorderLineBody();
lineBody.FkStationId = lineBodyDto.FkStationId;
lineBody.FkWorkorderId = lineBodyDto.FkWorkorderId;
lineBody.FkProcessId = lineBodyDto.FkProcessId;
lineBody.CreatedTime = DateTime.Now;
result = Context.Insertable(lineBody).ExecuteCommand();
}
return result;
}
///
/// 传入工单号 默认选中
///
///
///
public int DefaultSelected(string WorkorderId)
{
int result = 0;
// 查询工单下单 工艺路线BaseRelWorkRouteProcesses
string RouteCode = Context.Queryable().Where(it => it.Workorder == WorkorderId).Select(it => it.LineCode).First();
var query = Context.Queryable().Where(it => it.Code == RouteCode);
int[] Processes_id = Context.Queryable(query).InnerJoin((q, r) => q.Id == r.FkWorkRoute)
.Select((q, r) => r.FkWorkProcesses).ToArray();
var linqs = Context.Queryable();
List inserts = new List();
foreach (var process in Processes_id)
{
int station = linqs
.Where(it => it.FkWorkProcesses == process)
.Select(it => it.Id).First();
ProRelWorkorderLineBody lineBody = new ProRelWorkorderLineBody();
lineBody.FkWorkorderId = WorkorderId;
lineBody.FkProcessId = process;
lineBody.FkStationId = station;
lineBody.CreatedTime = DateTime.Now;
inserts.Add(lineBody);
}
UseTran2(() =>
{
Context.Deleteable().Where(it => it.FkWorkorderId == WorkorderId);
result = Context.Insertable(inserts).ExecuteCommand();
});
return result;
}
}
}