using Admin.Core.IService;
using Admin.Core.Service;
using Admin.Core.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Admin.Core.Common;
using System.Linq;
using S71500.Impl;
using Admin.Core.Model.ViewModels;
using log4net;
using Npgsql.TypeHandlers;
namespace Admin.Core.Api
{
///
/// 小料料仓类型防差错
///
[Route("api/[controller]/[action]")]
[ApiController]
[Authorize(Permissions.Name)]
[AllowAnonymous]
public class xl_materialController : BaseApiController
{
private static readonly log4net.ILog log = LogManager.GetLogger(typeof(xl_materialServices));
int i = 0;
///
/// 物料服务
///
private readonly Ixl_materialServices _xl_materialService;
private readonly IPmt_BinServices _binServices;
private readonly IXlPlanServices _xlPlanServices;
#region 构造方法
///
/// 构造方法
///
///
public xl_materialController(Ixl_materialServices xl_materialServices, IPmt_BinServices binServices,
IXlPlanServices xlPlanServices)
{
_xl_materialService = xl_materialServices;
_binServices = binServices;
_xlPlanServices = xlPlanServices;
_xlPlanServices = xlPlanServices;
}
#endregion
#region 根据扫描的二维码查询小料物料
///
/// 根据扫描的二维码查询小料物料
///
/// 物料条码信息
///
[HttpGet]
public async Task> GetByMaterialCode(string code)
{
if (!code.IsNotEmptyOrNull())
{
return Failed("传入参数为空!");
}
var query = await _xl_materialService.GetByMaterialCode(code.Trim());
log.Info(query == null);
if (query == null)
{
return Failed("查询失败!");
}
return Success(query);
}
#endregion
#region 根据扫描的二维码查询料仓
///
/// 根据扫描的二维码查询料仓
///
/// 料仓条码信息
///
[HttpGet]
public async Task> GetByBinCode(string code)
{
BinView view=new BinView();
log.Info(code);
if (!code.IsNotEmptyOrNull())
{
return Failed("传入参数为空!");
}
var binList = await _binServices.QueryAsync();
var bin = binList.FirstOrDefault(d=>d.Bin_Code.Trim() == code.Trim());
if (bin == null) return Failed("未查询到桶信息!");
var _materialList = await _xl_materialService.QueryAsync(d => d.IsEnable == "是");
xl_material mater = _materialList.Where(d => d.ID == bin.Material_ID).Take(1).FirstOrDefault(d => d.ID == bin.Material_ID);
if (_materialList == null) return Failed("未查询到物料信息!");
view.Bin_Serial = bin.Bin_Serial;
view.Bin_Name = bin.Bin_Name;
view.Bin_Code = bin.Bin_Code;
view.Material_ID = mater.Material_code;
view.Material_Name = mater.Material_name;
view.Material_code = mater.Material_code;
return Success(view);
}
#endregion
#region 执行PLC
///
///执行PLC
///
/// 料仓排序Bin_Serial
/// 匹配状态:条码匹配写“1”,不匹配时写“2”
/// 物料编码
/// 物料重量
/// 物料二维码
///
[HttpGet]
public async Task> ExecPlcCode(int code, int state,string materialCode,decimal weight,string? barCode)
{
//log.Info(code + "&" + state);
if (!code.IsNotEmptyOrNull())
{
return Failed("料仓条码信息传入参数为空!");
}
if (!state.IsNotEmptyOrNull())
{
return Failed("匹配状态传入参数为空!");
}
if (!materialCode.IsNotEmptyOrNull())
{
return Failed("匹配状态传入参数为空!");
}
if (!weight.IsNotEmptyOrNull())
{
return Failed("匹配状态传入参数为空!");
}
var result = await _xl_materialService.ExecPlcState(code, state, materialCode, weight, barCode);
if (result.success)
{
return result;
}
return Failed(result.msg);
}
#endregion
#region 检量称扫码绑定桶(新加)
///
/// 检量称扫码绑定桶(新加)
///
/// 桶条码
///
[HttpGet]
public async Task> BindBarrel(string code)
{
if (!code.IsNotEmptyOrNull())
return Failed("桶条码信息传入参数为空!");
var result = await _xl_materialService.BindBarrel(code.Trim());
if (result == 0)
return Failed("未查询到正在执行的计划!");
if (result == 1)
return Failed("未查询到桶!");
if (result == 2)
return Failed($"未查询到[{code}]桶信息!");
if (result == 3)
return Failed("该桶已经绑定物料,请联系系统管理员处理!");
if (result == 6)
return Success(true);
if (result == 7)
return Failed("称重未完成!");
if (result == 8)
return Failed("PLC连接失败,请检查PLC或者联系管理员处理!");
if (result == 10)
return Failed("未知异常,请查看错误日志!");
if (result == 99)
return Failed("保存处理数据失败!");
if (result == 100)
return Failed("处理中....!");
if (result == 110)
return Failed("执行批次未获取!");
return Failed("执行失败!");
}
#endregion
#region 获取当前正在称量的物料
///
/// 获取当前正在称量的物料
///
///
[HttpGet]
public Task> GetCurrentMaterial()
{
MessageModel ms=new MessageModel();
return Task.FromResult(ms);
}
#endregion
#region 获取所有小料计划订单
///
/// 获取所有小料计划订单
///
///
[HttpGet]
public async Task>> GetXlPlanInfo()
{
MessageModel> message=new MessageModel>();
var list= await _xlPlanServices.GetAllXlPlanInfo();
if (list.Count>0)
{
message.success = true;
message.data = list;
}
else
{
message.success = false;
message.msg = "未查询到计划数据!";
}
return message;
}
#endregion
#region 料框防错验证——只验证单料
///
/// 料框防错验证——只验证单料
///
///
[HttpGet]
public async Task> MaterialVerification(string barCode)
{
MessageModel message = await _xl_materialService.MaterialVerification(barCode);
return message;
}
#endregion
///
/// 小料同步MES物料信息
///
///
[HttpGet]
public async Task> xlDownLoadMateralInfo(string materialCode, string materialName)
{
try
{
var result = await _xl_materialService.xlInsertMaterial(materialCode, materialName);
if (result)
{
return Success(result);
}
}
catch (Exception ex)
{
}
return Failed("执行失败!");
}
}
}