public Result Loan(int assetId, int targetDeptId, int targetAccountId, DateTime loanAt, DateTime expectedReturnAt, string pics, string newPositon, string remark, IAppUser user)
        {
            // 验证参数
            if (assetId <= 0)
            {
                return(ResultUtil.Do(ResultCodes.验证失败, "指定了无效的资产id"));
            }
            if (targetDeptId <= 0)
            {
                return(ResultUtil.Do(ResultCodes.验证失败, "指定了无效的部门id"));
            }
            if (loanAt < DateTime.Parse("1900-1-1") || expectedReturnAt < DateTime.Parse("1900-1-1"))
            {
                return(ResultUtil.Do(ResultCodes.验证失败, "借出和预计归还日期必须大于1900-1-1"));
            }
            if (loanAt > expectedReturnAt)
            {
                return(ResultUtil.Do(ResultCodes.验证失败, "预计归还日期必须大于等于借出日期"));
            }

            var loan  = new Loan();
            var asset = LoadDto(assetId);

            if (asset == null)
            {
                return(ResultUtil.Do(ResultCodes.数据不存在, "资产不存在或已删除"));
            }
            if (asset.State == "借出" || asset.State == "报废")
            {
                return(ResultUtil.Do(ResultCodes.验证失败, "该资产已借出或已报废,禁止借出操作"));
            }

            if (targetAccountId > 0)
            {
                var accountService = new AccountService();
                var account        = accountService.Load(targetAccountId);

                if (account == null)
                {
                    return(ResultUtil.Do(ResultCodes.数据不存在, "目标使用人不存在或已删除"));
                }

                var dept = DeptUtil.Get(account.DeptId);
                if (dept == null)
                {
                    throw new Exception("无法找到员工所属部门。员工ID:" + account.Id.ToString() + ";部门ID:" + account.DeptId.ToString());
                }

                loan.TargetAccountId   = targetAccountId;
                loan.TargetAccountName = account.Name;
                loan.TargetDeptId      = targetDeptId;
                loan.TargetDeptName    = dept.Name;
            }
            else
            {
                var dept = DeptUtil.Get(targetDeptId);
                if (dept == null)
                {
                    return(ResultUtil.Do(ResultCodes.数据不存在, "目标部门不存在或已删除"));
                }
                loan.TargetAccountId   = 0;
                loan.TargetAccountName = "";
                loan.TargetDeptId      = dept.Id;
                loan.TargetDeptName    = dept.Name;
            }

            loan.AssetId          = asset.Id;
            loan.AssetCode        = asset.Code;
            loan.AssetName        = asset.Name;
            loan.FromAccountId    = asset.AccountId;
            loan.FromAccountName  = asset.AccountName;
            loan.FromDeptId       = asset.DeptId;
            loan.FromDeptName     = asset.DeptName;
            loan.LoanAt           = loanAt;
            loan.ExpectedReturnAt = expectedReturnAt;
            loan.IsReturn         = false;
            loan.ReturnAt         = null;
            loan.Pics             = pics;

            var log = new AssetLog
            {
                AssetId           = loan.AssetId,
                AssetCode         = loan.AssetCode,
                AssetName         = loan.AssetName,
                FromAccountId     = loan.FromAccountId,
                FromAccountName   = loan.FromAccountName,
                FromDeptId        = loan.FromDeptId,
                FromDeptName      = loan.FromDeptName,
                TargetAccountId   = loan.TargetAccountId,
                TargetAccountName = loan.TargetAccountName,
                TargetDeptId      = loan.TargetDeptId,
                TargetDeptName    = loan.TargetDeptName,
                Type      = "借出",
                OperateAt = loan.LoanAt,
                Pics      = loan.Pics
            };

            loan.BeforeCreate(user);
            log.BeforeCreate(user);

            var sqls = new KeyValuePairList
            {
                {
                    "UPDATE Asset_Asset SET [State]='借出',Position=@Position,DeptId=@DeptId,AccountId=@AccountId,UpdateAt=GETDATE(),UpdateBy=@UserId,Updator=@UserName WHERE Id=@Id",
                    new
                    {
                        Id        = log.AssetId,
                        Position  = newPositon,
                        UserId    = user.Id,
                        UserName  = user.Name,
                        AccountId = log.TargetAccountId,
                        DeptId    = log.TargetDeptId
                    }
                },
                { db.GetCommonInsertSql <Loan>(), loan },
                { db.GetCommonInsertSql <AssetLog>(), log }
            };

            var row = db.ExecuteTran(sqls);

            return(row ? ResultUtil.Success() : ResultUtil.Do(ResultCodes.数据库操作失败, "数据库写入失败"));
        }
        /// <summary>
        /// 转移资产
        /// </summary>
        public Result Move(AssetLog log, string newPosition, IAppUser user)
        {
            var asset = LoadDto(log.AssetId);

            if (asset == null)
            {
                return(ResultUtil.Do(ResultCodes.数据不存在, "指定的资产不存在或已删除"));
            }
            switch (asset.State)
            {
            case "报废":
                return(ResultUtil.Do(ResultCodes.数据不存在, "指定的资产已报废,禁止操作"));

            case "借出":
                return(ResultUtil.Do(ResultCodes.数据不存在, "指定的资产已借出,禁止操作"));
            }

            log.AssetCode = asset.Code;
            log.AssetName = asset.Name;

            if (log.OperateAt < DateTime.Parse("1900-1-1"))
            {
                return(ResultUtil.Do(ResultCodes.验证失败, "调配日期无效"));
            }

            DeptDto targetDept;

            if (log.TargetAccountId > 0)
            {
                var accountService = new AccountService();
                var targetAccount  = accountService.Load(log.TargetAccountId);

                if (targetAccount == null)
                {
                    return(ResultUtil.Do(ResultCodes.数据不存在, "目标用户不存在"));
                }

                targetDept = DeptUtil.Get(targetAccount.DeptId);
                if (targetDept == null)
                {
                    throw new Exception("用户所在的部门信息不存在,请联系管理员");
                }

                log.TargetAccountId   = targetAccount.Id;
                log.TargetAccountName = targetAccount.Name;
                log.TargetDeptId      = targetDept.Id;
                log.TargetDeptName    = targetDept.Name;
            }
            else
            {
                targetDept = DeptUtil.Get(log.TargetDeptId);
                if (targetDept == null)
                {
                    return(ResultUtil.Do(ResultCodes.数据不存在, "目标部门不存在"));
                }
                else
                {
                    log.TargetDeptId      = targetDept.Id;
                    log.TargetDeptName    = targetDept.Name;
                    log.TargetAccountId   = 0;
                    log.TargetAccountName = "";
                }
            }

            log.Type = "调配";

            var sql = new KeyValuePairList
            {
                {
                    "UPDATE Asset_Asset SET DeptId=@DeptId,@Position=@Position,AccountId=@AccountId,UpdateAt=GETDATE(),UpdateBy=@UserId,Updator=@UserName WHERE Id=@Id",
                    new { Id = log.AssetId, Position = newPosition, DeptId = log.TargetDeptId, AccountId = log.TargetAccountId, UserId = user.Id, UserName = user.Name }
                }
            };

            log.BeforeCreate(user);
            sql.Add(db.GetCommonInsertSql <AssetLog>(), log);

            var row = db.ExecuteTran(sql);

            return(row ? ResultUtil.Success() : ResultUtil.Do(ResultCodes.数据库操作失败, "数据库写入失败"));
        }