public bool DeleteIndexTypeUser(string indexTypeCode, string userCode, string userCodeBy)
        {
            Dictionary<string, object> param = new Dictionary<string, object>();
            // Get IndexTypeUser
            param.Add("indexTypeCode", indexTypeCode);
            param.Add("userCode", userCode);
            MstIndexTypeUserModels mstIndexTypeUser = mapper.QueryForObject<MstIndexTypeUserModels>("Master.selectIndexTypeUser", param);

            DateTime? oldFromDate = null;
            DateTime? oldToDate = null;
            // If not exist -> return true
            if (mstIndexTypeUser == null)
            {
                return true;
            }
            else // If exist -> Update
            {
                mapper.Delete("Master.deleteIndexTypeUser", param);

                oldFromDate = mstIndexTypeUser.fromDate;
                oldToDate = mstIndexTypeUser.toDate;
            }

            // Insert into Transaction
            TraTransactionDAO transactionDAO = new TraTransactionDAO(this.mapper);
            transactionDAO.insertUserTypeUserTransaction(
                    Constants.TransactionType.Delete // string transactionType
                    , indexTypeCode // string indexTypeCode
                    , userCode // string userCode
                    , null // string packageCode
                    , DateTime.Now  // DateTime transactionDate
                    , oldFromDate   // DateTime oldFromDate
                    , oldToDate // DateTime oldToDate
                    , null // DateTime newFromDate
                    , null   // DateTime newToDate
                    , userCodeBy    // string userCodeBy
                );

            return true;
        }
        public bool SaveIndexTypeUser(SysUserViewModels model, string userCodeBy, string transactionType)
        {
            // Get package
            MstAccessPackageDAO accessPackageDAO = new MstAccessPackageDAO(this.mapper);
            IList<MstAccessPackageModels> packageList = accessPackageDAO.GetAccessPackageList(model.packageCode, null, -1, Constants.Status.Active);
            if (packageList == null || packageList.Count <= 0)
            {
                model.errorMessage = String.Format(Resource.MsgSuccess, new object[] { Resource.PackageCode, model.packageCode });
                return true;
            }
            MstAccessPackageModels packageModel = packageList[0];

            Dictionary<string, object> param = new Dictionary<string, object>();
            // Get IndexTypeUser
            param.Add("indexTypeCode", model.indexTypeCode);
            param.Add("userCode", model.userCode);
            MstIndexTypeUserModels mstIndexTypeUser = mapper.QueryForObject<MstIndexTypeUserModels>("Master.selectIndexTypeUser", param);

            MstIndexTypeUserModels newModel = new MstIndexTypeUserModels();
            newModel.userCode = model.userCode;
            newModel.indexTypeCode = model.indexTypeCode;
            newModel.viewFlg = Constants.Flag.HasRight;
            newModel.updatedDate = DateTime.Now;
            newModel.updatedBy = userCodeBy;
            DateTime? oldFromDate = null;
            DateTime? oldToDate = null;
            // If not exist -> Insert
            if (mstIndexTypeUser == null)
            {

                newModel.fromDate = DateUtils.getNowWithoutTime();
                newModel.toDate = newModel.fromDate.AddDays(packageModel.duration);
                newModel.createdDate = DateTime.Now;
                newModel.createdBy = userCodeBy;
                mapper.Insert("Master.insertIndexTypeUser", newModel);
            }
            else // If exist -> Update
            {
                // If user has no right or to date < current date
                // Set To Date = New + duraction
                if (mstIndexTypeUser.viewFlg.Equals(Constants.Flag.NoRight) || mstIndexTypeUser.toDate < DateTime.Now)
                {
                    newModel.toDate = DateUtils.getNowWithoutTime().AddDays(packageModel.duration);
                }
                else // Set To Date = To Date + duraction
                {
                    newModel.toDate = mstIndexTypeUser.toDate.AddDays(packageModel.duration);
                }
                newModel.createdDate = mstIndexTypeUser.createdDate;
                newModel.createdBy = mstIndexTypeUser.createdBy;
                newModel.fromDate = mstIndexTypeUser.fromDate;
                mapper.Update("Master.updateIndexTypeUser", newModel);

                oldFromDate = mstIndexTypeUser.fromDate;
                oldToDate = mstIndexTypeUser.toDate;
            }

            // Insert into Transaction
            TraTransactionDAO transactionDAO = new TraTransactionDAO(this.mapper);
            transactionDAO.insertUserTypeUserTransaction(
                    transactionType // string transactionType
                    , model.indexTypeCode // string indexTypeCode
                    , model.userCode // string userCode
                    , model.packageCode // string packageCode
                    , DateTime.Now  // DateTime transactionDate
                    , oldFromDate   // DateTime oldFromDate
                    , oldToDate // DateTime oldToDate
                    , newModel.fromDate // DateTime newFromDate
                    , newModel.toDate   // DateTime newToDate
                    , userCodeBy    // string userCodeBy
                );

            model.infoMessage = Resource.MsgSuccess;
            return true;
        }