/* public List<VIMS_BIZ_INVOICE> FetchList(Condition condition)
         * {
         *
         *   using (TaxEntities db = new TaxEntities())
         *   {
         *
         *       var datas = condition.Filter(db.VIMS_BIZ_INVOICE);
         *
         *       //if (!string.IsNullOrEmpty(condition.Location))
         *       //{
         *       //    datas = datas.Where(o => o.Address.Contains(condition.Location));
         *
         *       //}
         *       //var data = datas.OrderByDescending(o => o.KPRQ).DoPage(condition.Pager).ToList();
         *       var data = datas.OrderByDescending(o => o.KPRQ).ToList();
         *       return data;
         *
         *   }
         *
         * }*/

        public ImportInvoiceListDTO FetchList(ImportInvoiceDTO condition)
        {
            ImportInvoiceListDTO rs = new ImportInvoiceListDTO
            {
                List   = null,
                Result = new ImportInvoiceResultDTO
                {
                    Message = "",
                    Status  = 0
                }
            };

            using (TaxEntities db = new TaxEntities())
            {
                var datas = db.VIMS_BIZ_INVOICE.Where(o =>
                                                      !string.IsNullOrEmpty(condition.InvoiceCode) ? o.FPDM == condition.InvoiceCode : true
                                                      &&
                                                      !string.IsNullOrEmpty(condition.InvoiceCode) ? o.FPDM == condition.InvoiceCode : true
                                                      );

                if (!string.IsNullOrEmpty(condition.InvoiceCode))
                {
                    datas = datas.Where(o => o.FPDM == condition.InvoiceCode);
                }
                // var data = datas.OrderByDescending(o => o.KPRQ).DoPage(condition.Pager).ToList();
                var data = datas.Select(o => new ImportInvoiceDTO
                {
                    InvoiceCode   = o.FPDM,
                    InvoiceNumber = o.FPHM,
                    // CreateDate = o.KPRQ.ToString(),
                    Amount         = o.FPJE,
                    Tax            = o.FPSE,
                    SalesTaxNumber = o.XFSH,
                }).OrderByDescending(o => o.InvoiceCode).ToList();
                rs.List = data;
            }
            return(rs);
        }
        public ImportInvoiceListDTO InjectList(ImportInvoiceListDTO soap)
        {
            int    probe        = 0;
            string exceptionMsg = "";

            using (TaxEntities db = new TaxEntities())
            {
                foreach (var model in soap.List)
                {
                    VIMS_BIZ_INVOICE entity = null;
                    if (db.VIMS_BIZ_INVOICE.Any(o => o.FPDM == model.InvoiceCode && o.FPHM == model.InvoiceNumber))
                    {//修改
                        entity = db.VIMS_BIZ_INVOICE.First(o => o.FPDM == model.InvoiceCode && o.FPHM == model.InvoiceNumber);
                        //db.VIMS_BIZ_INVOICE.Remove(old);
                        entity.DKZT = model.DeductionStatus;
                        // entity.QRBZ = model.IsChosen == "是" ? "2" : "1";
                        entity.XFMC = model.SalesName;
                        entity.SHRQ = DateTime.Now;
                        entity.XFSH = string.IsNullOrEmpty(model.SalesTaxNumber)?  "N/A": model.SalesTaxNumber;//model.SalesTaxNumber;//销方税号(货运为承运人,机动车为纳税人识别号)
                    }
                    else
                    {//新增
                        entity      = new VIMS_BIZ_INVOICE();
                        entity.ID   = Guid.NewGuid().ToString();
                        entity.DKZT = model.DeductionStatus;
                        // entity.QRBZ = model.IsChosen == "是" ? "2" : "1";
                        entity.XFMC = model.SalesName;
                        entity.SHRQ = DateTime.Now;
                        entity.XFSH = string.IsNullOrEmpty(model.SalesTaxNumber) ? "N/A" : model.SalesTaxNumber;//model.SalesTaxNumber;//销方税号(货运为承运人,机动车为纳税人识别号)
                        #region 非空冗余
                        entity.FPLB = "0";
                        entity.GFSH = model.TaxCode;
                        entity.FPMW = "N/A";
                        // entity.FPSL = 0.0M;//发票税率(只有机动车销售发票显示税率)
                        entity.QDBZ = "N";//清单标识(当清单标识为Y时发票为汉字防伪发票)
                        #endregion
                        #region Mapping
                        entity.RZJG = "0"; //认证结果(0:认证通过,1:认证未通过,255:待认证)
                        entity.RZZT = "0"; //!string.IsNullOrEmpty(model.CertificateStatus) && string.Compare(model.CertificateStatus, "已确认", StringComparison.InvariantCultureIgnoreCase) == 0 ? "0" : "1"; //认证结果(0:认证通过,1:认证未通过,255:待认证)//认证状态 0:可以认证 1:不能认证
                        entity.FPDM = model.InvoiceCode;
                        entity.FPHM = model.InvoiceNumber;
                        DateTime d_createDate = DateTime.Now;
                        entity.KPRQ = DateTime.TryParse(model.CreateDate, out d_createDate) ? d_createDate : d_createDate;


                        entity.FPJE = model.Amount;
                        entity.FPSE = model.Tax;
                        //??????
                        entity.FPLY = "0";//??????发票来源 FPLY(0:手工录入;1:扫描采集;2:接口传入;3:导入;4:录入)  /  来源 RZLY(0:系统认证;1:其他认证)

                        //entity.??? = model.SelectTag;
                        //entity.RZSJ = model.OperationTime;

                        #endregion
                        entity = db.VIMS_BIZ_INVOICE.Add(entity);
                    }
                }
                this.Errors = GetErrors(db);
                if (!this.HasError)
                {
                    try
                    {
                        //db.SaveChanges();
                        probe = db.SaveChanges();
                    }
                    catch (Exception ex)
                    {
                        exceptionMsg = ex.Message;
                    }
                }
                else
                {
                    exceptionMsg = this.Errors != null && this.Errors.Count > 0 ? this.Errors.Values.FirstOrDefault() : "DTO校验异常";
                }
            }
            soap.Result = new ImportInvoiceResultDTO
            {
                Message = !string.IsNullOrEmpty(exceptionMsg) ? exceptionMsg : "SUCCESS",
                Status  = probe
            };
            return(soap);
        }
        /* public List<VIMS_BIZ_INVOICE> FetchList(Condition condition)
         * {
         *
         *   using (TaxEntities db = new TaxEntities())
         *   {
         *
         *       var datas = condition.Filter(db.VIMS_BIZ_INVOICE);
         *
         *       //if (!string.IsNullOrEmpty(condition.Location))
         *       //{
         *       //    datas = datas.Where(o => o.Address.Contains(condition.Location));
         *
         *       //}
         *       //var data = datas.OrderByDescending(o => o.KPRQ).DoPage(condition.Pager).ToList();
         *       var data = datas.OrderByDescending(o => o.KPRQ).ToList();
         *       return data;
         *
         *   }
         *
         * }*/

        public ImportInvoiceListDTO FetchList(ImportInvoiceDTO condition)
        {
            ImportInvoiceListDTO rs = new ImportInvoiceListDTO
            {
                List   = null,
                Result = new ImportInvoiceResultDTO
                {
                    Message = "",
                    Status  = 0
                }
            };

            try
            {
                using (TaxEntities db = new TaxEntities())
                {
                    var datas = db.VIMS_BIZ_INVOICE.Where(o =>
                                                          !string.IsNullOrEmpty(condition.InvoiceCode) ? o.FPDM == condition.InvoiceCode : true
                                                          &&
                                                          !string.IsNullOrEmpty(condition.InvoiceNumber) ? o.FPDM == condition.InvoiceNumber : true

                                                          );

                    //if (!string.IsNullOrEmpty(condition.IsChosen))
                    //{
                    //    datas = datas.Where(o => o.GSBZ == condition.IsChosen);

                    //}
                    //if (!string.IsNullOrEmpty(condition.IsConfirmed))
                    //{
                    //    datas = datas.Where(o => o.QRBZ == condition.IsConfirmed);

                    //}

                    if (condition.BeginDateTime.HasValue)
                    {
                        datas = datas.Where(o => o.KPRQ >= condition.BeginDateTime.Value);
                    }
                    if (condition.EndDateTime.HasValue)
                    {
                        datas = datas.Where(o => o.KPRQ <= condition.EndDateTime.Value);
                    }
                    if (!string.IsNullOrEmpty(condition.TaxCode))
                    {
                        datas = datas.Where(o => o.GFSH == condition.TaxCode);
                    }
                    // var data = datas.OrderByDescending(o => o.KPRQ).DoPage(condition.Pager).ToList();
                    var data = datas.Select(o => new ImportInvoiceDTO
                    {
                        InvoiceCode    = o.FPDM,
                        InvoiceNumber  = o.FPHM,
                        Amount         = o.FPJE,
                        Tax            = o.FPSE,
                        SalesTaxNumber = o.XFSH,
                        //IsChosen = o.GSBZ,
                        //IsConfirmed == o.QRBZ,
                        CreateDateTime = o.KPRQ,
                        //CreateDate = o.KPRQ.ToString("yyyy-MM-dd"),//"LINQ to Entities 不识别方法“System.String ToString(System.String)”,因此该方法无法转换为存储表达式。"
                        IsChosen    = "1",
                        IsConfirmed = "0"
                    }).OrderByDescending(o => o.InvoiceCode).ToList();
                    rs.List          = data;
                    rs.Result.Status = data.Count;
                }
            }
            catch (Exception ex)
            {
                rs.Result.Status  = -1;
                rs.Result.Message = ex.Message;
            }
            return(rs);
        }