using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Text;
using BarCode.Web.Core;
using BarCode.Web.Core.Help;
using BarCode.Web.Core.Internal.Results;
using BarCode.Web.Domain.Values;
namespace BarCode.Web.Domain.Entitys
{
///
/// 箱信息头
/// alter table t_barcode_box_details AUTO_INCREMENT=50000;
///
[Serializable]
[Table("t_barcode_box")]
public class Box : EntityBase
{
///
/// ID
///
[Column("Id")]
public override int Id { get; set; }
///
/// 箱编号
///
[Column("BoxBillNo")]
public string BoxBillNo { get; set; } = "";
///
/// 供应商Code
///
[Column("SupplierCode")]
public string SupplierCode { get; set; } = "";
///
/// 组织Code
///
[Column("OrgCode")]
public string OrgCode { get; set; } = "";
///
/// 公司Id
///
[Column("CompanyId")]
public int CompanyId { get; set; } = 0;
///
/// 箱状态
///
[Column("Status")]
public BoxStatus Status { get; set; } = BoxStatus.NoUse;
///
/// 装箱开始时间
///
[Column("CartonBeginTime")]
public DateTime? CartonBeginTime { get; set; }
///
/// 装箱结束时间(完成装箱时间)
///
[Column("CartonEndTime")]
public DateTime? CartonEndTime { get; set; }
///
/// 装箱用户
///
[Column("CartonUserId")]
public int CartonUserId { get; set; } = 0;
///
/// 箱子创建用户
///
[Column("CreatorId")]
public int CreatorId { get; set; } = 0;
///
/// 创建时间
///
[Column("CreateTime")]
public DateTime CreateTime { get; set; } = DateTime.Now;
///
/// 箱号打印次数
///
[Column("PrintNumber")]
public int PrintNumber { get; set; } = 0;
///
/// 最新打印时间
///
[Column("PrintTime")]
public DateTime? PrintTime { get; set; }
///
/// 明细
///
public List Details { get; set; } = new List();
public void GenerateNo()
{
//用户手动输入了 就不自动生成了
if (!string.IsNullOrEmpty(this.BoxBillNo)) return;
if (this.Id.ToString().Length >= 8)
{
this.BoxBillNo = "CTN" + this.Id.ToString();
return;
}
string idStr = this.Id.ToString();
while (true)
{
idStr = "0" + idStr;
if (idStr.Length >= 8) break;
}
this.BoxBillNo = "CTN" + idStr;
}
///
/// 打印
///
public void Print()
{
this.PrintNumber++;
this.PrintTime = DateTime.Now;
}
///
/// 解绑序列号
///
///
///
/// /// 是否不计数
public Result UnSerialNumber(string serialNumber, string materialNumber,string isNotCount)
{
var detail = this.Details.FirstOrDefault(f => f.MaterialNumber.Equals(materialNumber));
if (detail == null)
return Result.ReFailure(ResultCodes.BoxMaterialNoDataError);
var serial = detail.SerialNumbers.FirstOrDefault(f => f.Equals(serialNumber));
if (serial == null && isNotCount!="1")
return Result.ReFailure(ResultCodes.BoxSerialNumberNoDataError);
detail.SerialNumbers.Remove(serialNumber);
// detail.Qty = detail.SerialNumbers.Count();
if(isNotCount=="1")
{
}
else
{
detail.Qty = detail.Qty - 1;
}
//如果数量为0了就删除这条明细
if (detail.Qty <= 0)
this.Details.Remove(detail);
//如果没有明细了 就把状态改成未使用
if (detail.Qty <= 0)
this.Status = BoxStatus.NoUse;
////如果数量为0了就删除这条明细
//if (detail.SerialNumbers.Count() <= 0)
// this.Details.Remove(detail);
////如果没有明细了 就把状态改成未使用
//if (this.Details.Count() <= 0)
// this.Status = BoxStatus.NoUse;
return Result.ReSuccess();
}
///
/// 清空
///
public void Clear()
{
this.Details.Clear();
}
///
/// 装箱
///
public Result Carton(bool isComplete, int cartonUserId)
{
if (this.Status == BoxStatus.Complete)
return Result.ReFailure(ResultCodes.CartonCompleteError);
if (this.CartonBeginTime == null) this.CartonBeginTime = DateTime.Now;
this.CartonUserId = cartonUserId;
if (isComplete == true)
{
//完成装箱
this.Status = BoxStatus.Complete;
this.CartonEndTime = DateTime.Now;
return Result.ReSuccess();
}
this.Status = BoxStatus.NoComplete;
return Result.ReSuccess();
}
///
/// 开始装箱
///
///
///
public Result BeginCarton(int cartonUserId)
{
if (this.Status == BoxStatus.Complete)
return Result.ReFailure(ResultCodes.CartonCompleteError);
if (this.CartonBeginTime == null)
this.CartonBeginTime = DateTime.Now;
if (this.CartonUserId == 0)
this.CartonUserId = cartonUserId;
this.Status = BoxStatus.NoComplete;
return Result.ReSuccess();
}
///
/// 重置
///
public void Restart()
{
this.Status = BoxStatus.NoComplete;
this.CartonEndTime = null;
}
}
}