using APT.BaseData.Domain.Enums;
using APT.BaseData.Domain.Msg;
using APT.Infrastructure.Core;
using APT.BaseData.Domain.ApiModel;
using APT.BaseData.Domain.Entities.FM;
using APT.BaseData.Domain.Enums;
using APT.BaseData.Domain.IServices.FM;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using APT.Infrastructure.Api;
namespace APT.BaseData.Services.Services.FM
{
    public class FMAutoPrintRecordService: CommonService, IFMAutoPrintRecordService
	{
		public FMAutoPrintRecordService(IRepository repository)
			: base(repository)
		{

		}

		/// <summary>添加打印参数</summary>
		/// <param name="printRecord"></param>
		/// <param name="paramName"></param>
		/// <param name="paramValue"></param>
		/// <param name="addPrintRecordDetails"></param>
		/// <returns></returns>
		private T_FM_AUTO_PRINT_RECORD_DETAIL AddPrintParam(T_FM_AUTO_PRINT_RECORD printRecord, string paramName, string paramValue, List<T_FM_AUTO_PRINT_RECORD_DETAIL> addPrintRecordDetails)
		{
			T_FM_AUTO_PRINT_RECORD_DETAIL printDetail = new T_FM_AUTO_PRINT_RECORD_DETAIL();
			printDetail.ORG_ID = printRecord.ORG_ID;
			printDetail.AUTO_PRINT_RECORD_ID = printRecord.ID;
			printDetail.PARAM_NAME = paramName;
			printDetail.PARAM_VALUE = paramValue;
			addPrintRecordDetails.Add(printDetail);
			return printDetail;
		}

		/// <summary>
		/// 添加打印记录
		/// </summary>
		/// <param name="param"></param>
		/// <param name="addPrintRecords"></param>
		/// <returns></returns>
		private T_FM_AUTO_PRINT_RECORD AddPrintRecord(AutoPrintParamModel param, List<T_FM_AUTO_PRINT_RECORD> addPrintRecords)
		{
			T_FM_AUTO_PRINT_RECORD record = new T_FM_AUTO_PRINT_RECORD();
			record.PRINTER_COMPUTER_NAME = param.PrinterComputerName;
			record.PRINTER_NAME = param.PrinterName;
			record.PRINT_NUMBER = param.PrintNumber;
			record.PRINT_TIME = param.PrintTime;
			record.SOURCE_ID = param.SourceId;
			record.SOURCE_TYPE = param.SourceType;
			record.TEMPLET_FILE_NAME = param.TempletFileName;
			record.REMARK = param.Remark;
			record.ORG_ID = param.OrgId;
			addPrintRecords.Add(record);
			return record;
		}

		/// <summary>
		/// 打印箱码（保存数据）
		/// </summary>
		/// <param name="id">箱码ID</param>
		/// <param name="printerId">打印机ID</param>
		public void PrintBox(string id, string printerId)
		{
			if (string.IsNullOrEmpty(id))
				throw new Exception(ErrMsg.WH_NOEXIT_BOX_ID);

			AutoPrintParamModel param = new AutoPrintParamModel();
			if (!string.IsNullOrEmpty(printerId))
			{
				var printer = this.GetEntity<T_FM_PRINTER>(printerId);
				if (printer != null)
				{
					param.PrinterComputerName = printer.PRINTER_COMPUTER_NAME;
					param.PrinterName = printer.PRINTER_NAME; 
					param.TempletFileName = printer.TEMPLET_FILE_NAME;
				}
			}
			param.SourceId = new Guid(id);
			//param.SourceType = (int)PFCodeRuleType.箱码;
			if (string.IsNullOrEmpty(param.TempletFileName))
				param.TempletFileName = "box.btw";

			PrintBox(param);
		}

		/// <summary>
		/// 打印箱码（保存数据）
		/// </summary>
		/// <param name="param">打印参数</param>
		public void PrintBox(AutoPrintParamModel param)
		{
			List<T_FM_AUTO_PRINT_RECORD> addPrintRecords = new List<T_FM_AUTO_PRINT_RECORD>();
			List<T_FM_AUTO_PRINT_RECORD_DETAIL> addPrintRecordDetails = new List<T_FM_AUTO_PRINT_RECORD_DETAIL>();

			PrintBox(param, addPrintRecords, addPrintRecordDetails);

			if (addPrintRecords.Any())
				this.BantchAddEntity(addPrintRecords);
			if (addPrintRecordDetails.Any())
				this.BantchAddEntity(addPrintRecordDetails);
		}
		
		/// <summary>
		/// 打印箱码（不保存数据）
		/// </summary>
		/// <param name="param">打印参数</param>
		/// <param name="addPrintRecords">自动打印记录</param>
		/// <param name="addPrintRecordDetails">自动打印记录明细</param>
		public void PrintBox(AutoPrintParamModel param, List<T_FM_AUTO_PRINT_RECORD> addPrintRecords, List<T_FM_AUTO_PRINT_RECORD_DETAIL> addPrintRecordDetails)
		{
			if (param.SourceId == null)
				throw new Exception(ErrMsg.WH_NOEXIT_BOX_ID);


			T_FM_AUTO_PRINT_RECORD record = new T_FM_AUTO_PRINT_RECORD();
			record.PRINTER_COMPUTER_NAME = param.PrinterComputerName;
			record.PRINTER_NAME = param.PrinterName;
			record.PRINT_NUMBER = param.PrintNumber;
			record.PRINT_TIME = param.PrintTime;
			record.SOURCE_ID = param.SourceId;
			record.SOURCE_TYPE = param.SourceType;
			record.TEMPLET_FILE_NAME = param.TempletFileName;
			record.REMARK = param.Remark;


			addPrintRecords.Add(record);

		}

    }
}
