private string UpdateContractorInfo(ContractorQualification cq, out string contractorID)
        {
            contractorID = cq.ContractorID.ToString();
            try
            {
                if (!string.IsNullOrEmpty(cq.ContractorID.ToString()))
                {
                    var contractorQualification = Db.ContractorQualifications.Where(c => c.ContractorName == cq.ContractorName && c.isvalid == 1).ToList();
                    if (contractorQualification.Count >= 1)
                    {
                        if (contractorQualification.Count == 1)
                        {
                            if (contractorID != contractorQualification[0].ContractorID)
                            {
                                return("ContractorID is being created,ContractorID  isn't the same!");
                            }
                        }
                        else
                        {
                            return("ContractorID  already exists");
                        }
                    }
                    var _ContractorQualification = Db.ContractorQualifications.Find(cq.ContractorID);
                    if (_ContractorQualification != null)
                    {
                        _ContractorQualification.ContractorID        = cq.ContractorID;
                        _ContractorQualification.ContractorName      = cq.ContractorName.ToUpper();
                        _ContractorQualification.CType               = cq.CType;
                        _ContractorQualification.isvalid             = cq.isvalid;
                        _ContractorQualification.Remark              = cq.Remark;
                        _ContractorQualification.DepartmentID        = cq.DepartmentID;
                        _ContractorQualification.UserID              = cq.UserID;
                        _ContractorQualification.AccDate             = cq.AccDate;
                        _ContractorQualification.Status              = cq.Status;
                        _ContractorQualification.Rcode               = cq.Rcode;
                        _ContractorQualification.Type                = cq.Type;
                        _ContractorQualification.Stamp               = DateTime.Now;
                        _ContractorQualification.Region              = cq.Region;
                        _ContractorQualification.StartDate           = cq.StartDate;
                        _ContractorQualification.EndDate             = cq.EndDate;
                        _ContractorQualification.ContractorByEmloyee = cq.ContractorByEmloyee;
                        _ContractorQualification.InternalNumber      = cq.InternalNumber;
                        _ContractorQualification.Email               = cq.Email;
                        _ContractorQualification.ContractorFile      = cq.ContractorFile;
                        _ContractorQualification.EmailContractor     = cq.EmailContractor;

                        Db.Entry(_ContractorQualification).State = EntityState.Modified;
                    }
                    else
                    {
                        return("ContractorID is not exist!");
                    }
                    Db.SaveChanges();
                    JsonObject json = new JsonObject();
                    json["contractorID"] = cq.ContractorID;
                    contractorID         = json.ToString();
                    return("");
                }
                else
                {
                    return("ContractorID is Null!");
                }
            }
            catch (Exception e)
            {
                Loger.Error(e);
                return(e.Message + e.StackTrace);
            }
        }
        public IHttpActionResult Save(EmployeeList EmpList)
        {
            Console.WriteLine("Save");
            try
            {
                foreach (var contractortoDb in EmpList._employeeList)
                {
                    var contractorInDb = Db.Contractors.Find(contractortoDb.EmployeeID, contractortoDb.ContractorID);

                    if (contractorInDb != null)
                    {
                        contractorInDb.IdCard       = contractortoDb.IdCard;
                        contractorInDb.ContractorID = contractortoDb.ContractorID;
                        contractorInDb.VoucherID    = contractorInDb.VoucherID;
                        contractorInDb.Name         = contractortoDb.Name;
                        contractorInDb.Mark         = contractortoDb.Mark;
                        contractorInDb.Birthday     = contractortoDb.Birthday;
                        contractorInDb.Sex          = contractortoDb.Sex;
                        contractorInDb.Job          = contractortoDb.Job;
                        contractorInDb.Region       = contractortoDb.Region;
                        contractorInDb.Remark       = contractortoDb.Remark;
                        contractorInDb.Phone        = contractortoDb.Phone;

                        if (contractortoDb.Status == "W")
                        {
                            contractorInDb.IsUpload = 0;
                        }

                        if (contractortoDb.Status != "P")
                        {
                            contractorInDb.PreStatus = contractortoDb.Status;
                        }

                        if (contractortoDb.Status == "N" || contractortoDb.Status == "W" || contractortoDb.Status == "P" || contractortoDb.Status == "RT")
                        {
                            contractorInDb.Status = contractortoDb.Status;
                        }
                        else
                        {
                            contractorInDb.Status = contractortoDb.IsUpdate ? "Q" : "F";
                        }

                        contractorInDb.Stamp             = DateTime.Now;
                        contractorInDb.UserId            = contractorInDb.UserId;
                        contractorInDb.EmployeeID        = contractorInDb.EmployeeID;
                        contractorInDb.ReasonReturn      = null;
                        contractorInDb.InsuranceDuration = contractortoDb.InsuranceDuration;
                        contractorInDb.SafetyCerDuration = contractortoDb.SafetyCerDuration;
                        contractorInDb.IsForeign         = contractortoDb.IsForeign;

                        if (contractortoDb.IsForeign == 1)
                        {
                            var Foreigner = Db.ContractorForeigners.Find(contractortoDb.EmployeeID);
                            if (Foreigner != null)
                            {
                                Foreigner.PassPort_Expiry      = contractortoDb.PassPort_Expiry;
                                Foreigner.PassPort_Nationality = contractortoDb.PassPort_Nationality;
                                Foreigner.WorkPermit_No        = contractortoDb.WorkPermit_No;
                                Foreigner.WorkPermit_Start     = contractortoDb.WorkPermit_Start;
                                Foreigner.WorkPermit_End       = contractortoDb.WorkPermit_End;
                                Foreigner.CategoryCard         = contractortoDb.CategoryCard;
                                Foreigner.Card_Type            = contractortoDb.Card_Type;
                                Foreigner.Card_No    = contractortoDb.Card_No;
                                Foreigner.Card_Start = contractortoDb.Card_Start;
                                Foreigner.Card_End   = contractortoDb.Card_End;

                                Db.Entry(Foreigner).State = EntityState.Modified;
                            }
                            else
                            {
                                var _Foreigner = new ContractorForeigner
                                {
                                    EmployeeID           = contractortoDb.EmployeeID,
                                    PassPort_Expiry      = contractortoDb.PassPort_Expiry,
                                    PassPort_Nationality = contractortoDb.PassPort_Nationality,
                                    WorkPermit_No        = contractortoDb.WorkPermit_No,
                                    WorkPermit_Start     = contractortoDb.WorkPermit_Start,
                                    WorkPermit_End       = contractortoDb.WorkPermit_End,
                                    CategoryCard         = contractortoDb.CategoryCard,
                                    Card_Type            = contractortoDb.Card_Type,
                                    Card_No    = contractortoDb.Card_No,
                                    Card_Start = contractortoDb.Card_Start,
                                    Card_End   = contractortoDb.Card_End
                                };

                                Db.ContractorForeigners.Add(_Foreigner);
                            }
                        }
                        else
                        {
                            var Foreigner = Db.ContractorForeigners.Find(contractortoDb.EmployeeID);
                            if (Foreigner != null)
                            {
                                Db.Entry(Foreigner).State = EntityState.Deleted;
                            }
                        }

                        if (contractortoDb.FileName != null)
                        {
                            var File = Db.ContractorEmployeeFiles.Find(contractortoDb.VoucherID);
                            if (File != null)
                            {
                                File.VoucherID       = contractortoDb.VoucherID;
                                File.FileName        = contractortoDb.FileName;
                                File.Stamp           = DateTime.Now;
                                File.CreateTime      = File.CreateTime;
                                Db.Entry(File).State = EntityState.Modified;
                            }
                            else
                            {
                                var _file = new ContractorEmployeeFile
                                {
                                    VoucherID  = contractorInDb.VoucherID,
                                    FileName   = contractortoDb.FileName,
                                    Stamp      = DateTime.Now,
                                    CreateTime = DateTime.Now
                                };

                                Db.ContractorEmployeeFiles.Add(_file);
                            }
                        }
                        else
                        {
                            var File = Db.ContractorEmployeeFiles.Find(contractortoDb.VoucherID);
                            if (File != null)
                            {
                                Db.Entry(File).State = EntityState.Deleted;
                            }
                        }

                        Db.Entry(contractorInDb).State = EntityState.Modified;
                    }
                    else
                    {
                        contractorInDb = new Contractor
                        {
                            VoucherID         = contractortoDb.VoucherID,
                            EmployeeID        = oAC.SelectScalar <string>("select dbo.fnContractorEmployeeID(@typeID)", new object[] { "Employee" }),
                            IdCard            = contractortoDb.IdCard,
                            Name              = contractortoDb.Name,
                            ContractorID      = contractortoDb.ContractorID,
                            Mark              = contractortoDb.Mark,
                            Birthday          = contractortoDb.Birthday,
                            Sex               = contractortoDb.Sex,
                            Job               = contractortoDb.Job,
                            Region            = contractortoDb.Region,
                            Remark            = contractortoDb.Remark,
                            Status            = contractortoDb.Status,
                            PreStatus         = contractortoDb.Status,
                            Stamp             = DateTime.Now,
                            UserId            = contractortoDb.UserId,
                            InsuranceDuration = contractortoDb.InsuranceDuration,
                            SafetyCerDuration = contractortoDb.SafetyCerDuration,
                            IsForeign         = contractortoDb.IsForeign,
                            Phone             = contractortoDb.Phone
                        };

                        Db.Contractors.Add(contractorInDb);

                        var _biometric = new ContractorBiometric
                        {
                            EmployeeID = contractorInDb.EmployeeID,
                            CardNo     = null,
                            FaceTmp    = null
                        };

                        Db.ContractorBiometrics.Add(_biometric);

                        if (contractortoDb.IsForeign == 1)
                        {
                            var _Foreigner = new ContractorForeigner
                            {
                                EmployeeID           = contractorInDb.EmployeeID,
                                PassPort_Expiry      = contractortoDb.PassPort_Expiry,
                                PassPort_Nationality = contractortoDb.PassPort_Nationality,
                                WorkPermit_No        = contractortoDb.WorkPermit_No,
                                WorkPermit_Start     = contractortoDb.WorkPermit_Start,
                                WorkPermit_End       = contractortoDb.WorkPermit_End,
                                CategoryCard         = contractortoDb.CategoryCard,
                                Card_Type            = contractortoDb.Card_Type,
                                Card_No    = contractortoDb.Card_No,
                                Card_Start = contractortoDb.Card_Start,
                                Card_End   = contractortoDb.Card_End
                            };

                            Db.ContractorForeigners.Add(_Foreigner);
                        }

                        if (contractortoDb.FileName != null)
                        {
                            var File = Db.ContractorEmployeeFiles.Find(contractortoDb.VoucherID);
                            if (File == null)
                            {
                                var _file = new ContractorEmployeeFile
                                {
                                    VoucherID  = contractorInDb.VoucherID,
                                    FileName   = contractortoDb.FileName,
                                    Stamp      = DateTime.Now,
                                    CreateTime = DateTime.Now
                                };

                                Db.ContractorEmployeeFiles.Add(_file);
                            }
                        }
                    }
                    Db.SaveChanges();
                }

                return(Ok());
            }
            catch (Exception e)
            {
                Loger.Error(e);
                Console.WriteLine(e.Message);
                return(BadRequest(e.Message));
            }
        }
        public IHttpActionResult Save(ContractorDto contractortoDb)
        {
            Console.WriteLine("Save");
            try
            {
                var contractorInDb = Db.Contractors.Find(contractortoDb.EmployeeID, contractortoDb.ContractorID);

                //update
                if (contractorInDb != null)
                {
                    contractorInDb.IdCard       = contractortoDb.IdCard;
                    contractorInDb.ContractorID = contractortoDb.ContractorID;
                    contractorInDb.VoucherID    = contractorInDb.VoucherID;
                    contractorInDb.Name         = contractortoDb.Name;
                    contractorInDb.Phone        = contractortoDb.Phone;
                    contractorInDb.Birthday     = contractortoDb.Birthday;
                    contractorInDb.Sex          = contractortoDb.Sex;
                    contractorInDb.Job          = contractortoDb.Job;
                    contractorInDb.Region       = contractortoDb.Region;
                    contractorInDb.Remark       = contractortoDb.Remark;
                    if (contractortoDb.Status == "N" || contractortoDb.Status == "X")
                    {
                        contractorInDb.Status = contractortoDb.Status;
                    }
                    else
                    {
                        contractorInDb.Status = contractortoDb.IsUpdate ? "Q" : "F";
                    }
                    contractorInDb.PreStatus       = contractortoDb.Status;
                    contractorInDb.Stamp           = DateTime.Now;
                    contractorInDb.UserId          = contractortoDb.UserId;
                    contractorInDb.TrainDate       = contractortoDb.TrainDate;
                    contractorInDb.AppointmentDate = contractortoDb.AppointmentDate;
                    contractorInDb.EmployeeID      = contractorInDb.EmployeeID;
                    contractorInDb.ReasonReturn    = null;
                    Db.Entry(contractorInDb).State = EntityState.Modified;
                }
                else
                {
                    contractorInDb = new Contractor
                    {
                        VoucherID       = contractortoDb.VoucherID,
                        EmployeeID      = oAC.SelectScalar <string>("select dbo.fnContractorEmployeeID(@typeID)", new object[] { "Employee" }),
                        IdCard          = contractortoDb.IdCard,
                        Name            = contractortoDb.Name,
                        ContractorID    = contractortoDb.ContractorID,
                        Phone           = contractortoDb.Phone,
                        Birthday        = contractortoDb.Birthday,
                        Sex             = contractortoDb.Sex,
                        Job             = contractortoDb.Job,
                        Region          = contractortoDb.Region,
                        Remark          = contractortoDb.Remark,
                        Status          = contractortoDb.Status,
                        PreStatus       = contractortoDb.Status,
                        Stamp           = DateTime.Now,
                        UserId          = contractortoDb.UserId,
                        TrainDate       = contractortoDb.TrainDate,
                        AppointmentDate = contractortoDb.AppointmentDate
                    };

                    Db.Contractors.Add(contractorInDb);

                    var _biometric = new ContractorBiometric
                    {
                        EmployeeID = contractorInDb.EmployeeID,
                        CardNo     = null,
                        FaceTmp    = null,
                        IsValid    = 0
                    };
                    Db.ContractorBiometrics.Add(_biometric);
                }

                Db.SaveChanges();

                return(Ok());
            }
            catch (Exception e)
            {
                Loger.Error(e);
                return(BadRequest(e.Message));
            }
        }